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
bug #50193 [Serializer] Fix SerializedPath not working with constructor arguments (HypeMC)
This PR was merged into the 6.2 branch.
Discussion
----------
[Serializer] Fix `SerializedPath` not working with constructor arguments
| Q | A
| ------------- | ---
| Branch? | 6.2
| Bug fix? | yes
| New feature? | no
| Deprecations? | no
| Tickets | -
| License | MIT
| Doc PR | -
Currently the `#[SerializedPath]` attribute doesn't work with constructor arguments:
```php
class Test
{
public function __construct(
#[SerializedPath('[foo][bar]')] public string $bar,
) {
}
}
$serializer->denormalize(['foo' => ['bar' => 'something']], Test::class);
```
```
In AbstractNormalizer.php line 384:
Cannot create an instance of "Test" from serialized data because its constructor requires parameter "bar" to be present.
```
Commits
-------
240c031 [Serializer] Fix SerializedPath not working with constructor arguments
if ($this->classDiscriminatorResolver && $mapping = $this->classDiscriminatorResolver->getMappingForClass($class)) {
230
-
if (!isset($data[$mapping->getTypeProperty()])) {
231
-
throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('Type property "%s" not found for the abstract object "%s".', $mapping->getTypeProperty(), $class), null, ['string'], isset($context['deserialization_path']) ? $context['deserialization_path'].'.'.$mapping->getTypeProperty() : $mapping->getTypeProperty(), false);
232
-
}
233
-
234
-
$type = $data[$mapping->getTypeProperty()];
235
-
if (null === ($mappedClass = $mapping->getClassForType($type))) {
236
-
throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('The type "%s" is not a valid value.', $type), $type, ['string'], isset($context['deserialization_path']) ? $context['deserialization_path'].'.'.$mapping->getTypeProperty() : $mapping->getTypeProperty(), true);
237
-
}
238
-
239
-
if ($mappedClass !== $class) {
240
-
return$this->instantiateObject($data, $mappedClass, $context, new \ReflectionClass($mappedClass), $allowedAttributes, $format);
241
-
}
229
+
if ($class !== $mappedClass = $this->getMappedClass($data, $class, $context)) {
230
+
return$this->instantiateObject($data, $mappedClass, $context, new \ReflectionClass($mappedClass), $allowedAttributes, $format);
if (!$mapping = $this->classDiscriminatorResolver?->getMappingForClass($class)) {
784
+
return$class;
785
+
}
786
+
787
+
if (null === $type = $data[$mapping->getTypeProperty()] ?? null) {
788
+
throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('Type property "%s" not found for the abstract object "%s".', $mapping->getTypeProperty(), $class), null, ['string'], isset($context['deserialization_path']) ? $context['deserialization_path'].'.'.$mapping->getTypeProperty() : $mapping->getTypeProperty(), false);
789
+
}
790
+
791
+
if (null === $mappedClass = $mapping->getClassForType($type)) {
792
+
throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('The type "%s" is not a valid value.', $type), $type, ['string'], isset($context['deserialization_path']) ? $context['deserialization_path'].'.'.$mapping->getTypeProperty() : $mapping->getTypeProperty(), true);
0 commit comments