|
27 | 27 | use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
|
28 | 28 | use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
|
29 | 29 | use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
|
| 30 | +use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; |
30 | 31 | use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
|
31 | 32 | use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
32 | 33 | use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
|
|
36 | 37 | use Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummy;
|
37 | 38 | use Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummyFirstChild;
|
38 | 39 | use Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummySecondChild;
|
| 40 | +use Symfony\Component\Serializer\Tests\Fixtures\DummyFirstChildQuux; |
39 | 41 | use Symfony\Component\Serializer\Tests\Fixtures\DummySecondChildQuux;
|
40 | 42 |
|
41 | 43 | class AbstractObjectNormalizerTest extends TestCase
|
@@ -252,6 +254,61 @@ public function hasMetadataFor($value): bool
|
252 | 254 | $this->assertInstanceOf(DummySecondChildQuux::class, $normalizedData->quux);
|
253 | 255 | }
|
254 | 256 |
|
| 257 | + public function testDenormalizeWithDiscriminatorMapAndObjectToPopulateUsesCorrectClassname() |
| 258 | + { |
| 259 | + $factory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); |
| 260 | + |
| 261 | + $loaderMock = new class() implements ClassMetadataFactoryInterface { |
| 262 | + public function getMetadataFor($value): ClassMetadataInterface |
| 263 | + { |
| 264 | + if (AbstractDummy::class === $value) { |
| 265 | + return new ClassMetadata( |
| 266 | + AbstractDummy::class, |
| 267 | + new ClassDiscriminatorMapping('type', [ |
| 268 | + 'first' => AbstractDummyFirstChild::class, |
| 269 | + 'second' => AbstractDummySecondChild::class, |
| 270 | + ]) |
| 271 | + ); |
| 272 | + } |
| 273 | + |
| 274 | + throw new InvalidArgumentException(); |
| 275 | + } |
| 276 | + |
| 277 | + public function hasMetadataFor($value): bool |
| 278 | + { |
| 279 | + return AbstractDummy::class === $value; |
| 280 | + } |
| 281 | + }; |
| 282 | + |
| 283 | + $discriminatorResolver = new ClassDiscriminatorFromClassMetadata($loaderMock); |
| 284 | + $normalizer = new AbstractObjectNormalizerDummy($factory, null, new PhpDocExtractor(), $discriminatorResolver); |
| 285 | + $serializer = new Serializer([$normalizer]); |
| 286 | + $normalizer->setSerializer($serializer); |
| 287 | + |
| 288 | + $data = [ |
| 289 | + 'foo' => 'foo', |
| 290 | + 'quux' => ['value' => 'quux'], |
| 291 | + ]; |
| 292 | + |
| 293 | + $normalizedData1 = $normalizer->denormalize($data + ['bar' => 'bar'], AbstractDummy::class, 'any', [ |
| 294 | + AbstractNormalizer::OBJECT_TO_POPULATE => new AbstractDummyFirstChild('notfoo', 'notbar'), |
| 295 | + ]); |
| 296 | + $this->assertInstanceOf(AbstractDummyFirstChild::class, $normalizedData1); |
| 297 | + $this->assertSame('foo', $normalizedData1->foo); |
| 298 | + $this->assertSame('notbar', $normalizedData1->bar); |
| 299 | + $this->assertInstanceOf(DummyFirstChildQuux::class, $normalizedData1->quux); |
| 300 | + $this->assertSame('quux', $normalizedData1->quux->getValue()); |
| 301 | + |
| 302 | + $normalizedData2 = $normalizer->denormalize($data + ['baz' => 'baz'], AbstractDummy::class, 'any', [ |
| 303 | + AbstractNormalizer::OBJECT_TO_POPULATE => new AbstractDummySecondChild('notfoo', 'notbaz'), |
| 304 | + ]); |
| 305 | + $this->assertInstanceOf(AbstractDummySecondChild::class, $normalizedData2); |
| 306 | + $this->assertSame('foo', $normalizedData2->foo); |
| 307 | + $this->assertSame('baz', $normalizedData2->baz); |
| 308 | + $this->assertInstanceOf(DummySecondChildQuux::class, $normalizedData2->quux); |
| 309 | + $this->assertSame('quux', $normalizedData2->quux->getValue()); |
| 310 | + } |
| 311 | + |
255 | 312 | public function testDenormalizeWithNestedDiscriminatorMap()
|
256 | 313 | {
|
257 | 314 | $classDiscriminatorResolver = new class() implements ClassDiscriminatorResolverInterface {
|
|
0 commit comments