|
15 | 15 | use PHPUnit\Framework\TestCase;
|
16 | 16 | use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
|
17 | 17 | use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
|
| 18 | +use Symfony\Component\PropertyInfo\PropertyInfoExtractor; |
18 | 19 | use Symfony\Component\Serializer\Encoder\DecoderInterface;
|
19 | 20 | use Symfony\Component\Serializer\Encoder\EncoderInterface;
|
20 | 21 | use Symfony\Component\Serializer\Encoder\JsonEncoder;
|
|
33 | 34 | use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
|
34 | 35 | use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
|
35 | 36 | use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
|
| 37 | +use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer; |
36 | 38 | use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
|
37 | 39 | use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
38 | 40 | use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
|
@@ -538,6 +540,38 @@ public function testNormalizePreserveEmptyArrayObject()
|
538 | 540 | $this->assertEquals('{"foo":{},"bar":["notempty"],"baz":{"nested":{}}}', $serializer->serialize($object, 'json', [AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS => true]));
|
539 | 541 | }
|
540 | 542 |
|
| 543 | + public function testUnionTypeDeserializable() |
| 544 | + { |
| 545 | + $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); |
| 546 | + $extractor = new PropertyInfoExtractor([], [new PhpDocExtractor(), new ReflectionExtractor()]); |
| 547 | + $serializer = new Serializer( |
| 548 | + [ |
| 549 | + new DateTimeNormalizer(), |
| 550 | + new ObjectNormalizer($classMetadataFactory, null, null, $extractor, new ClassDiscriminatorFromClassMetadata($classMetadataFactory)), |
| 551 | + ], |
| 552 | + ['json' => new JsonEncoder()] |
| 553 | + ); |
| 554 | + |
| 555 | + $actual = $serializer->deserialize('{ "changed": null }', DummyUnionType::class, 'json', [ |
| 556 | + DateTimeNormalizer::FORMAT_KEY => \DateTime::ISO8601, |
| 557 | + ]); |
| 558 | + |
| 559 | + $this->assertEquals((new DummyUnionType())->setChanged(null), $actual, 'Union type denormalization first case failed.'); |
| 560 | + |
| 561 | + $actual = $serializer->deserialize('{ "changed": "2022-03-22T16:15:05+0000" }', DummyUnionType::class, 'json', [ |
| 562 | + DateTimeNormalizer::FORMAT_KEY => \DateTime::ISO8601, |
| 563 | + ]); |
| 564 | + |
| 565 | + $expectedDateTime = \DateTime::createFromFormat(\DateTime::ISO8601, '2022-03-22T16:15:05+0000'); |
| 566 | + $this->assertEquals((new DummyUnionType())->setChanged($expectedDateTime), $actual, 'Union type denormalization second case failed.'); |
| 567 | + |
| 568 | + $actual = $serializer->deserialize('{ "changed": false }', DummyUnionType::class, 'json', [ |
| 569 | + DateTimeNormalizer::FORMAT_KEY => \DateTime::ISO8601, |
| 570 | + ]); |
| 571 | + |
| 572 | + $this->assertEquals(new DummyUnionType(), $actual, 'Union type denormalization third case failed.'); |
| 573 | + } |
| 574 | + |
541 | 575 | private function serializerWithClassDiscriminator()
|
542 | 576 | {
|
543 | 577 | $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
|
@@ -610,6 +644,26 @@ public function __construct($value)
|
610 | 644 | }
|
611 | 645 | }
|
612 | 646 |
|
| 647 | +class DummyUnionType |
| 648 | +{ |
| 649 | + /** |
| 650 | + * @var \DateTime|bool|null |
| 651 | + */ |
| 652 | + public $changed = false; |
| 653 | + |
| 654 | + /** |
| 655 | + * @param \DateTime|bool|null |
| 656 | + * |
| 657 | + * @return $this |
| 658 | + */ |
| 659 | + public function setChanged($changed): self |
| 660 | + { |
| 661 | + $this->changed = $changed; |
| 662 | + |
| 663 | + return $this; |
| 664 | + } |
| 665 | +} |
| 666 | + |
613 | 667 | interface NormalizerAwareNormalizer extends NormalizerInterface, NormalizerAwareInterface
|
614 | 668 | {
|
615 | 669 | }
|
|
0 commit comments