Moritz Büttner
Moritz Büttner

Reputation: 904

JSON API: Correct way to show links?

I made a JSON API using Laravel with Laravel 5 JSON API Transformer package which was listed on jsonapi.org

For now, everything works as expected, a sample response from my api is like (btw the validation failed so i took a look at the jsonapi specs):

{
  "data": [
    {
      "type": "inventory",
      "id": "INV0001",
      "attributes": {
        "inv_inventory_id": "INV0001",
        "inv_owner_company_id": 1,
        "inv_owner_department_id": 1,
        "inv_user_department_id": 1,
        "inv_user_worker_id": 1,
        "title": "Schreibtisch"
      },
      "links": {
        "self": {
          "href": "http://127.0.0.1:8000/api/v2/inventory/INV0001"
        },
        "user": {
          "href": "http://127.0.0.1:8000/api/v2/worker/1"
        },
        "owner_dept": {
          "href": "http://127.0.0.1:8000/api/v2/department/1"
        },
        "owner_comp": {
          "href": "http://127.0.0.1:8000/api/v2/company/1"
        }
      },
      "relationships": {
        "worker": {
          "data": {
            "type": "worker",
            "id": "1"
          }
        },
        "department": {
          "data": {
            "type": "department",
            "id": "1"
          }
        },
        "company": {
          "data": {
            "type": "company",
            "id": "1"
          }
        }
      }
    }
  ],
  "included": [
    {
      "type": "worker",
      "id": "1",
      "attributes": {
        "wrk_forename": "Moritz",
        "wrk_surname": "ASDF",
        "wrk_department_id": 2,
        "wrk_homeoffice": true,
        "wrk_room_id": 1
      },
      "links": {
        "self": {
          "href": "http://127.0.0.1:8000/api/v2/worker/1"
        },
        "hardware": {
          "href": "http://127.0.0.1:8000/api/v2/worker/1/hardware"
        },
        "software": {
          "href": "http://127.0.0.1:8000/api/v2/worker/1/software"
        },
        "inventory": {
          "href": "http://127.0.0.1:8000/api/v2/worker/1/inventory"
        },
        "accessory": {
          "href": "http://127.0.0.1:8000/api/v2/worker/1/accessory"
        }
      }
    },
    {
      "type": "department",
      "id": "1",
      "attributes": {
        "department": "Entwicklung",
        "dept_floor_id": 3
      },
      "links": {
        "self": {
          "href": "http://127.0.0.1:8000/api/v2/department/1"
        },
        "floor": {
          "href": "http://127.0.0.1:8000/api/v2/floor/3"
        },
        "hardware": {
          "href": "http://127.0.0.1:8000/api/v2/department/1/hardware"
        },
        "software": {
          "href": "http://127.0.0.1:8000/api/v2/department/1/software"
        },
        "inventory": {
          "href": "http://127.0.0.1:8000/api/v2/department/1/inventory"
        },
        "accessory": {
          "href": "http://127.0.0.1:8000/api/v2/department/1/accessory"
        }
      }
    },
    {
      "type": "company",
      "id": "1",
      "attributes": {
        "company": "GermanPersonnel",
        "com_building_id": 1
      },
      "links": {
        "self": {
          "href": "http://127.0.0.1:8000/api/v2/company/1"
        }
      }
    }
  ],
  "links": {
    "self": {
      "url": "http://127.0.0.1:8000/api/v2/inventory?page[number]=1&page[size]=10"
    },
    "first": {
      "url": "http://127.0.0.1:8000/api/v2/inventory?page[number]=1&page[size]=10"
    },
    "last": {
      "url": "http://127.0.0.1:8000/api/v2/inventory?page[number]=1&page[size]=10"
    }
  },
  "meta": {
    "page": {
      "total": 1,
      "last": 1,
      "number": 1,
      "size": 10
    }
  },
  "jsonapi": {
    "version": "1.0"
  }
}

But according to the specifications on jsonapi.org a link should look like

  "links": {
    "self": "http://127.0.0.1:8000/api/v2/inventory/INV0001"
   },

My question is:

Is it legit to display a link like in my sample ouput as an object with "href"? I'm pretty confused, because the package i used was listed on jsonapi.org, but seems not to meet the specs.

BTW: My English might be a little bit confusing, but i hope i descriped my problem as well as possible

Upvotes: 3

Views: 343

Answers (1)

XYZ
XYZ

Reputation: 27387

This is actually valid JSON API output base on the specification,

http://jsonapi.org/format/#document-links

Where specified, a links member can be used to represent links. The value of each links member MUST be an object (a “links object”).

A Valid Sample From Specification

"links": {
  "related": {
    "href": "http://example.com/articles/1/comments",
    "meta": {
      "count": 10
    }
  }
}

Each member of a links object is a “link”. A link MUST be represented as either:

  • a string containing the link’s URL.
  • an object (“link object”) which can contain the following members:
    • href: a string containing the link’s URL.
    • meta: a meta object containing non-standard meta-information about the link.

Therefore, your output is actually valid.

Upvotes: 2

Related Questions