Closed timyourivh closed 2 years ago
The docs state this will be the case: https://github.com/laravel-json-api/docs/blob/eb296a3efa759401eb9924e4136f6659f3f1f8f6/2.0/schemas/filters.md?plain=1#L842
Thus, I wouldn't consider this a bug, but perhaps a discussion could be had to express why this is the case.
Personally, I feel like the singular filters feature will typically add complexity to the client consuming the api (needing to know which situations will lead to an array vs. object/null response), and I'd rather it be consistent 100% of the time. If you need a singular/null response, you should hit the resource's show route with the route key. Otherwise, if you're hitting the index (or relation index) routes, you'll always get a collection response. I actually feel not doing this goes against the json:api specification, but perhaps that's just me.
I'll just stick with if (!!data[0]) { ... }
then.
Hi! yes this is intentionally, because the JSON:API spec defines that a has-many relation will be an array of resources. Which is why I designed it to ignore the singular filters on has-many relationships.
For the index route of a resource, the spec isn't restrictive over whether data
is an array or a resource, which is why the singular resources are an option for developers to use there.
@lindyhopchris Is there a way to enable this behavior anyway? Because I would prefer to have a singular response when defined and would make my client code look cleaner.
@timyourivh at the moment there's no way to enable it. I'm dubious whether it's compliant with the spec.
Reading the spec, it definitely isn't allowed for the relationship endpoint e.g. /api/v1/posts/123/relationships/comments
. It's not allowed because the spec says the response must be a relationship linkage which is defined as an array of identifiers for a to-many relationship.
The spec is unclear whether it's allowed for the related resources endpoint, e.g. /api/v1/posts/123/comments
.
When I have 2 entities, for example:
group
andmember
:Group
```php public function fields(): array { return [ ID::make(), Str::make('name'), Str::make('slug'), DateTime::make('created_at')->sortable()->readOnly(), DateTime::make('updated_at')->sortable()->readOnly(), // Relations HasMany::make('mambers')->canCount(), ]; } public function filters(): array { return [ WhereIdIn::make($this), Where::make('slug')->singular(), ]; } ```Member
```php public function fields(): array { return [ ID::make()->uuid(), Str::make('name'), Str::make('identifier'), Str::make('data'), DateTime::make('created_at')->sortable()->readOnly(), DateTime::make('updated_at')->sortable()->readOnly(), // Relations BelongsTo::make('group') ]; } public function filters(): array { return [ WhereIdIn::make($this), Where::make('identifier')->singular(), ]; } ```If I want to query a member of a group by it's unique and singular
identifier
I still get an array as if it wasn't singular.To elaborate:
Works fine:
Result
Doesn't return singular
Returns
I've also tried defining the filter like this:
Which returns the same as above