|
20 | 20 | use Symfony\Component\Serializer\Encoder\DecoderInterface;
|
21 | 21 | use Symfony\Component\Serializer\Encoder\EncoderInterface;
|
22 | 22 | use Symfony\Component\Serializer\Encoder\JsonEncoder;
|
| 23 | +use Symfony\Component\Serializer\Exception\ExtraAttributesException; |
23 | 24 | use Symfony\Component\Serializer\Exception\InvalidArgumentException;
|
24 | 25 | use Symfony\Component\Serializer\Exception\LogicException;
|
25 | 26 | use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
|
|
32 | 33 | use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
|
33 | 34 | use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
|
34 | 35 | use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
|
| 36 | +use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; |
35 | 37 | use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
|
36 | 38 | use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
|
37 | 39 | use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
|
@@ -751,6 +753,35 @@ public function testUnionTypeDeserializable()
|
751 | 753 | $this->assertEquals(new DummyUnionType(), $actual, 'Union type denormalization third case failed.');
|
752 | 754 | }
|
753 | 755 |
|
| 756 | + public function testUnionTypeDeserializableWithoutAllowedExtraAttributes() |
| 757 | + { |
| 758 | + $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); |
| 759 | + $extractor = new PropertyInfoExtractor([], [new PhpDocExtractor(), new ReflectionExtractor()]); |
| 760 | + $serializer = new Serializer( |
| 761 | + [ |
| 762 | + new ObjectNormalizer($classMetadataFactory, null, null, $extractor, new ClassDiscriminatorFromClassMetadata($classMetadataFactory)), |
| 763 | + ], |
| 764 | + ['json' => new JsonEncoder()] |
| 765 | + ); |
| 766 | + |
| 767 | + $actual = $serializer->deserialize('{ "v": { "a": 0 }}', DummyUnionWithAAndB::class, 'json', [ |
| 768 | + AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => false, |
| 769 | + ]); |
| 770 | + |
| 771 | + $this->assertEquals(new DummyUnionWithAAndB(new DummyATypeForUnion()), $actual); |
| 772 | + |
| 773 | + $actual = $serializer->deserialize('{ "v": { "b": 1 }}', DummyUnionWithAAndB::class, 'json', [ |
| 774 | + AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => false, |
| 775 | + ]); |
| 776 | + |
| 777 | + $this->assertEquals(new DummyUnionWithAAndB(new DummyBTypeForUnion()), $actual); |
| 778 | + |
| 779 | + $this->expectException(ExtraAttributesException::class); |
| 780 | + $serializer->deserialize('{ "v": { "b": 1, "c": "i am not allowed" }}', DummyUnionWithAAndB::class, 'json', [ |
| 781 | + AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => false, |
| 782 | + ]); |
| 783 | + } |
| 784 | + |
754 | 785 | /**
|
755 | 786 | * @requires PHP 8.2
|
756 | 787 | */
|
@@ -1221,6 +1252,30 @@ public function setChanged($changed): self
|
1221 | 1252 | }
|
1222 | 1253 | }
|
1223 | 1254 |
|
| 1255 | +class DummyATypeForUnion |
| 1256 | +{ |
| 1257 | + public $a = 0; |
| 1258 | +} |
| 1259 | + |
| 1260 | +class DummyBTypeForUnion |
| 1261 | +{ |
| 1262 | + public $b = 1; |
| 1263 | +} |
| 1264 | + |
| 1265 | +class DummyUnionWithAAndB |
| 1266 | +{ |
| 1267 | + /** @var DummyATypeForUnion|DummyBTypeForUnion */ |
| 1268 | + public $v; |
| 1269 | + |
| 1270 | + /** |
| 1271 | + * @param DummyATypeForUnion|DummyBTypeForUnion $v |
| 1272 | + */ |
| 1273 | + public function __construct($v) |
| 1274 | + { |
| 1275 | + $this->v = $v; |
| 1276 | + } |
| 1277 | +} |
| 1278 | + |
1224 | 1279 | class Baz
|
1225 | 1280 | {
|
1226 | 1281 | public $list;
|
|
0 commit comments