|
25 | 25 | use Symfony\Component\Serializer\Exception\NotNormalizableValueException; |
26 | 26 | use Symfony\Component\Serializer\Exception\RuntimeException; |
27 | 27 | use Symfony\Component\Serializer\Exception\UnexpectedValueException; |
| 28 | +use Symfony\Component\Serializer\Mapping\ClassDiscriminatorFromClassMetadata; |
28 | 29 | use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory; |
29 | 30 | use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface; |
30 | 31 | use Symfony\Component\Serializer\Mapping\Loader\AttributeLoader; |
@@ -1086,6 +1087,23 @@ public function testPrecedenceOfAccessorMethods() |
1086 | 1087 | 'foo' => 'hasFoo', |
1087 | 1088 | ], $normalizedSwappedHasserIsser); |
1088 | 1089 | } |
| 1090 | + |
| 1091 | + public function testDiscriminatorWithAllowExtraAttributesFalse() |
| 1092 | + { |
| 1093 | + // Discriminator type property should be allowed with allow_extra_attributes=false |
| 1094 | + $classMetadataFactory = new ClassMetadataFactory(new AttributeLoader()); |
| 1095 | + $discriminator = new ClassDiscriminatorFromClassMetadata($classMetadataFactory); |
| 1096 | + $normalizer = new ObjectNormalizer($classMetadataFactory, null, null, null, $discriminator); |
| 1097 | + |
| 1098 | + $obj = $normalizer->denormalize( |
| 1099 | + ['type' => 'type_a'], |
| 1100 | + DiscriminatorDummyInterface::class, |
| 1101 | + null, |
| 1102 | + [AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => false] |
| 1103 | + ); |
| 1104 | + |
| 1105 | + $this->assertInstanceOf(DiscriminatorDummyTypeA::class, $obj); |
| 1106 | + } |
1089 | 1107 | } |
1090 | 1108 |
|
1091 | 1109 | class ProxyObjectDummy extends ObjectDummy |
@@ -1429,6 +1447,25 @@ public function isolate() |
1429 | 1447 | } |
1430 | 1448 | } |
1431 | 1449 |
|
| 1450 | +#[\Symfony\Component\Serializer\Attribute\DiscriminatorMap( |
| 1451 | + typeProperty: 'type', |
| 1452 | + mapping: [ |
| 1453 | + 'type_a' => DiscriminatorDummyTypeA::class, |
| 1454 | + 'type_b' => DiscriminatorDummyTypeB::class, |
| 1455 | + ] |
| 1456 | +)] |
| 1457 | +interface DiscriminatorDummyInterface |
| 1458 | +{ |
| 1459 | +} |
| 1460 | + |
| 1461 | +class DiscriminatorDummyTypeA implements DiscriminatorDummyInterface |
| 1462 | +{ |
| 1463 | +} |
| 1464 | + |
| 1465 | +class DiscriminatorDummyTypeB implements DiscriminatorDummyInterface |
| 1466 | +{ |
| 1467 | +} |
| 1468 | + |
1432 | 1469 | class ObjectWithPropertyAndAllAccessorMethods |
1433 | 1470 | { |
1434 | 1471 | public function __construct( |
|
0 commit comments