|
23 | 23 | use Symfony\Component\Serializer\Exception\LogicException; |
24 | 24 | use Symfony\Component\Serializer\Exception\RuntimeException; |
25 | 25 | use Symfony\Component\Serializer\Exception\UnexpectedValueException; |
| 26 | +use Symfony\Component\Serializer\Mapping\ClassDiscriminatorFromClassMetadata; |
26 | 27 | use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory; |
27 | 28 | use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface; |
28 | 29 | use Symfony\Component\Serializer\Mapping\Loader\AttributeLoader; |
@@ -1061,6 +1062,23 @@ public function testPrecedenceOfAccessorMethods() |
1061 | 1062 | 'foo' => 'hasFoo', |
1062 | 1063 | ], $normalizedSwappedHasserIsser); |
1063 | 1064 | } |
| 1065 | + |
| 1066 | + public function testDiscriminatorWithAllowExtraAttributesFalse() |
| 1067 | + { |
| 1068 | + // Discriminator type property should be allowed with allow_extra_attributes=false |
| 1069 | + $classMetadataFactory = new ClassMetadataFactory(new AttributeLoader()); |
| 1070 | + $discriminator = new ClassDiscriminatorFromClassMetadata($classMetadataFactory); |
| 1071 | + $normalizer = new ObjectNormalizer($classMetadataFactory, null, null, null, $discriminator); |
| 1072 | + |
| 1073 | + $obj = $normalizer->denormalize( |
| 1074 | + ['type' => 'type_a'], |
| 1075 | + DiscriminatorDummyInterface::class, |
| 1076 | + null, |
| 1077 | + [AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => false] |
| 1078 | + ); |
| 1079 | + |
| 1080 | + $this->assertInstanceOf(DiscriminatorDummyTypeA::class, $obj); |
| 1081 | + } |
1064 | 1082 | } |
1065 | 1083 |
|
1066 | 1084 | class ProxyObjectDummy extends ObjectDummy |
@@ -1404,6 +1422,25 @@ public function isolate() |
1404 | 1422 | } |
1405 | 1423 | } |
1406 | 1424 |
|
| 1425 | +#[\Symfony\Component\Serializer\Attribute\DiscriminatorMap( |
| 1426 | + typeProperty: 'type', |
| 1427 | + mapping: [ |
| 1428 | + 'type_a' => DiscriminatorDummyTypeA::class, |
| 1429 | + 'type_b' => DiscriminatorDummyTypeB::class, |
| 1430 | + ] |
| 1431 | +)] |
| 1432 | +interface DiscriminatorDummyInterface |
| 1433 | +{ |
| 1434 | +} |
| 1435 | + |
| 1436 | +class DiscriminatorDummyTypeA implements DiscriminatorDummyInterface |
| 1437 | +{ |
| 1438 | +} |
| 1439 | + |
| 1440 | +class DiscriminatorDummyTypeB implements DiscriminatorDummyInterface |
| 1441 | +{ |
| 1442 | +} |
| 1443 | + |
1407 | 1444 | class ObjectWithPropertyAndAllAccessorMethods |
1408 | 1445 | { |
1409 | 1446 | public function __construct( |
|
0 commit comments