-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[AbstractObjectNormalizer][PropertyInfo] AbstractObjectNormalizer/PropertyInfoExtractor does not respect constructor type hinting. #30053
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
@karser could you have a look please? |
Hi folks! Since ReflectionExtractor takes into account the constructor types (unlike PhpDocExtractor), so you can override ReflectionExtractor definition and increase its priority. @gusarov112 Just place this workaround to your services.yaml. For me, it fixes your issue.
|
Can we patch |
It's typical solution when you has type conversion at constructor
Bad idea, because you will get huge BC risk |
I see. I'll try to patch |
@gusarov112 @sfortop Your case is a constructor with a string argument, a private property without getter/setter. |
…riority than PhpDocExtractor and ReflectionExtractor (karser) This PR was merged into the 5.2-dev branch. Discussion ---------- [PropertyInfo] ConstructorExtractor which has higher priority than PhpDocExtractor and ReflectionExtractor | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes | New feature? | yes | BC breaks? | hopefully no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #30053 | License | MIT Supersedes #30056 #30128 In short, when using PhpDocExtractor, it ignores the constructor argument type, although `argument types from the constructor are the only types that are valid for the class instantiation`. This PR adds a separate extractor - `ConstructorExtractor` which is called first (-999) and it attempts to extract the type from constructor only, either from PhpDoc or using reflection. I added `getTypesFromConstructor` to `PhpDocExtractor` and `ReflectionExtractor` - they implement `ConstructorArgumentTypeExtractorInterface` interface. `ConstructorExtractor` aggregates those extractors using compiler pass. So the flow of control looks like this: ``` PropertyInfoExtractor::getTypes: - ConstructorExtractor::getTypes - PhpDocExtractor::getTypesFromConstructor - ReflectionExtractor::getTypesFromConstructor - PhpDocExtractor::getTypes - ReflectionExtractor::getTypes ``` Commits ------- 5049e25 Added ConstructorExtractor which has higher priority than PhpDocExtractor and ReflectionExtractor
Symfony version(s) affected: v4.2.2 v4.1.11 v4.1.10 v3.4.21
Description
Commit 8741d00
\Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer::denormalizeParameter()
$this->propertyTypeExtractor->getTypes()
\Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface::getTypes()
PropertyInfoExtractor has extractors:
Last two extractors are registered here:
vendor/symfony/framework-bundle/Resources/config/property_info.xml
and here:
\Symfony\Bundle\FrameworkBundle\DependencyInjection\FrameworkExtension::registerPropertyInfoConfiguration
and are tagged as
property_info.type_extractor
with priorities PhpDocExtractor[-1001], ReflectionExtractor[-1002]The problem is that PropertyInfoExtractor does not respect constructor type hinting, but this is wrong.
Argument types from constructor are the only types that are valid for the class instantiation. And neither priority of the extractors nor other magic should affect this.
How to reproduce
Possible Solution
Respect constructor type hinting
Additional context
ErrorMessage:
The type of the "uuid" attribute for class "App\Command\DTO" must be one of "App\Command\UuidInterface" ("string" given).
Stack trace:
The text was updated successfully, but these errors were encountered: