JSON API 中的空稀疏字段集

Empty sparse fieldset in JSON API

我有一个资源(例如帖子)与其他资源(例如评论)有一对多关系。我不需要相关资源的任何字段,只需要它们的 self-links(按需异步获取它们)。响应应如下所示:

{
  "links": {
    "self": "http://example.com/posts/1?xxx",
  },
  "data": [{
    "type": "posts",
    "id": "1",
    "attributes": {
      "title": "JSON API paints my bikeshed!"
    },
    "relationships": {
      "comments": {
        "links": {
          "self": "http://example.com/posts/1/relationships/comments",
          "related": "http://example.com/posts/1/comments"
        },
        "data": [
          { "type": "comments", "id": "5" },
          { "type": "comments", "id": "12" }
        ]
      }
    },
    "links": {
      "self": "http://example.com/posts/1"
    }
  }],
  "included": [{
    "type": "comments",
    "id": "5",
    "links": {
      "self": "http://example.com/comments/5"
    }
  }, {
    "type": "comments",
    "id": "12",
    "links": {
      "self": "http://example.com/comments/12"
    }
  }]
}

我的问题是请求的 URL 应该是什么样子?

我的想法是包含评论,然后使用一个空的稀疏字段集来避免获得任何评论字段,但只有自己 link (+id, type)。 因此,它应该看起来像 http://example.com/posts/1?include=comments&fields[comments]=[]。 所以我需要类似空的稀疏字段集的东西。

JSON API 规范并没有过多说明稀疏字段集 (http://jsonapi.org/format/#fetching-sparse-fieldsets) 及其与 link 的关系。

JSON API 能够 answer my question

简而言之,指定空稀疏字段集的正确方法是:

http://example.com/posts/1?include=comments&fields[comments]=

有一个discussion关于是否在关系对象中包含指向各个关系项目的链接:

{
  "links": {
    "self": "http://example.com/posts/1",
  },
  "data": [{
    "type": "posts",
    "id": "1",
    "attributes": {
      "title": "JSON API paints my bikeshed!"
    },
    "relationships": {
      "comments": {
        "links": {
          "self": "http://example.com/posts/1/relationships/comments",
          "related": "http://example.com/posts/1/comments",
          "item": [ "http://example.com/comments/5", "http://example.com/comments/12" ]
        },
        "data": [{
           "type": "comments", 
           "id": "5", 
           "links": {
              "about": "http://example.com/comments/5"
            }
        }, {
           "type": "comments", 
           "id": "12", 
           "links": {
              "about": "http://example.com/comments/12"
            }
        }] 
      }
    },
    "links": {
      "self": "http://example.com/posts/1"
    }
  }]
}