|
16 | 16 | use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
|
17 | 17 | use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
|
18 | 18 | use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
|
| 19 | +use Symfony\Component\Serializer\Annotation\DiscriminatorMap; |
19 | 20 | use Symfony\Component\Serializer\Exception\LogicException;
|
| 21 | +use Symfony\Component\Serializer\Mapping\ClassDiscriminatorFromClassMetadata; |
20 | 22 | use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
|
21 | 23 | use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
|
22 | 24 | use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
|
@@ -498,6 +500,27 @@ protected function getNormalizerForSkipUninitializedValues(): NormalizerInterfac
|
498 | 500 | {
|
499 | 501 | return new GetSetMethodNormalizer(new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())));
|
500 | 502 | }
|
| 503 | + |
| 504 | + public function testNormalizeWithDiscriminator() |
| 505 | + { |
| 506 | + $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); |
| 507 | + $discriminator = new ClassDiscriminatorFromClassMetadata($classMetadataFactory); |
| 508 | + $normalizer = new GetSetMethodNormalizer($classMetadataFactory, null, null, $discriminator); |
| 509 | + |
| 510 | + $this->assertSame(['type' => 'one', 'url' => 'URL_ONE'], $normalizer->normalize(new GetSetMethodDiscriminatedDummyOne())); |
| 511 | + } |
| 512 | + |
| 513 | + public function testDenormalizeWithDiscriminator() |
| 514 | + { |
| 515 | + $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); |
| 516 | + $discriminator = new ClassDiscriminatorFromClassMetadata($classMetadataFactory); |
| 517 | + $normalizer = new GetSetMethodNormalizer($classMetadataFactory, null, null, $discriminator); |
| 518 | + |
| 519 | + $denormalized = new GetSetMethodDiscriminatedDummyTwo(); |
| 520 | + $denormalized->setUrl('url'); |
| 521 | + |
| 522 | + $this->assertEquals($denormalized, $normalizer->denormalize(['type' => 'two', 'url' => 'url'], GetSetMethodDummyInterface::class)); |
| 523 | + } |
501 | 524 | }
|
502 | 525 |
|
503 | 526 | class GetSetDummy
|
@@ -762,3 +785,43 @@ public function __call($key, $value)
|
762 | 785 | throw new \RuntimeException('__call should not be called. Called with: '.$key);
|
763 | 786 | }
|
764 | 787 | }
|
| 788 | + |
| 789 | +/** |
| 790 | + * @DiscriminatorMap(typeProperty="type", mapping={ |
| 791 | + * "one" = GetSetMethodDiscriminatedDummyOne::class, |
| 792 | + * "two" = GetSetMethodDiscriminatedDummyTwo::class, |
| 793 | + * }) |
| 794 | + */ |
| 795 | +interface GetSetMethodDummyInterface |
| 796 | +{ |
| 797 | +} |
| 798 | + |
| 799 | +class GetSetMethodDiscriminatedDummyOne implements GetSetMethodDummyInterface |
| 800 | +{ |
| 801 | + private string $url = 'URL_ONE'; |
| 802 | + |
| 803 | + public function getUrl(): string |
| 804 | + { |
| 805 | + return $this->url; |
| 806 | + } |
| 807 | + |
| 808 | + public function setUrl(string $url): void |
| 809 | + { |
| 810 | + $this->url = $url; |
| 811 | + } |
| 812 | +} |
| 813 | + |
| 814 | +class GetSetMethodDiscriminatedDummyTwo implements GetSetMethodDummyInterface |
| 815 | +{ |
| 816 | + private string $url = 'URL_TWO'; |
| 817 | + |
| 818 | + public function getUrl(): string |
| 819 | + { |
| 820 | + return $this->url; |
| 821 | + } |
| 822 | + |
| 823 | + public function setUrl(string $url): void |
| 824 | + { |
| 825 | + $this->url = $url; |
| 826 | + } |
| 827 | +} |
0 commit comments