更正 JSON API 中的空关系

Correct null relationship in JSON API

我有一个JSON API类型的关系

relationships {
    fruit: {data: {id: 123, type: "fruits"}}
}

实例没有水果关系怎么办?

我应该这样做吗

relationships {
    fruit: {data: {id: null, type: "fruits"}}
}

或这个

relationships {
    fruit: {data: null}
}

或这个

relationships {
    fruit: null
}

还是完全不同的东西?

根据规范,资源链接对象可能具有以下值:

Resource linkage MUST be represented as one of the following:

  • null for empty to-one relationships.
  • an empty array ([]) for empty to-many relationships.
  • a single resource identifier object for non-empty to-one relationships.
  • an array of resource identifier objects for non-empty to-many relationships.

https://jsonapi.org/format/#document-resource-object-linkage

仅来自您的示例

"relationships": {
    "fruit": {
      "data": null
    }
}
根据 JSON:API 规范,

是有效的 relationship object

这个关系对象告诉您的客户

  • resource object有关系fruit,
  • 这是一对一关系,
  • 并且没有任何相关资源。