-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
\Symfony\Component\Serializer\Exception\UnexpectedValueException provides insufficient semantics #20534
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
Comments
422 exceptions are a Drupal specificity. Symfony doesn't have any builtin mechanism to transform Serializer's exceptions in HTTP errors (by default it will throw a 500 error, like for any other exception). For instance API Platform transforms such exceptions in 400 errors. You should report this problem on the Drupal bug tracker instead of here. |
This issue was opened as a spin-off from a Drupal issue, see the discussion in https://www.drupal.org/node/2827084 which led to it being opened. We have a workaround for Drupal, but the lack of specificity in Symfony's exceptions precluded a less intrusive approach that was suggested, hence this issue. |
@dunglas More specific exceptions would allow to base the response status code on the exception being thrown instead of having to parse the exception message. See #21239 for a suggestion to improve the situation for Drupal. @wimleers @catch56 Can you confirm that such a change would ease Drupal's situation? |
This PR was merged into the 3.4 branch. Discussion ---------- [Serializer] throw more specific exceptions | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #20534 | License | MIT | Doc PR | Commits ------- aa30d04 [Serializer] throw more specific exceptions
Drupal 8's normalizers'
denormalize()
implementations throwUnexpectedValueException
to indicate unexpected values in the received data, which cannot be denormalized to corresponding PHP objects. Such exceptions should result in a 422 response:\Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException
.Sadly, the same exception is used in Symfony's encoders/decoders — it is thrown by:
\Symfony\Component\Serializer\Encoder\JsonDecode::decode()
\Symfony\Component\Serializer\Encoder\JsonEncode::encode()
\Symfony\Component\Serializer\Encoder\XmlEncoder::buildXml()
\Symfony\Component\Serializer\Encoder\XmlEncoder::decode()
… yet encoding/decoding problems should result in a 400 response:
\Symfony\Component\HttpKernel\Exception\BadRequestHttpException
.The official docs don't mention 422 responses or (de)normalization failures either: http://symfony.com/doc/current/components/serializer.html + http://symfony.com/doc/current/serializer.html.
The text was updated successfully, but these errors were encountered: