Skip to content

Commit 28779bb

Browse files
fix unexpected type in denormalization errors when union type used in constructor in xml
1 parent a7d76a1 commit 28779bb

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

Normalizer/AbstractObjectNormalizer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,8 @@ private function validateAndDenormalize(array $types, string $currentClass, stri
601601
if (!$isUnionType && !$isNullable) {
602602
throw $e;
603603
}
604+
605+
$expectedTypes[Type::BUILTIN_TYPE_OBJECT === $builtinType && $class ? $class : $builtinType] = true;
604606
} catch (ExtraAttributesException $e) {
605607
if (!$isUnionType && !$isNullable) {
606608
throw $e;

Tests/Normalizer/ObjectNormalizerTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
2222
use Symfony\Component\Serializer\Attribute\Ignore;
2323
use Symfony\Component\Serializer\Exception\LogicException;
24+
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
2425
use Symfony\Component\Serializer\Exception\RuntimeException;
2526
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
2627
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorFromClassMetadata;
@@ -41,6 +42,7 @@
4142
use Symfony\Component\Serializer\Tests\Fixtures\Attributes\GroupDummy;
4243
use Symfony\Component\Serializer\Tests\Fixtures\CircularReferenceDummy;
4344
use Symfony\Component\Serializer\Tests\Fixtures\DummyPrivatePropertyWithoutGetter;
45+
use Symfony\Component\Serializer\Tests\Fixtures\DummyWithUnion;
4446
use Symfony\Component\Serializer\Tests\Fixtures\FormatAndContextAwareNormalizer;
4547
use Symfony\Component\Serializer\Tests\Fixtures\OtherSerializedNameDummy;
4648
use Symfony\Component\Serializer\Tests\Fixtures\Php74Dummy;
@@ -343,6 +345,22 @@ public function testConstructorWithUnknownObjectTypeHintDenormalize()
343345
$normalizer->denormalize($data, DummyWithConstructorInexistingObject::class);
344346
}
345347

348+
public function testConstructorWithNotMatchingUnionTypes()
349+
{
350+
$data = [
351+
'value' => 'string',
352+
'value2' => 'string',
353+
];
354+
$normalizer = new ObjectNormalizer(new ClassMetadataFactory(new AttributeLoader()), null, null, new PropertyInfoExtractor([], [new ReflectionExtractor()]));
355+
356+
$this->expectException(NotNormalizableValueException::class);
357+
$this->expectExceptionMessage('The type of the "value" attribute for class "Symfony\Component\Serializer\Tests\Fixtures\DummyWithUnion" must be one of "int", "float" ("string" given).');
358+
359+
$normalizer->denormalize($data, DummyWithUnion::class, 'xml', [
360+
AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => false,
361+
]);
362+
}
363+
346364
// attributes
347365

348366
protected function getNormalizerForAttributes(): ObjectNormalizer

0 commit comments

Comments
 (0)