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
The AbstractNormalizer::instantiateObject unsets the corresponding &$data value for a constructor argument if it believes it will be set by constructor invocation so that no set is called later on. However, if constructor invocation fails as it is missing arguments, the value won't be set again at a later stage, even if the object could be instantiated partially when setting the property later on.
How to reproduce
If we have an example object
class ExampleObject
{
publicfunction__construct(publicreadonlystring$propertyMissing, publicreadonlystring$propertyGiven)
{
}
}
however it says: $propertyGiven must not be accessed before initialization as there was no attempt to set it later on after the constructor instantiation failed.
Possible Solution
Remember the properties that have to be unset if the constructor instantiation succeeds and only unset them if there are no missing parameters and the instantiation actually worked.
Additional Context
I realized this as the behaviour changed with commit c54cfbb6f3129c41ea5b26b4e3115919132ab726 as it now collects all missing arguments. Before it returned early and the $data values for later constructor arguments were not unset. So this is kind of a regression, although before it just worked "accidentially" in my use case as the first argument was already missing for my data.
The text was updated successfully, but these errors were encountered:
This PR was merged into the 5.4 branch.
Discussion
----------
[Serializer] Fix denormalize constructor arguments
| Q | A
| ------------- | ---
| Branch? | 5.4
| Bug fix? | yes
| New feature? | no
| Deprecations? | no
| Issues | Fix#52499, Fix#52422
| License | MIT
Since this PR: #51907, objects with partial constructor parameters were wrongly instantiated.
This PR fixes that issue by delegating the properties values assignment, by unsetting normalized data only when the constructor has been called properly.
This might correct #50759 as well.
Commits
-------
8f7c7ae [Serializer] Fix denormalize constructor arguments
Symfony version(s) affected
6.3.x,5.4.x
Description
The
AbstractNormalizer::instantiateObject
unsets the corresponding&$data
value for a constructor argument if it believes it will be set by constructor invocation so that noset
is called later on. However, if constructor invocation fails as it is missing arguments, the value won't be set again at a later stage, even if the object could be instantiated partially when setting the property later on.How to reproduce
If we have an example object
I would expect the following test to pass:
however it says:
$propertyGiven must not be accessed before initialization
as there was no attempt to set it later on after the constructor instantiation failed.Possible Solution
Remember the properties that have to be unset if the constructor instantiation succeeds and only unset them if there are no missing parameters and the instantiation actually worked.
Additional Context
I realized this as the behaviour changed with commit c54cfbb6f3129c41ea5b26b4e3115919132ab726 as it now collects all missing arguments. Before it returned early and the
$data
values for later constructor arguments were not unset. So this is kind of a regression, although before it just worked "accidentially" in my use case as the first argument was already missing for my data.The text was updated successfully, but these errors were encountered: