Skip to content

Encoder might skip intermediate resources when fieldset filters are applied #98

@hglattergotz

Description

@hglattergotz

At the application level a developer is presented with the request that might look like this:

GET /sites/1?include=posts.comments,comments.author&fields[comments]=body HTTP/1.1
Accept: application/vnd.api+json

From the test example EncodeIncludedObjectsTest we can see that the include and fields parameters need to be combined (in this example comments):

    public function testEncodeWithRecursiveIncludedObjects()
    {
        $this->author->{Author::LINK_COMMENTS} = $this->comments;

        $actual = Encoder::instance([
            Author::class  => AuthorSchema::class,
            Comment::class => CommentSchema::class,
            Post::class    => PostSchema::class,
            Site::class    => SiteSchema::class,
        ], $this->encoderOptions)->encodeData($this->site, new EncodingParameters(
            // include only this relation (according to the spec intermediate will be included as well)
            [Site::LINK_POSTS . '.' . Post::LINK_COMMENTS],
            // include only these attributes and links
            [
                'comments' => [Comment::ATTRIBUTE_BODY, Comment::LINK_AUTHOR],
                'posts'    => [Post::LINK_COMMENTS],
                'sites'    => [Site::LINK_POSTS],
            ]
        ));
        :
        :
    }

Given that the url will contain the includes and fields in separate parameters, it is up to the application level to correctly parse posts.comments,comments.author and figure out to create the following for the $includePaths:

    [Site::LINK_POSTS . '.' . Post::LINK_COMMENTS], // posts.comments

and

    'comments' => [Comment::ATTRIBUTE_BODY, Comment::LINK_AUTHOR /*author*/],

for $fieldSets.

Would it not be better to put this logic in the library?

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions