You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This problem exists only if you're using MetadataAwareNameConverter (with attributes #[SerializedName] or #[SerializedPath]). The current implementation of ConstraintViolationListNormalizer does not respect such attributes and way of name converting.
As you can see, we will exceed the maximum length of the field.
What do we expect to receive as a response:
{
"type": "https://symfony.com/errors/validation",
"title": "Validation Failed",
"status": 422,
"detail": "first_name: This value is too long. It should have 10 characters or less.",
"violations": [
{
"propertyPath": "first_name",
"title": "This value is too long. It should have 10 characters or less.",
"template": "This value is too long. It should have {{ limit }} characters or less.",
"parameters": {
"{{ value }}": "\"supercalifragilisticexpialidocious\"",
"{{ limit }}": "10"
},
"type": "urn:uuid:d94b19cc-114f-4f44-9cc4-4138e80a87b9"
}
]
}
But we will receive the following one:
{
"type": "https://symfony.com/errors/validation",
"title": "Validation Failed",
"status": 422,
"detail": "firstName: This value is too long. It should have 10 characters or less.",
"violations": [
{
"propertyPath": "firstName",
"title": "This value is too long. It should have 10 characters or less.",
"template": "This value is too long. It should have {{ limit }} characters or less.",
"parameters": {
"{{ value }}": "\"supercalifragilisticexpialidocious\"",
"{{ limit }}": "10"
},
"type": "urn:uuid:d94b19cc-114f-4f44-9cc4-4138e80a87b9"
}
]
}
As you can see, the current implementation of ConstraintViolationListNormalizer does not respect MetadataAwareNameConverter correctly and fallback to the property name.
Possible Solution
The first idea that I got is to try to pass the class name of the original object to which the property belongs (root). But I'm not sure that it will cover all possible cases.
Additional Context
No response
The text was updated successfully, but these errors were encountered:
AlexMinaev19
changed the title
[Serializer][Validator] Using Serializer attributes during constraint violation serializing[Serializer][Validator] Using Serializer attributes during constraint violation serializing
May 22, 2024
OskarStark
changed the title
[Serializer][Validator] Using Serializer attributes during constraint violation serializing
[Serializer][Validator] Using Serializer attributes during constraint violation serializing
May 23, 2024
Symfony version(s) affected
7.0
Description
This problem exists only if you're using
MetadataAwareNameConverter
(with attributes#[SerializedName]
or#[SerializedPath]
). The current implementation ofConstraintViolationListNormalizer
does not respect such attributes and way of name converting.Why it happens?
Let's take a look at
ConstraintViolationListNormalizer::normalize()
method. On the line63
(https://github.com/symfony/symfony/blob/7.0/src/Symfony/Component/Serializer/Normalizer/ConstraintViolationListNormalizer.php#L63), we try to normalize the property path according to current name converter. If we're usingCamelCaseToSnakeCaseNameConverter
everything is working fine, but withMetadataAwareNameConverter
it does not.Here we pass the following parameters to
MetadataAwareNameConverter::normalize()
method:$violation->getPropertyPath()
as reported property pathnull
as class$format
as format$context
as normalization contextThe most important parameter for us here is class. Since we don't know the class
MetadataAwareNameConverter::normalize()
fallback to another strategy of normalization (line https://github.com/symfony/symfony/blob/7.0/src/Symfony/Component/Serializer/NameConverter/MetadataAwareNameConverter.php#L47). If there is no fallback name converter available, we will return passed property name itself. This is what happens in our case (we did not configure any fallback).How to reproduce
For example, we have the following request object
Also, let's imagine we have the following controller
And we have the following configuration of the framework
Finally, we want to send the following request
As you can see, we will exceed the maximum length of the field.
What do we expect to receive as a response:
But we will receive the following one:
As you can see, the current implementation of
ConstraintViolationListNormalizer
does not respectMetadataAwareNameConverter
correctly and fallback to the property name.Possible Solution
The first idea that I got is to try to pass the class name of the original object to which the property belongs (root). But I'm not sure that it will cover all possible cases.
Additional Context
No response
The text was updated successfully, but these errors were encountered: