8000 \Symfony\Component\Serializer\Exception\UnexpectedValueException provides insufficient semantics · Issue #20534 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

\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

Closed
wimleers opened this issue Nov 16, 2016 · 3 comments

Comments

@wimleers
Copy link

Drupal 8's normalizers' denormalize() implementations throw UnexpectedValueException 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.

@dunglas
Copy link
Member
dunglas commented Nov 17, 2016

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.

@catch56
Copy link
catch56 commented Nov 18, 2016

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.

@xabbuh
Copy link
Member
xabbuh commented Jan 11, 2017

@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?

fabpot added a commit that referenced this issue Sep 27, 2017
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
@fabpot fabpot closed this as completed Sep 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants
0