Skip to content

Commit ba2e50a

Browse files
committed
Merge branch '6.4' into 7.3
* 6.4: fix unexpected type in denormalization errors when union type used in constructor in xml
2 parents 363bd0a + 28779bb commit ba2e50a

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

Normalizer/AbstractObjectNormalizer.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,8 @@ private function validateAndDenormalizeLegacy(array $types, string $currentClass
611611
if (!$isUnionType && !$isNullable) {
612612
throw $e;
613613
}
614+
615+
$expectedTypes[LegacyType::BUILTIN_TYPE_OBJECT === $builtinType && $class ? $class : $builtinType] = true;
614616
} catch (ExtraAttributesException $e) {
615617
if (!$isUnionType && !$isNullable) {
616618
throw $e;
@@ -901,6 +903,8 @@ private function validateAndDenormalize(Type $type, string $currentClass, string
901903
if (!$type instanceof UnionType) {
902904
throw $e;
903905
}
906+
907+
$expectedTypes[TypeIdentifier::OBJECT === $typeIdentifier && $class ? $class : $typeIdentifier->value] = true;
904908
} catch (ExtraAttributesException $e) {
905909
if (!$type instanceof UnionType) {
906910
throw $e;

Tests/Normalizer/ObjectNormalizerTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
use Symfony\Component\Serializer\Tests\Fixtures\Attributes\GroupDummy;
4444
use Symfony\Component\Serializer\Tests\Fixtures\CircularReferenceDummy;
4545
use Symfony\Component\Serializer\Tests\Fixtures\DummyPrivatePropertyWithoutGetter;
46+
use Symfony\Component\Serializer\Tests\Fixtures\DummyWithUnion;
4647
use Symfony\Component\Serializer\Tests\Fixtures\OtherSerializedNameDummy;
4748
use Symfony\Component\Serializer\Tests\Fixtures\Php74Dummy;
4849
use Symfony\Component\Serializer\Tests\Fixtures\Php74DummyPrivate;
@@ -65,6 +66,7 @@
6566
use Symfony\Component\Serializer\Tests\Normalizer\Features\TypedPropertiesObject;
6667
use Symfony\Component\Serializer\Tests\Normalizer\Features\TypedPropertiesObjectWithGetters;
6768
use Symfony\Component\Serializer\Tests\Normalizer\Features\TypeEnforcementTestTrait;
69+
use Symfony\Component\TypeInfo\Type;
6870

6971
/**
7072
* @author Kévin Dunglas <dunglas@gmail.com>
@@ -346,6 +348,27 @@ public function testConstructorWithUnknownObjectTypeHintDenormalize()
346348
$normalizer->denormalize($data, DummyWithConstructorInexistingObject::class);
347349
}
348350

351+
public function testConstructorWithNotMatchingUnionTypes()
352+
{
353+
$data = [
354+
'value' => 'string',
355+
'value2' => 'string',
356+
];
357+
$normalizer = new ObjectNormalizer(new ClassMetadataFactory(new AttributeLoader()), null, null, new PropertyInfoExtractor([], [new ReflectionExtractor()]));
358+
359+
$this->expectException(NotNormalizableValueException::class);
360+
361+
if (class_exists(Type::class) && method_exists(PropertyInfoExtractor::class, 'getType')) {
362+
$this->expectExceptionMessage('The type of the "value" attribute for class "Symfony\Component\Serializer\Tests\Fixtures\DummyWithUnion" must be one of "float", "int" ("string" given).');
363+
} else {
364+
$this->expectExceptionMessage('The type of the "value" attribute for class "Symfony\Component\Serializer\Tests\Fixtures\DummyWithUnion" must be one of "int", "float" ("string" given).');
365+
}
366+
367+
$normalizer->denormalize($data, DummyWithUnion::class, 'xml', [
368+
AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => false,
369+
]);
370+
}
371+
349372
// attributes
350373

351374
protected function getNormalizerForAttributes(): ObjectNormalizer

0 commit comments

Comments
 (0)