-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
docs: document 422 denormalization error handling #2286
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
Merged
+297
−0
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -648,3 +648,141 @@ If the submitted data has denormalization errors, the HTTP status code will be s | |||||
|
|
||||||
| You can also enable collecting of denormalization errors globally in the | ||||||
| [Global Resources Defaults](https://api-platform.com/docs/core/configuration/#global-resources-defaults). | ||||||
|
|
||||||
| ## Constraint-Aware 422 for Denormalization Errors | ||||||
|
|
||||||
| Starting with API Platform 4.4, type mismatches detected during input denormalization (for example, | ||||||
| the client sends `"foo"` for an `int` field, or `null` for a non-nullable property) are promoted to | ||||||
| HTTP 422 validation responses when the affected property has a matching Symfony Validator constraint. | ||||||
| When no matching constraint exists, API Platform rethrows the original serializer exception as an | ||||||
| honest HTTP 400. | ||||||
|
|
||||||
| This eliminates the need to enable `collectDenormalizationErrors` on every resource or write a | ||||||
| custom event listener solely to convert 400 serializer errors into 422 validation responses. | ||||||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. applied |
||||||
|
|
||||||
| ### How It Works | ||||||
|
|
||||||
| `DeserializeProvider` catches `NotNormalizableValueException` and `PartialDenormalizationException` | ||||||
| from the Symfony Serializer. It delegates to `ApiPlatform\Validator\DenormalizationViolationFactory`, | ||||||
| which reads the Symfony Validator metadata for the operation's resource class and applies the | ||||||
| following rule table: | ||||||
|
|
||||||
| | Serializer `currentType` | Matching constraint on the property | HTTP status | Violation code | | ||||||
| |--------------------------|-------------------------------------|-------------|----------------------------| | ||||||
| | `null` | `NotBlank` | 422 | `NotBlank::IS_BLANK_ERROR` | | ||||||
| | `null` | `NotNull` | 422 | `NotNull::IS_NULL_ERROR` | | ||||||
| | any wrong type | `Type` | 422 | `Type::INVALID_TYPE_ERROR` | | ||||||
| | any wrong type | any other constraint | 422 | `Type::INVALID_TYPE_ERROR` | | ||||||
| | any wrong type | (no constraint) | 400 | original exception rethrown| | ||||||
|
|
||||||
| In `collectDenormalizationErrors` mode (where the serializer raises `PartialDenormalizationException` | ||||||
| instead of failing on the first error), properties without any constraint still emit a generic | ||||||
| `Type::INVALID_TYPE_ERROR` violation so the 422 response surface remains consistent with prior | ||||||
| behavior. | ||||||
|
|
||||||
| Validation groups set via `Operation::getValidationContext()['groups']` are respected when looking | ||||||
| up constraints. | ||||||
|
|
||||||
| ### Example | ||||||
|
|
||||||
| ```php | ||||||
| <?php | ||||||
| // api/src/Entity/Book.php | ||||||
| namespace App\Entity; | ||||||
|
|
||||||
| use ApiPlatform\Metadata\ApiResource; | ||||||
| use Doctrine\ORM\Mapping as ORM; | ||||||
| use Symfony\Component\Validator\Constraints as Assert; | ||||||
|
|
||||||
| #[ORM\Entity] | ||||||
| #[ApiResource] | ||||||
| class Book | ||||||
| { | ||||||
| #[ORM\Id, ORM\Column, ORM\GeneratedValue] | ||||||
| private ?int $id = null; | ||||||
|
|
||||||
| #[ORM\Column] | ||||||
| #[Assert\NotBlank] | ||||||
| public string $title; | ||||||
|
|
||||||
| #[ORM\Column] | ||||||
| #[Assert\NotNull] | ||||||
| #[Assert\Type('int')] | ||||||
| public int $year; | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| Sending `null` for the `year` field: | ||||||
|
|
||||||
| ```http | ||||||
| POST /books HTTP/1.1 | ||||||
| Content-Type: application/ld+json | ||||||
|
|
||||||
| {"title": "Dune", "year": null} | ||||||
| ``` | ||||||
|
|
||||||
| Returns a 422 using the `NotNull` constraint message: | ||||||
|
|
||||||
| ```json | ||||||
| { | ||||||
| "@context": "/contexts/ConstraintViolationList", | ||||||
| "@type": "ConstraintViolationList", | ||||||
| "title": "An error occurred", | ||||||
| "description": "year: This value should not be null.", | ||||||
| "violations": [ | ||||||
| { | ||||||
| "propertyPath": "year", | ||||||
| "message": "This value should not be null.", | ||||||
| "code": "ad32d13f-c3d4-423b-909a-857b961eb720" | ||||||
| } | ||||||
| ] | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| Sending a string for the `year` field: | ||||||
|
|
||||||
| ```http | ||||||
| POST /books HTTP/1.1 | ||||||
| Content-Type: application/ld+json | ||||||
|
|
||||||
| {"title": "Dune", "year": "nineteen-sixty-five"} | ||||||
| ``` | ||||||
|
|
||||||
| Returns a 422 using the `Type` constraint message: | ||||||
|
|
||||||
| ```json | ||||||
| { | ||||||
| "@context": "/contexts/ConstraintViolationList", | ||||||
| "@type": "ConstraintViolationList", | ||||||
| "title": "An error occurred", | ||||||
| "description": "year: This value should be of type int.", | ||||||
| "violations": [ | ||||||
| { | ||||||
| "propertyPath": "year", | ||||||
| "message": "This value should be of type int.", | ||||||
| "code": "ba785a8c-82cb-4283-967c-3cf342181b40" | ||||||
| } | ||||||
| ] | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| If `year` had no validator constraint at all, both requests would receive HTTP 400 instead. | ||||||
|
|
||||||
| ### BackedEnum Properties | ||||||
|
|
||||||
| Prior to 4.4, a special case promoted `BackedEnum` denormalization failures to 422 unconditionally. | ||||||
| That implicit promotion has been replaced by the constraint-aware rule above: | ||||||
|
|
||||||
| - Enum properties annotated with `#[Assert\NotNull]`, `#[Assert\Type]`, or any other constraint | ||||||
| continue to produce 422 responses. | ||||||
| - Enum properties with **no constraints** now receive HTTP 400. | ||||||
|
|
||||||
| To preserve the 422 behavior for an unconstrained enum property, either enable Symfony Validator's | ||||||
| [auto-mapping](https://symfony.com/doc/current/validation/auto_mapping.html) on the resource class | ||||||
| (which generates an implicit `Type` constraint from the PHP type declaration) or add an explicit | ||||||
| constraint: | ||||||
|
|
||||||
| ```php | ||||||
| #[Assert\Type(Status::class)] | ||||||
| public Status $status; | ||||||
| ``` | ||||||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
applied