[Serializer] Improve nested payload validation for #[MapRequestPayload]
using a new serialization context
#53250
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.
Hello everyone,
When validating a nested object using MapRequestPayload, and given payload schema doesn't respect the DTO provided, Symfony serializer throws
PartialDenormalizationException
.Unfortunately the
MapRequestPayloadResolver
doesn't have enough information about the Dto structure to be able to provide a more detailed error, Instead symfony throws the errors "This value should be of type unknown."This unknown type is originated from the
AbstractNormalizer
in the serializer component, which injects this value to NotNormalizableValueException class.I propose to add new Serialization context
AbstractNormalizer::USE_CLASS_AS_DEFAULT_EXPECTED_TYPE
,which allows us to retrieve the class as the expected type for Our DTO if needed.
Example when using the context AbstractNormalizer::USE_CLASS_AS_DEFAULT_EXPECTED_TYPE:

I honestly do not know the reason behind using unkown instead of the FQCN. I find adding a new context more safe, and certainly Backward compatible to support both usages.
Usage example in a controller:
https://github.com/mmouih/apps-demo/blob/19f9edfa4361567a4b592a32041ead327fb3b898/src/Controller/HomeController.php#L22,L28
I included tests for the new feature:
https://github.com/mmouih/symfony/blob/05ef92028cb75c556ebbf2857617bdf40b8f1643/src/Symfony/Component/HttpKernel/Tests/Controller/ArgumentResolver/RequestPayloadValueResolverTest.php#L288C21-L288C94
Best regards.