neomerx / json-api

Framework agnostic JSON API (jsonapi.org) implementation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Self link in an included record

steverhoades opened this issue · comments

I'm trying to get the self links to appear for included records but have not been successful thus far. I'm hoping I'm missing something completely obvious here. Any assistance that could be provided here would be greatly appreciated.

For Schema:

class ArticleSchema extends SchemaProvider
{
    protected $resourceType = 'articles';
    
    protected $selfSubUrl = '/articles';

    public function getId($resource)
    {
        return $resource->getIdentifier();
    }

    public function getAttributes($resource)
    {
        return $resource->toArray();
    }

    public function getRelationships($resource, $isPrimary, array $includeRelationships)
    {
        return [
            'author' => [
                self::DATA => $resource->author,
                self::SHOW_RELATED => true,
                self::SHOW_SELF => true
            ]
        ];
    }

    public function getIncludePaths()
    {
        return [
            'author'
        ];
    }
}

Here is what I get as the output of the Encoder

{
  "data": [{
    "type": "articles",
    "id": "1",
    "attributes": {
      "title": "JSON API paints my bikeshed!"
    },
    "links": {
      "self": "http://example.com/articles/1"
    },
    "relationships": {
      "author": {
        "links": {
          "self": "http://example.com/articles/1/relationships/author",
          "related": "http://example.com/articles/1/author"
        },
        "data": { "type": "people", "id": "9" }
      }
    }
  }],
  "included": [{
    "type": "people",
    "id": "9",
    "attributes": {
      "first-name": "Dan",
      "last-name": "Gebhardt",
      "twitter": "dgeb"
    }
  }]
}

Here is the expected result:

{
  "data": [{
    "type": "articles",
    "id": "1",
    "attributes": {
      "title": "JSON API paints my bikeshed!"
    },
    "links": {
      "self": "http://example.com/articles/1"
    },
    "relationships": {
      "author": {
        "links": {
          "self": "http://example.com/articles/1/relationships/author",
          "related": "http://example.com/articles/1/author"
        },
        "data": { "type": "people", "id": "9" }
      }
    }
  }],
  "included": [{
    "type": "people",
    "id": "9",
    "attributes": {
      "first-name": "Dan",
      "last-name": "Gebhardt",
      "twitter": "dgeb"
    },
    "links": {
      "self": "http://example.com/people/9"
    }
  }]
}

@steverhoades I've found for you an example included objects with self link. Here it is.

@steverhoades and these lines add the link you want.

@neomerx Thank you for the quick response, I should have checked the tests. I'll try this and see if I can get it working.

@steverhoades np. Don't hesitate to ask.