我是否应该删除 JSON API 响应中已经存在于 `data` 中的 `included` 模型?

Should I remove `included` models that are already in `data` in a JSON API response?

假设我有一个名为 topics 的模型,它是自引用的(主题 通过其 parent_topic_id 属于 主题)。

所以,我有一个名为 sports 的主题和一个名为 basketball 的儿童主题。

JSON API 响应当前正在序列化为:

{
  "data": [
    {
      "type": "topics",
      "id": "sports",
      "attributes": {
        "name": "Sports",
        "show-role-title": null,
        "created-at": "2017-04-16T21:19:25.000Z",
        "updated-at": null
      },
      "links": {
        "self": "/topics/sports"
      }
    },
    {
      "type": "topics",
      "id": "sports-basketball",
      "attributes": {
        "name": "Basketball",
        "show-role-title": null,
        "created-at": "2017-04-16T21:19:25.000Z",
        "updated-at": null
      },
      "relationships": {
        "parent-topic": {
          "data": {
            "type": "topics",
            "id": "sports"
          }
        }
      },
      "links": {
        "self": "/topics/sports-basketball"
      }
    }
  ],
  "included": [
    {
      "type": "topics",
      "id": "sports",
      "attributes": {
        "name": "Sports",
        "show-role-title": null,
        "parent-topic-id": null,
        "created-at": "2017-04-16T21:19:25.000Z",
        "updated-at": null
      },
      "links": {
        "self": "/topics/sports"
      }
    }
  ]
}

现在,考虑到 sports 已经在 data 中,但 basketball 相关,将其附加为有效是否有效included 条记录?

在这种情况下,您应该拥有两个资源 /topics/parentTopic 并且在主对象和包含对象中拥有相同的数据副本是有效的,前提是您的请求有这种必要.在您的情况下,请求将是 /topics?include=parentTopic

根据 JSON API spec,包含不是强制性的。我的建议是 /topics 可以,你可以从 /topics 关系中引用父主题数据(取决于客户端框架),如果你查询特定的资源记录,你可以添加包含 /topics/sports-basketball?include=parentTopic