|
34 | 34 | use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
|
35 | 35 | use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
|
36 | 36 | use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
|
| 37 | +use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; |
37 | 38 | use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
|
38 | 39 | use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
|
39 | 40 | use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
|
44 | 45 | use Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummy;
|
45 | 46 | use Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummyFirstChild;
|
46 | 47 | use Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummySecondChild;
|
| 48 | +use Symfony\Component\Serializer\Tests\Fixtures\DummyFirstChildQuux; |
47 | 49 | use Symfony\Component\Serializer\Tests\Fixtures\DummySecondChildQuux;
|
48 | 50 | use Symfony\Component\Serializer\Tests\Normalizer\Features\ObjectDummyWithContextAttribute;
|
49 | 51 |
|
@@ -480,6 +482,61 @@ public function hasMetadataFor($value): bool
|
480 | 482 | $this->assertInstanceOf(DummySecondChildQuux::class, $normalizedData->quux);
|
481 | 483 | }
|
482 | 484 |
|
| 485 | + public function testDenormalizeWithDiscriminatorMapAndObjectToPopulateUsesCorrectClassname() |
| 486 | + { |
| 487 | + $factory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); |
| 488 | + |
| 489 | + $loaderMock = new class() implements ClassMetadataFactoryInterface { |
| 490 | + public function getMetadataFor($value): ClassMetadataInterface |
| 491 | + { |
| 492 | + if (AbstractDummy::class === $value) { |
| 493 | + return new ClassMetadata( |
| 494 | + AbstractDummy::class, |
| 495 | + new ClassDiscriminatorMapping('type', [ |
| 496 | + 'first' => AbstractDummyFirstChild::class, |
| 497 | + 'second' => AbstractDummySecondChild::class, |
| 498 | + ]) |
| 499 | + ); |
| 500 | + } |
| 501 | + |
| 502 | + throw new InvalidArgumentException(); |
| 503 | + } |
| 504 | + |
| 505 | + public function hasMetadataFor($value): bool |
| 506 | + { |
| 507 | + return AbstractDummy::class === $value; |
| 508 | + } |
| 509 | + }; |
| 510 | + |
| 511 | + $discriminatorResolver = new ClassDiscriminatorFromClassMetadata($loaderMock); |
| 512 | + $normalizer = new AbstractObjectNormalizerDummy($factory, null, new PhpDocExtractor(), $discriminatorResolver); |
| 513 | + $serializer = new Serializer([$normalizer]); |
| 514 | + $normalizer->setSerializer($serializer); |
| 515 | + |
| 516 | + $data = [ |
| 517 | + 'foo' => 'foo', |
| 518 | + 'quux' => ['value' => 'quux'], |
| 519 | + ]; |
| 520 | + |
| 521 | + $normalizedData1 = $normalizer->denormalize($data + ['bar' => 'bar'], AbstractDummy::class, 'any', [ |
| 522 | + AbstractNormalizer::OBJECT_TO_POPULATE => new AbstractDummyFirstChild('notfoo', 'notbar'), |
| 523 | + ]); |
| 524 | + $this->assertInstanceOf(AbstractDummyFirstChild::class, $normalizedData1); |
| 525 | + $this->assertSame('foo', $normalizedData1->foo); |
| 526 | + $this->assertSame('notbar', $normalizedData1->bar); |
| 527 | + $this->assertInstanceOf(DummyFirstChildQuux::class, $normalizedData1->quux); |
| 528 | + $this->assertSame('quux', $normalizedData1->quux->getValue()); |
| 529 | + |
| 530 | + $normalizedData2 = $normalizer->denormalize($data + ['baz' => 'baz'], AbstractDummy::class, 'any', [ |
| 531 | + AbstractNormalizer::OBJECT_TO_POPULATE => new AbstractDummySecondChild('notfoo', 'notbaz'), |
| 532 | + ]); |
| 533 | + $this->assertInstanceOf(AbstractDummySecondChild::class, $normalizedData2); |
| 534 | + $this->assertSame('foo', $normalizedData2->foo); |
| 535 | + $this->assertSame('baz', $normalizedData2->baz); |
| 536 | + $this->assertInstanceOf(DummySecondChildQuux::class, $normalizedData2->quux); |
| 537 | + $this->assertSame('quux', $normalizedData2->quux->getValue()); |
| 538 | + } |
| 539 | + |
483 | 540 | public function testDenormalizeWithNestedDiscriminatorMap()
|
484 | 541 | {
|
485 | 542 | $classDiscriminatorResolver = new class() implements ClassDiscriminatorResolverInterface {
|
|
0 commit comments