|
19 | 19 | use Symfony\Component\Serializer\Encoder\DecoderInterface;
|
20 | 20 | use Symfony\Component\Serializer\Encoder\EncoderInterface;
|
21 | 21 | use Symfony\Component\Serializer\Encoder\JsonEncoder;
|
| 22 | +use Symfony\Component\Serializer\Exception\ExtraAttributesException; |
22 | 23 | use Symfony\Component\Serializer\Exception\InvalidArgumentException;
|
23 | 24 | use Symfony\Component\Serializer\Exception\LogicException;
|
24 | 25 | use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
|
|
31 | 32 | use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
|
32 | 33 | use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
|
33 | 34 | use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
|
| 35 | +use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; |
34 | 36 | use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
|
35 | 37 | use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
|
36 | 38 | use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
|
@@ -573,6 +575,35 @@ public function testUnionTypeDeserializable()
|
573 | 575 | $this->assertEquals(new DummyUnionType(), $actual, 'Union type denormalization third case failed.');
|
574 | 576 | }
|
575 | 577 |
|
| 578 | + public function testUnionTypeDeserializableWithoutAllowedExtraAttributes() |
| 579 | + { |
| 580 | + $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); |
| 581 | + $extractor = new PropertyInfoExtractor([], [new PhpDocExtractor(), new ReflectionExtractor()]); |
| 582 | + $serializer = new Serializer( |
| 583 | + [ |
| 584 | + new ObjectNormalizer($classMetadataFactory, null, null, $extractor, new ClassDiscriminatorFromClassMetadata($classMetadataFactory)), |
| 585 | + ], |
| 586 | + ['json' => new JsonEncoder()] |
| 587 | + ); |
| 588 | + |
| 589 | + $actual = $serializer->deserialize('{ "v": { "a": 0 }}', DummyUnionWithAAndB::class, 'json', [ |
| 590 | + AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => false, |
| 591 | + ]); |
| 592 | + |
| 593 | + $this->assertEquals(new DummyUnionWithAAndB(new DummyATypeForUnion()), $actual); |
| 594 | + |
| 595 | + $actual = $serializer->deserialize('{ "v": { "b": 1 }}', DummyUnionWithAAndB::class, 'json', [ |
| 596 | + AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => false, |
| 597 | + ]); |
| 598 | + |
| 599 | + $this->assertEquals(new DummyUnionWithAAndB(new DummyBTypeForUnion()), $actual); |
| 600 | + |
| 601 | + $this->expectException(ExtraAttributesException::class); |
| 602 | + $serializer->deserialize('{ "v": { "b": 1, "c": "i am not allowed" }}', DummyUnionWithAAndB::class, 'json', [ |
| 603 | + AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => false, |
| 604 | + ]); |
| 605 | + } |
| 606 | + |
576 | 607 | /**
|
577 | 608 | * @requires PHP 8.2
|
578 | 609 | */
|
@@ -678,6 +709,30 @@ public function setChanged($changed): self
|
678 | 709 | }
|
679 | 710 | }
|
680 | 711 |
|
| 712 | +class DummyATypeForUnion |
| 713 | +{ |
| 714 | + public $a = 0; |
| 715 | +} |
| 716 | + |
| 717 | +class DummyBTypeForUnion |
| 718 | +{ |
| 719 | + public $b = 1; |
| 720 | +} |
| 721 | + |
| 722 | +class DummyUnionWithAAndB |
| 723 | +{ |
| 724 | + /** @var DummyATypeForUnion|DummyBTypeForUnion */ |
| 725 | + public $v; |
| 726 | + |
| 727 | + /** |
| 728 | + * @param DummyATypeForUnion|DummyBTypeForUnion $v |
| 729 | + */ |
| 730 | + public function __construct($v) |
| 731 | + { |
| 732 | + $this->v = $v; |
| 733 | + } |
| 734 | +} |
| 735 | + |
681 | 736 | interface NormalizerAwareNormalizer extends NormalizerInterface, NormalizerAwareInterface
|
682 | 737 | {
|
683 | 738 | }
|
|
0 commit comments