Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty sparse fieldset parameter returns 500 error #225

Closed
mdpoulter opened this issue Jan 25, 2023 · 7 comments
Closed

Empty sparse fieldset parameter returns 500 error #225

mdpoulter opened this issue Jan 25, 2023 · 7 comments
Labels
bug Something isn't working

Comments

@mdpoulter
Copy link

mdpoulter commented Jan 25, 2023

When providing an empty value to any of the fields query parameters, the server returns a 500 error with server error being:

LaravelJsonApi\Validation\Rules\AllowedFieldSets::notAllowed(): Argument # 2 ($fields) must be of type string, null given, called in /vendor/laravel-json-api/validation/src/Rules/AllowedFieldSets.php on line 232"

For example, if the client requests the following:

http://localhost/api/v1/employees?fields[employees]=

It certainly makes sense for an error to be returned, but I would have expected a validation error given that this is a client issue. It also ends up in our error tracking software which isn't great.

While not an ideal solution, I wondered if adding validation for each of the fields parameters (current resource and all relations) would help. Unfortunately, it still returns the same 500 error for the above request. For example:

'fields' => [
    'nullable',
    'array',
    JsonApiRule::fieldSets(),
],
'fields.employees' => 'filled',
@lindyhopchris lindyhopchris added the bug Something isn't working label Jan 25, 2023
@lindyhopchris
Copy link
Contributor

Thanks for reporting. This does indeed look like a bug, so I'll get this fixed.

@lindyhopchris
Copy link
Contributor

Out of interest, what do you expect the server to return when they've request no fields for the employees resource, as per that example?

@lindyhopchris
Copy link
Contributor

lindyhopchris commented Jan 25, 2023

Actually, have found the answer in the spec:

An empty value indicates that no fields should be returned.

So that would mean you only get the type and id - i.e. no attributes or relationships.

@mdpoulter
Copy link
Author

Ah that's interesting! Thanks for investigating 😄

@lindyhopchris
Copy link
Contributor

Tagged in 2.5.2 of this package. To update:

composer require laravel-json-api/laravel --no-update
composer up "laravel-json-api/*"

@mdpoulter
Copy link
Author

Wow, that was fast - thank you so much!

@lindyhopchris
Copy link
Contributor

Aha yeah you caught me on an evening when I was doing Laravel JSON:API admin!! 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants