Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,8 @@ private function validateAndDenormalize(array $types, string $currentClass, stri
if (!$isUnionType && !$isNullable) {
throw $e;
}

$expectedTypes[Type::BUILTIN_TYPE_OBJECT === $builtinType && $class ? $class : $builtinType] = true;
} catch (ExtraAttributesException $e) {
if (!$isUnionType && !$isNullable) {
throw $e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
use Symfony\Component\Serializer\Attribute\Ignore;
use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
use Symfony\Component\Serializer\Exception\RuntimeException;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorFromClassMetadata;
Expand All @@ -41,6 +42,7 @@
use Symfony\Component\Serializer\Tests\Fixtures\Attributes\GroupDummy;
use Symfony\Component\Serializer\Tests\Fixtures\CircularReferenceDummy;
use Symfony\Component\Serializer\Tests\Fixtures\DummyPrivatePropertyWithoutGetter;
use Symfony\Component\Serializer\Tests\Fixtures\DummyWithUnion;
use Symfony\Component\Serializer\Tests\Fixtures\FormatAndContextAwareNormalizer;
use Symfony\Component\Serializer\Tests\Fixtures\OtherSerializedNameDummy;
use Symfony\Component\Serializer\Tests\Fixtures\Php74Dummy;
Expand Down Expand Up @@ -343,6 +345,22 @@ public function testConstructorWithUnknownObjectTypeHintDenormalize()
$normalizer->denormalize($data, DummyWithConstructorInexistingObject::class);
}

public function testConstructorWithNotMatchingUnionTypes()
{
$data = [
'value' => 'string',
'value2' => 'string',
];
$normalizer = new ObjectNormalizer(new ClassMetadataFactory(new AttributeLoader()), null, null, new PropertyInfoExtractor([], [new ReflectionExtractor()]));

$this->expectException(NotNormalizableValueException::class);
$this->expectExceptionMessage('The type of the "value" attribute for class "Symfony\Component\Serializer\Tests\Fixtures\DummyWithUnion" must be one of "int", "float" ("string" given).');

$normalizer->denormalize($data, DummyWithUnion::class, 'xml', [
AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => false,
]);
}

// attributes

protected function getNormalizerForAttributes(): ObjectNormalizer
Expand Down
Loading