[ValidationException] Allow customization of validation error status code#3808
Merged
soyuka merged 6 commits intoNov 8, 2020
Merged
[ValidationException] Allow customization of validation error status code#3808soyuka merged 6 commits into
soyuka merged 6 commits into
Conversation
dunglas
approved these changes
Nov 2, 2020
Contributor
Author
Member
|
I'll handle the rest I though I just fixed the missing tests in #3818 :) |
Member
|
Thanks @ahmed-bhs ! |
Contributor
|
Quick question: Should this not have been a BC break? People expecting 400 responses now get 422 on validation errors - may be worth marking as BC in the changelog? |
Contributor
It does, so it should :-) |
Contributor
|
Hello, |
Contributor
Author
|
@odoucet you can keep the old http code if you dont want to update your unit tests, all you need is to update the exception status code: api_platform:
exception_to_status:
ApiPlatform\Core\Bridge\Symfony\Validator\Exception\ValidationException: !php/const Symfony\Component\HttpFoundation\Response::HTTP_BAD_REQUEST |
soyuka
added a commit
that referenced
this pull request
Feb 17, 2021
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In general, I think it's a good practice to use 400 Bad Request http status code when the request body is malformed (i.e. incorrect syntax in the payload or invalid parameters in the URL) and use 422 Unprocessable Entity when the query is semantically incorrect.
According to the RFC https://tools.ietf.org/html/rfc4918#section-11.2 the code 422 seems to me to be much more appropriate and specific in a case of data validation failure.
Cause the server understands the content type of the request entity and the syntax of the request entity is correct but was unable to process the data because its semantically erroneous.
The idea, is to give the hand to configure the status code for the ValidationException under
exception_to_statussection, just like this :Any way, just let me know if this requires to add some unit tests or some documentation, (if it'is an interesting idea).
Best regards,