-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] deserialization_path missing in custom DenormalizerInterface upon using object's constructor #44925
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
The same problem, when I use CamelCaseToSnakeCaseNameConverter. deserialization_path missing the full path when child attribute has name in snake_case, but property of object in camelCase. symfony/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php Lines 258 to 266 in 22fd05a
I think it will be ok if deserialization_path will be built before checking metadata. private function getAttributeDenormalizationContext(string $class, string $attribute, array $context): array
{
$context['deserialization_path'] = ($context['deserialization_path'] ?? false) ? $context['deserialization_path'].'.'.$attribute : $attribute;
if (null === $metadata = $this->getAttributeMetadata($class, $attribute)) {
return $context;
}
return array_merge($context, $metadata->getDenormalizationContextForGroups($this->getGroups($context)));
} |
Hey, thanks for your report! |
I'm experiencing related issue with attribute
In this example, the context data is not used during denormalization process. I think that both my issue and the issue in this ticket could be solved by adding this one line: https://github.com/symfony/serializer/blob/6.1/Normalizer/AbstractObjectNormalizer.php#L556-L559 |
Hey, thanks for your report! |
Its still relevant, constructor and serializer do not go in pair. |
Still relevant. What are the possible solutions? |
In Symfony 6.3, the issue doesn't exist because it has been cover by #46680, but I'll a little fix for 5.4 |
This PR was merged into the 5.4 branch. Discussion ---------- [Serializer] Fix constructor deserialization path | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #44925 | License | MIT This fix doesn't have to be upmerged because it already has been covered by #46680. Commits ------- 272bc28 [Serializer] Fix constructor deserialization path
…ctor (mtarld) This PR was merged into the 5.4 branch. Discussion ---------- [Serializer] Fix deserialization_path missing using contructor | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprec 5E84 ations? | no | Issues | Fix #44925 #52683 | License | MIT While trying to fix #44925, I used a wrong approach (#52683), and therefore introduced a bug. This PR fixes it and solve the previous issue in a better way. Commits ------- 8a33f53 [Serializer] Fix deserialization_path missing using contructor
Symfony version(s) affected
5.4 and above
Description
In denormalizing to object by constructor, symfony serializer is not sending
deserialization_path
in $context.In AbstractObjectNormalizer there is foreach which goes through all attributes and creating context
(symfony/serializer/Normalizer/AbstractObjectNormalizer.php:374)
And it works fine when we want to denormalize by properties etc.
Problem occurs if object has only constructor available, then Serializer fires AbstractNormalizer.
In AbstractNormalizer there is foreach which goes through construct arguments
(symfony/serializer/Normalizer/AbstractNormalizer.php:362)
key
would be equivalent toattribute
from AbstractObjectNormalizer, hence the question why not addkey
to context asdeserialization_path
as construct arguments needs to be same as class attributes.When you want to use Collecting Type Errors While Denormalizing path is empty, so you cannot return info which property had incorrect type.
How to reproduce
To reproduce you will need serializer, annotations and property access
example.php:
Above example will output
The type must be one of "unknown" ("array" given). in: "" <- no path and expected type is unknown
Possible Solution
For the path in
\Symfony\Component\Serializer\Normalizer\AbstractNormalizer::instantiateObject
add at the beginning of constructor arguments foreach or same context build as in AbstractObjectNormalizer:As for the "unknown" I have not looked into it.
Additional Context
I use DTOs in my project which have custom denormalizers which shows same issue.
Also I have noticed issue when collecting errors that despite mismatch of constructor arguments Serializer Component will try to create object resulting in
\Symfony\Component\Serializer\Exception\ExceptionInterface
when using custom Denormalizer which throwsNotNormalizableValueException
indenormalize
method and I think it is a bit odd.The text was updated successfully, but these errors were encountered: