假设我们有几个资源,它们可以单独存在,也可以按树状层次结构组织。为了方便起见,我称它们为根、树枝和叶子。现在我想检索叶子的数据:
GET /leaf/1
Accept: application/vnd.api+json
根据JSON API 规范,它必须返回我这样的东西:
{
"data": [{
"type": "leaf",
"id": "1",
"title": "Leaf 1",
"links": {
"self": "http://example.com/leaf/1",
"branch": {
"self": "http://example.com/leaf/1/links/branch",
"related": "http://example.com/leaf/1/branch",
"linkage": { "type": "branch", "id": "5" }
},
"root": {
"self": "http://example.com/leaf/1/links/root",
"related": "http://example.com/leaf/1/root",
"linkage": { "type": "root", "id": "7" }
}
}
}],
"included": [{
"type": "branch",
"id": "5",
"title": "Branch 5",
"links": {
"self": "http://example.com/branch/5"
}
}, {
"type": "root",
"id": "7",
"title": "Root 7",
"links": {
"self": "http://example.com/root/7"
}
}]
}
从响应数据中,我不能说数据是分层的,并且更改叶子的根似乎是合适的:
PATCH /leaf/1
Content-Type: application/vnd.api+json
{
"data": {
"type": "leaf",
"id": 1,
"links": {
"root": {
"linkage": { "type": "root", "id": "3"}
}
}
}
}
这当然是不可能的,因为这个叶子连接到分支,然后才连接到根,并且在下面的模式中更改根需要有这个根分支的 id。问题是:
- 如何(如果可能)在资源表示中表示层次结构,以使 API 用户清楚地知道变化的关系可能需要额外的数据?