json-api icon indicating copy to clipboard operation
json-api copied to clipboard

sparse fields filter relationships

Open efinder2 opened this issue 5 years ago • 3 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?

efinder2 avatar Apr 29 '20 14:04 efinder2

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.

efinder2 avatar Apr 30 '20 06:04 efinder2

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

efinder2 avatar Apr 30 '20 06:04 efinder2

Wrong button on comment pressed 🙈

efinder2 avatar Apr 30 '20 06:04 efinder2