neomerx / json-api

Framework agnostic JSON API (jsonapi.org) implementation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ResponsesInterface - relationships response missing.

lindyhopchris opened this issue · comments

The ResponsesInterface doesn't have a method for sending responses for fetch relationship requests:
http://jsonapi.org/format/#fetching-relationships

I.e. where just the relationship identifiers are requested by the client.

Happy to submit a pull request but wasn't sure if you've intentionally missed this out? It seems like it should be in there, because it is a valid response defined by the spec.

It's not clear if response should contain only identifiers. I've had impression it should be normal resources. For this reason it doesn't have any special reply method (getContentResponse does it fine).

Though we don't have response method for encoding data as identifiers. That's a clear omission you've spotted 😉

Your PR would be welcome

My thoughts

    public function getIdentifiersResponse(
        $data,
        $statusCode = self::HTTP_OK,
        $links = null,
        $meta = null,
        array $headers = []
    ) {
        $encoder = $this->getEncoder();
        $links === null ?: $encoder->withLinks($links);
        $meta === null ?: $encoder->withMeta($meta);
        $content = $encoder->encodeIdentifiers($data, $this->getEncodingParameters());

        return $this->createJsonApiResponse($content, $statusCode, $headers);
    }

Yep, the method signature looks good. Will put together a PR.

For info, the spec is totally clear about only containing identifiers. To use this example from your wiki:

{
    "data": {
        "type": "sites",
        "id": "1",
        "attributes": {},
        "relationships": {
            "posts": {
                "data": { "type": "posts", "id": "321" },
                "links": {
                    "self": "http://example.com/sites/1/relationships/posts",
                    "related": "http://example.com/sites/1/posts"
                }
            }
        }
    }
}

The spec says @ http://jsonapi.org/format/#fetching-resources

A server MUST support fetching resource data for every URL provided as:
...
a related link as part of a relationship-level links object

So the related link http://example.com/sites/1/posts returns the posts resource that is related to sites:1

The spec says @ http://jsonapi.org/format/#fetching-relationships

A server MUST support fetching relationship data for every relationship URL provided as a self link as part of a relationship’s links object.

So the self link http://example.com/sites/1/relationships/posts returns the relationship data, not the resource data.

If the self link returned the related resource object, then it would be identical to the related link - which is not the point of having the two separate links.

The example HTTP response from the server to a GET request to a relationship self link is shown in the spec as follows:

HTTP/1.1 200 OK
Content-Type: application/vnd.api+json

{
  "links": {
    "self": "/articles/1/relationships/author",
    "related": "/articles/1/author"
  },
  "data": {
    "type": "people",
    "id": "12"
  }
}

I.e. only an identifier.

Can you please send #141 and #144 today? Both are just a few lines. I've fixed a couple of enhancements today and would like to make a release.

yeah, i can do it this evening after work (UK time)