neomerx / json-api

Framework agnostic JSON API (jsonapi.org) implementation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

sparse fields filter relationships

efinder2 opened this issue · comments

I've started implementing the sparse fieldsets in my application. At first I was surprised how easy it was to add the functionality to my app. Without included relationships is the result as expected. Problem is that the relationships aren't added if I use sparse fieldsets. These could be accomplished by adding the type name to the fieldsets array.

According to the tests this should be working:

$aFieldSets = [
	// Attributes and relationships that should be shown
	'employees'  => ['firstName', 'lastName'],
	'subsidiaries'  => ['name'],
]
$sJson = $this->getJsonEncoder()
		->withLinks($aLinks)
		->withMeta($metaData))
		->withFieldSets($aFieldSets)
		->withIncludedPaths(['subsidiaries'])
		->encodeData($oRecords);

Only if the Fieldset is changed to this, the related data is shown.

$aFieldSets = [
	// Attributes and relationships that should be shown
	'employees'  => ['firstName', 'lastName','subsidiaries'],
	'subsidiaries'  => ['name'],
]

Where could be the problem?

The same thing you do in the tests. According to the specification https://jsonapi.org/format/#fetching-sparse-fieldsets there is no need to do that.
Here a snippet from the class FieldSetFilter:77-81

$parentType = $position->getParentType();
if ($this->hasFilter($parentType) === true) {
    return isset($this->getAllowedFields($parentType)[$position->getParentRelationship()]);
}

It checks whether the name of the relationship type is named in the required fields. To be sepc conform this method should always return true imho.

I know there were a discussion on this topic in #105 and #134. In my opinion the example in the 1.0 spec requires my interpretion:

GET /articles?include=author&fields[articles]=title,body&fields[people]=name HTTP/1.1
Accept: application/vnd.api+json

There is no reference of the author or people in the requested fields. But I assume the author should be included. Otherwise this would be nonsense

Wrong button on comment pressed 🙈