JSON API:显示链接的正确方法?

时间:2017-02-14 14:06:23

标签: specifications json-api

我使用Laravel制作了一个JSON API jsonapi.org

列出了Laravel 5 JSON API Transformer package

现在,一切都按预期工作,来自我的api的示例响应就像(顺便验证失败,所以我看了jsonapi规范):

{
  "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"
  }
}

但是根据jsonapi.org上的规范,链接应该是

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

我的问题是:

在我的示例输出中显示链接作为对象是否合法 用“href”?我很困惑,因为我使用的包是 列在jsonapi.org上,但似乎不符合规格。

顺便说一句:我的英语可能有点令人困惑,但我希望我尽可能地描述我的问题

1 个答案:

答案 0 :(得分:2)

这实际上是基于规范的有效JSON API输出,

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

  

在指定的地方,链接成员可用于表示链接。每个链接成员的值必须是一个对象(“链接对象”)。

来自规范的有效样本

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

链接对象的每个成员都是“链接”。链接必须表示为:

  • 包含链接网址的字符串。
  • 一个对象(“链接对象”),它可以包含以下成员:
    • href:包含链接URL的字符串。
    • meta:包含有关链接的非标准元信息的元对象。

因此,您的输出实际上是有效的。