From fbfef8965fe20a27d940555b28461a239289365f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Ols=CC=8Cavsky=CC=81?= Date: Fri, 20 May 2022 17:21:02 +0200 Subject: [PATCH 1/2] [Serializer] Allow (de)normalization of empty objects in PropertyNormalizer and GetSetMethodNormalizer --- .../Normalizer/GetSetMethodNormalizer.php | 24 +++++++++++++ .../Normalizer/PropertyNormalizer.php | 10 +++++- .../Normalizer/GetSetMethodNormalizerTest.php | 34 +++++++++++++++++++ .../Normalizer/PropertyNormalizerTest.php | 30 ++++++++++++++++ 4 files changed, 97 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php index edbf08e296962..61d835269ec04 100644 --- a/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php @@ -11,6 +11,11 @@ namespace Symfony\Component\Serializer\Normalizer; +use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface; +use Symfony\Component\Serializer\Mapping\ClassDiscriminatorResolverInterface; +use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface; +use Symfony\Component\Serializer\NameConverter\NameConverterInterface; + /** * Converts between objects with getter and setter methods and arrays. * @@ -36,6 +41,21 @@ class GetSetMethodNormalizer extends AbstractObjectNormalizer { private static $setterAccessibleCache = []; + private $allowNormalizationOfObjectsWithoutAnyGetters; + + public function __construct( + ClassMetadataFactoryInterface $classMetadataFactory = null, + NameConverterInterface $nameConverter = null, + PropertyTypeExtractorInterface $propertyTypeExtractor = null, + ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null, + callable $objectClassResolver = null, + array $defaultContext = [], + bool $allowNormalizationOfObjectsWithoutAnyGetters = false, + ) { + parent::__construct($classMetadataFactory, $nameConverter, $propertyTypeExtractor, $classDiscriminatorResolver, $objectClassResolver, $defaultContext); + $this->allowNormalizationOfObjectsWithoutAnyGetters = $allowNormalizationOfObjectsWithoutAnyGetters; + } + /** * {@inheritdoc} * @@ -69,6 +89,10 @@ public function hasCacheableSupportsMethod(): bool */ private function supports(string $class): bool { + if ($this->allowNormalizationOfObjectsWithoutAnyGetters) { + return true; + } + $class = new \ReflectionClass($class); $methods = $class->getMethods(\ReflectionMethod::IS_PUBLIC); foreach ($methods as $method) { diff --git a/src/Symfony/Component/Serializer/Normalizer/PropertyNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/PropertyNormalizer.php index bd537a1b1d44f..29faba8831bbb 100644 --- a/src/Symfony/Component/Serializer/Normalizer/PropertyNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/PropertyNormalizer.php @@ -45,13 +45,17 @@ class PropertyNormalizer extends AbstractObjectNormalizer */ public const NORMALIZE_VISIBILITY = 'normalize_visibility'; - public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null, ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null, callable $objectClassResolver = null, array $defaultContext = []) + private $allowNormalizationOfObjectsWithoutAnyProperties; + + public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null, ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null, callable $objectClassResolver = null, array $defaultContext = [], bool $allowNormalizationOfObjectsWithoutAnyProperties = false) { parent::__construct($classMetadataFactory, $nameConverter, $propertyTypeExtractor, $classDiscriminatorResolver, $objectClassResolver, $defaultContext); if (!isset($this->defaultContext[self::NORMALIZE_VISIBILITY])) { $this->defaultContext[self::NORMALIZE_VISIBILITY] = self::NORMALIZE_PUBLIC | self::NORMALIZE_PROTECTED | self::NORMALIZE_PRIVATE; } + + $this->allowNormalizationOfObjectsWithoutAnyProperties = $allowNormalizationOfObjectsWithoutAnyProperties; } /** @@ -87,6 +91,10 @@ public function hasCacheableSupportsMethod(): bool */ private function supports(string $class): bool { + if ($this->allowNormalizationOfObjectsWithoutAnyProperties) { + return true; + } + $class = new \ReflectionClass($class); // We look for at least one non-static property diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php index 6fd430bb47a43..efc9131c58aab 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php @@ -357,6 +357,36 @@ public function testRejectInvalidKey() $this->markTestSkipped('This test makes no sense with the GetSetMethodNormalizer'); } + protected function getNormalizerAllowingObjectsWithoutGetters(): GetSetMethodNormalizer + { + return new GetSetMethodNormalizer(null, null, null, null, null, [], true); + } + + public function testNormalizeObjectWithoutAnyProperties() + { + $normalizer = $this->getNormalizerAllowingObjectsWithoutGetters(); + $obj = new EmptyObjectDummy(); + + $this->assertTrue($normalizer->supportsNormalization($obj)); + + $this->assertEquals( + [], + $normalizer->normalize($obj), + ); + } + + public function testDenormalizeObjectWithoutAnyProperties() + { + $normalizer = $this->getNormalizerAllowingObjectsWithoutGetters(); + $obj = new EmptyObjectDummy(); + + $this->assertTrue($normalizer->supportsDenormalization($obj, \get_class($obj))); + $this->assertEquals( + $obj, + $normalizer->denormalize([], \get_class($obj)), + ); + } + protected function getNormalizerForIgnoredAttributes(): GetSetMethodNormalizer { $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); @@ -722,3 +752,7 @@ public function hasFoo() return $this->foo; } } + +class EmptyObjectDummy +{ +} diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/PropertyNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/PropertyNormalizerTest.php index 2b9a111ed1e27..bdcfde9dad71a 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/PropertyNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/PropertyNormalizerTest.php @@ -294,6 +294,36 @@ protected function getDenormalizerForGroups(): PropertyNormalizer return new PropertyNormalizer($classMetadataFactory); } + protected function getNormalizerAllowingObjectsWithoutProperties(): PropertyNormalizer + { + return new PropertyNormalizer(null, null, null, null, null, [], true); + } + + public function testNormalizeObjectWithoutAnyProperties() + { + $normalizer = $this->getNormalizerAllowingObjectsWithoutProperties(); + $obj = new StaticPropertyDummy(); + + $this->assertTrue($normalizer->supportsNormalization($obj)); + + $this->assertEquals( + [], + $normalizer->normalize($obj), + ); + } + + public function testDenormalizeObjectWithoutAnyProperties() + { + $normalizer = $this->getNormalizerAllowingObjectsWithoutProperties(); + $obj = new StaticPropertyDummy(); + + $this->assertTrue($normalizer->supportsDenormalization($obj, \get_class($obj))); + $this->assertEquals( + $obj, + $normalizer->denormalize([], \get_class($obj)), + ); + } + public function testGroupsNormalizeWithNameConverter() { $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); From 9de526dcfb9e0a1084391967c0d0678acbe5f64d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Ols=CC=8Cavsky=CC=81?= Date: Mon, 23 May 2022 19:28:34 +0200 Subject: [PATCH 2/2] Add deprecation notice Update changelog --- .../Twig/Tests/Mime/TemplatedEmailTest.php | 2 +- .../Resources/config/serializer.php | 1 + .../Component/Mime/Tests/EmailTest.php | 2 +- .../Component/Mime/Tests/MessageTest.php | 2 +- src/Symfony/Component/Serializer/CHANGELOG.md | 2 + .../Normalizer/GetSetMethodNormalizer.php | 4 ++ .../Normalizer/PropertyNormalizer.php | 4 ++ .../Normalizer/AbstractNormalizerTest.php | 6 +-- .../Normalizer/GetSetMethodNormalizerTest.php | 51 ++++++++----------- .../Normalizer/PropertyNormalizerTest.php | 51 ++++++++----------- .../Serializer/Tests/SerializerTest.php | 48 ++++++++--------- 11 files changed, 85 insertions(+), 88 deletions(-) diff --git a/src/Symfony/Bridge/Twig/Tests/Mime/TemplatedEmailTest.php b/src/Symfony/Bridge/Twig/Tests/Mime/TemplatedEmailTest.php index 77548fb119626..d7baf43bc9672 100644 --- a/src/Symfony/Bridge/Twig/Tests/Mime/TemplatedEmailTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Mime/TemplatedEmailTest.php @@ -102,7 +102,7 @@ public function testSymfonySerialize() EOF; $extractor = new PhpDocExtractor(); - $propertyNormalizer = new PropertyNormalizer(null, null, $extractor); + $propertyNormalizer = new PropertyNormalizer(null, null, $extractor, null, null, [], true); $serializer = new Serializer([ new ArrayDenormalizer(), new MimeMessageNormalizer($propertyNormalizer), diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.php b/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.php index 773016b7a662d..ef9d0b68409d8 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.php @@ -138,6 +138,7 @@ service('serializer.mapping.class_discriminator_resolver')->ignoreOnInvalid(), null, [], + false, ]) ->alias(PropertyNormalizer::class, 'serializer.normalizer.property') diff --git a/src/Symfony/Component/Mime/Tests/EmailTest.php b/src/Symfony/Component/Mime/Tests/EmailTest.php index 2abffb9b3e521..5d98551b0dbf7 100644 --- a/src/Symfony/Component/Mime/Tests/EmailTest.php +++ b/src/Symfony/Component/Mime/Tests/EmailTest.php @@ -524,7 +524,7 @@ public function testSymfonySerialize() EOF; $extractor = new PhpDocExtractor(); - $propertyNormalizer = new PropertyNormalizer(null, null, $extractor); + $propertyNormalizer = new PropertyNormalizer(null, null, $extractor, null, null, [], true); $serializer = new Serializer([ new ArrayDenormalizer(), new MimeMessageNormalizer($propertyNormalizer), diff --git a/src/Symfony/Component/Mime/Tests/MessageTest.php b/src/Symfony/Component/Mime/Tests/MessageTest.php index 6ed5aabdbe680..c3f8c781cd3b4 100644 --- a/src/Symfony/Component/Mime/Tests/MessageTest.php +++ b/src/Symfony/Component/Mime/Tests/MessageTest.php @@ -245,7 +245,7 @@ public function testSymfonySerialize() EOF; $extractor = new PhpDocExtractor(); - $propertyNormalizer = new PropertyNormalizer(null, null, $extractor); + $propertyNormalizer = new PropertyNormalizer(null, null, $extractor, null, null, [], true); $serializer = new Serializer([ new ArrayDenormalizer(), new MimeMessageNormalizer($propertyNormalizer), diff --git a/src/Symfony/Component/Serializer/CHANGELOG.md b/src/Symfony/Component/Serializer/CHANGELOG.md index 31934dd32ca31..88214d792d2d2 100644 --- a/src/Symfony/Component/Serializer/CHANGELOG.md +++ b/src/Symfony/Component/Serializer/CHANGELOG.md @@ -7,6 +7,8 @@ CHANGELOG * Add support for constructor promoted properties to `Context` attribute * Add context option `PropertyNormalizer::NORMALIZE_VISIBILITY` with bitmask flags `PropertyNormalizer::NORMALIZE_PUBLIC`, `PropertyNormalizer::NORMALIZE_PROTECTED`, `PropertyNormalizer::NORMALIZE_PRIVATE` * Add method `withNormalizeVisibility` to `PropertyNormalizerContextBuilder` +* Add `allowNormalizationOfObjectsWithoutAnyProperties` option to `PropertyNormalizer` +* Add `allowNormalizationOfObjectsWithoutAnyGetters` option to `GetSetMethodNormalizer` 6.1 --- diff --git a/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php index 61d835269ec04..fc0e161fa27d7 100644 --- a/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php @@ -54,6 +54,10 @@ public function __construct( ) { parent::__construct($classMetadataFactory, $nameConverter, $propertyTypeExtractor, $classDiscriminatorResolver, $objectClassResolver, $defaultContext); $this->allowNormalizationOfObjectsWithoutAnyGetters = $allowNormalizationOfObjectsWithoutAnyGetters; + + if (\func_num_args() < 7) { + trigger_deprecation('symfony/serializer', '6.2', '$allowNormalizationOfObjectsWithoutAnyGetters parameter of %s() should be explicitly provided since the default value will change to `true` in symfony/serializer >=7.0', __METHOD__); + } } /** diff --git a/src/Symfony/Component/Serializer/Normalizer/PropertyNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/PropertyNormalizer.php index 29faba8831bbb..6fa17d8773ce8 100644 --- a/src/Symfony/Component/Serializer/Normalizer/PropertyNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/PropertyNormalizer.php @@ -56,6 +56,10 @@ public function __construct(ClassMetadataFactoryInterface $classMetadataFactory } $this->allowNormalizationOfObjectsWithoutAnyProperties = $allowNormalizationOfObjectsWithoutAnyProperties; + + if (\func_num_args() < 7) { + trigger_deprecation('symfony/serializer', '6.2', '$allowNormalizationOfObjectsWithoutAnyProperties parameter of %s() should be explicitly provided since the default value will change to `true` in symfony/serializer >=7.0', __METHOD__); + } } /** diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php index ee305403837c2..b21541ca0bdfe 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php @@ -205,8 +205,8 @@ public function getNormalizer() { $extractor = new PhpDocExtractor(); - yield [new PropertyNormalizer()]; - yield [new PropertyNormalizer(null, null, $extractor)]; + yield [new PropertyNormalizer(null, null, null, null, null, [], true)]; + yield [new PropertyNormalizer(null, null, $extractor, null, null, [], true)]; yield [new ObjectNormalizer()]; yield [new ObjectNormalizer(null, null, null, $extractor)]; } @@ -222,7 +222,7 @@ public function testIgnore() $dummy = new IgnoreDummy(); $dummy->ignored1 = 'hello'; - $normalizer = new PropertyNormalizer($this->classMetadata); + $normalizer = new PropertyNormalizer($this->classMetadata, null, null, null, null, [], true); $this->assertSame([], $normalizer->normalize($dummy)); } diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php index efc9131c58aab..eb1c989f610e2 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php @@ -73,7 +73,7 @@ protected function setUp(): void private function createNormalizer(array $defaultContext = []) { $this->serializer = $this->createMock(SerializerNormalizer::class); - $this->normalizer = new GetSetMethodNormalizer(null, null, null, null, null, $defaultContext); + $this->normalizer = new GetSetMethodNormalizer(null, null, null, null, null, $defaultContext, true); $this->normalizer->setSerializer($this->serializer); } @@ -234,20 +234,20 @@ protected function getNormalizerForCallbacksWithPropertyTypeExtractor(): GetSetM { $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); - return new GetSetMethodNormalizer($classMetadataFactory, new MetadataAwareNameConverter($classMetadataFactory), $this->getCallbackPropertyTypeExtractor()); + return new GetSetMethodNormalizer($classMetadataFactory, new MetadataAwareNameConverter($classMetadataFactory), $this->getCallbackPropertyTypeExtractor(), null, null, [], true); } protected function getNormalizerForCallbacks(): GetSetMethodNormalizer { $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); - return new GetSetMethodNormalizer($classMetadataFactory, new MetadataAwareNameConverter($classMetadataFactory)); + return new GetSetMethodNormalizer($classMetadataFactory, new MetadataAwareNameConverter($classMetadataFactory), null, null, null, [], true); } protected function getNormalizerForCircularReference(array $defaultContext): GetSetMethodNormalizer { $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); - $normalizer = new GetSetMethodNormalizer($classMetadataFactory, new MetadataAwareNameConverter($classMetadataFactory), null, null, null, $defaultContext); + $normalizer = new GetSetMethodNormalizer($classMetadataFactory, new MetadataAwareNameConverter($classMetadataFactory), null, null, null, $defaultContext, true); new Serializer([$normalizer]); return $normalizer; @@ -261,7 +261,7 @@ protected function getSelfReferencingModel() protected function getDenormalizerForConstructArguments(): GetSetMethodNormalizer { $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); - $denormalizer = new GetSetMethodNormalizer($classMetadataFactory, new MetadataAwareNameConverter($classMetadataFactory)); + $denormalizer = new GetSetMethodNormalizer($classMetadataFactory, new MetadataAwareNameConverter($classMetadataFactory), null, null, null, [], true); new Serializer([$denormalizer]); return $denormalizer; @@ -271,20 +271,20 @@ protected function getNormalizerForGroups(): GetSetMethodNormalizer { $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); - return new GetSetMethodNormalizer($classMetadataFactory); + return new GetSetMethodNormalizer($classMetadataFactory, null, null, null, null, [], true); } protected function getDenormalizerForGroups(): GetSetMethodNormalizer { $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); - return new GetSetMethodNormalizer($classMetadataFactory); + return new GetSetMethodNormalizer($classMetadataFactory, null, null, null, null, [], true); } public function testGroupsNormalizeWithNameConverter() { $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); - $this->normalizer = new GetSetMethodNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter()); + $this->normalizer = new GetSetMethodNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter(), null, null, null, [], true); $this->normalizer->setSerializer($this->serializer); $obj = new GroupDummy(); @@ -305,7 +305,7 @@ public function testGroupsNormalizeWithNameConverter() public function testGroupsDenormalizeWithNameConverter() { $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); - $this->normalizer = new GetSetMethodNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter()); + $this->normalizer = new GetSetMethodNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter(), null, null, null, [], true); $this->normalizer->setSerializer($this->serializer); $obj = new GroupDummy(); @@ -326,7 +326,7 @@ public function testGroupsDenormalizeWithNameConverter() protected function getNormalizerForMaxDepth(): NormalizerInterface { $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); - $normalizer = new GetSetMethodNormalizer($classMetadataFactory); + $normalizer = new GetSetMethodNormalizer($classMetadataFactory, null, null, null, null, [], true); $serializer = new Serializer([$normalizer]); $normalizer->setSerializer($serializer); @@ -336,7 +336,7 @@ protected function getNormalizerForMaxDepth(): NormalizerInterface protected function getDenormalizerForObjectToPopulate(): DenormalizerInterface { $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); - $normalizer = new GetSetMethodNormalizer($classMetadataFactory, null, new PhpDocExtractor()); + $normalizer = new GetSetMethodNormalizer($classMetadataFactory, null, new PhpDocExtractor(), null, null, [], true); new Serializer([$normalizer]); return $normalizer; @@ -345,7 +345,7 @@ protected function getDenormalizerForObjectToPopulate(): DenormalizerInterface protected function getDenormalizerForTypeEnforcement(): DenormalizerInterface { $extractor = new PropertyInfoExtractor([], [new PhpDocExtractor(), new ReflectionExtractor()]); - $normalizer = new GetSetMethodNormalizer(null, null, $extractor); + $normalizer = new GetSetMethodNormalizer(null, null, $extractor, null, null, [], true); $serializer = new Serializer([new ArrayDenormalizer(), $normalizer]); $normalizer->setSerializer($serializer); @@ -357,40 +357,32 @@ public function testRejectInvalidKey() $this->markTestSkipped('This test makes no sense with the GetSetMethodNormalizer'); } - protected function getNormalizerAllowingObjectsWithoutGetters(): GetSetMethodNormalizer - { - return new GetSetMethodNormalizer(null, null, null, null, null, [], true); - } - public function testNormalizeObjectWithoutAnyProperties() { - $normalizer = $this->getNormalizerAllowingObjectsWithoutGetters(); $obj = new EmptyObjectDummy(); - $this->assertTrue($normalizer->supportsNormalization($obj)); - + $this->assertTrue($this->normalizer->supportsNormalization($obj)); $this->assertEquals( [], - $normalizer->normalize($obj), + $this->normalizer->normalize($obj), ); } public function testDenormalizeObjectWithoutAnyProperties() { - $normalizer = $this->getNormalizerAllowingObjectsWithoutGetters(); $obj = new EmptyObjectDummy(); - $this->assertTrue($normalizer->supportsDenormalization($obj, \get_class($obj))); + $this->assertTrue($this->normalizer->supportsDenormalization($obj, \get_class($obj))); $this->assertEquals( $obj, - $normalizer->denormalize([], \get_class($obj)), + $this->normalizer->denormalize([], \get_class($obj)), ); } protected function getNormalizerForIgnoredAttributes(): GetSetMethodNormalizer { $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); - $normalizer = new GetSetMethodNormalizer($classMetadataFactory, null, new PhpDocExtractor()); + $normalizer = new GetSetMethodNormalizer($classMetadataFactory, null, new PhpDocExtractor(), null, null, [], true); new Serializer([$normalizer]); return $normalizer; @@ -399,7 +391,7 @@ protected function getNormalizerForIgnoredAttributes(): GetSetMethodNormalizer protected function getDenormalizerForIgnoredAttributes(): GetSetMethodNormalizer { $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); - $normalizer = new GetSetMethodNormalizer($classMetadataFactory, null, new PhpDocExtractor()); + $normalizer = new GetSetMethodNormalizer($classMetadataFactory, null, new PhpDocExtractor(), null, null, [], true); new Serializer([$normalizer]); return $normalizer; @@ -457,7 +449,8 @@ public function testNoTraversableSupport() public function testNoStaticGetSetSupport() { - $this->assertFalse($this->normalizer->supportsNormalization(new ObjectWithJustStaticSetterDummy())); + $normalizer = new GetSetMethodNormalizer(null, null, null, null, null, [], false); + $this->assertFalse($normalizer->supportsNormalization(new ObjectWithJustStaticSetterDummy())); } public function testPrivateSetter() @@ -496,12 +489,12 @@ protected function getObjectCollectionWithExpectedArray(): array protected function getNormalizerForCacheableObjectAttributesTest(): GetSetMethodNormalizer { - return new GetSetMethodNormalizer(); + return new GetSetMethodNormalizer(null, null, null, null, null, [], true); } protected function getNormalizerForSkipUninitializedValues(): NormalizerInterface { - return new GetSetMethodNormalizer(new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()))); + return new GetSetMethodNormalizer(new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())), null, null, null, null, [], true); } } diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/PropertyNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/PropertyNormalizerTest.php index bdcfde9dad71a..1196261207eb8 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/PropertyNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/PropertyNormalizerTest.php @@ -77,7 +77,7 @@ protected function setUp(): void private function createNormalizer(array $defaultContext = []) { $this->serializer = $this->createMock(SerializerInterface::class); - $this->normalizer = new PropertyNormalizer(null, null, null, null, null, $defaultContext); + $this->normalizer = new PropertyNormalizer(null, null, null, null, null, $defaultContext, true); $this->normalizer->setSerializer($this->serializer); } @@ -234,17 +234,17 @@ public function testConstructorDenormalizeWithNullArgument() protected function getNormalizerForCallbacks(): PropertyNormalizer { - return new PropertyNormalizer(); + return new PropertyNormalizer(null, null, null, null, null, [], true); } protected function getNormalizerForCallbacksWithPropertyTypeExtractor(): PropertyNormalizer { - return new PropertyNormalizer(null, null, $this->getCallbackPropertyTypeExtractor()); + return new PropertyNormalizer(null, null, $this->getCallbackPropertyTypeExtractor(), null, null, [], true); } protected function getNormalizerForCircularReference(array $defaultContext): PropertyNormalizer { - $normalizer = new PropertyNormalizer(null, null, null, null, null, $defaultContext); + $normalizer = new PropertyNormalizer(null, null, null, null, null, $defaultContext, true); new Serializer([$normalizer]); return $normalizer; @@ -273,7 +273,7 @@ public function testSiblingReference() protected function getDenormalizerForConstructArguments(): PropertyNormalizer { $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); - $denormalizer = new PropertyNormalizer($classMetadataFactory, new MetadataAwareNameConverter($classMetadataFactory)); + $denormalizer = new PropertyNormalizer($classMetadataFactory, new MetadataAwareNameConverter($classMetadataFactory), null, null, null, [], true); $serializer = new Serializer([$denormalizer]); $denormalizer->setSerializer($serializer); @@ -284,50 +284,42 @@ protected function getNormalizerForGroups(): PropertyNormalizer { $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); - return new PropertyNormalizer($classMetadataFactory); + return new PropertyNormalizer($classMetadataFactory, null, null, null, null, [], true); } protected function getDenormalizerForGroups(): PropertyNormalizer { $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); - return new PropertyNormalizer($classMetadataFactory); - } - - protected function getNormalizerAllowingObjectsWithoutProperties(): PropertyNormalizer - { - return new PropertyNormalizer(null, null, null, null, null, [], true); + return new PropertyNormalizer($classMetadataFactory, null, null, null, null, [], true); } public function testNormalizeObjectWithoutAnyProperties() { - $normalizer = $this->getNormalizerAllowingObjectsWithoutProperties(); $obj = new StaticPropertyDummy(); - $this->assertTrue($normalizer->supportsNormalization($obj)); - + $this->assertTrue($this->normalizer->supportsNormalization($obj)); $this->assertEquals( [], - $normalizer->normalize($obj), + $this->normalizer->normalize($obj), ); } public function testDenormalizeObjectWithoutAnyProperties() { - $normalizer = $this->getNormalizerAllowingObjectsWithoutProperties(); $obj = new StaticPropertyDummy(); - $this->assertTrue($normalizer->supportsDenormalization($obj, \get_class($obj))); + $this->assertTrue($this->normalizer->supportsDenormalization($obj, \get_class($obj))); $this->assertEquals( $obj, - $normalizer->denormalize([], \get_class($obj)), + $this->normalizer->denormalize([], \get_class($obj)), ); } public function testGroupsNormalizeWithNameConverter() { $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); - $this->normalizer = new PropertyNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter()); + $this->normalizer = new PropertyNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter(), null, null, null, [], true); $this->normalizer->setSerializer($this->serializer); $obj = new GroupDummy(); @@ -348,7 +340,7 @@ public function testGroupsNormalizeWithNameConverter() public function testGroupsDenormalizeWithNameConverter() { $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); - $this->normalizer = new PropertyNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter()); + $this->normalizer = new PropertyNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter(), null, null, null, [], true); $this->normalizer->setSerializer($this->serializer); $obj = new GroupDummy(); @@ -368,7 +360,7 @@ public function testGroupsDenormalizeWithNameConverter() protected function getDenormalizerForIgnoredAttributes(): PropertyNormalizer { - $normalizer = new PropertyNormalizer(); + $normalizer = new PropertyNormalizer(null, null, null, null, null, [], true); // instantiate a serializer with the normalizer to handle normalizing recursive structures new Serializer([$normalizer]); @@ -377,7 +369,7 @@ protected function getDenormalizerForIgnoredAttributes(): PropertyNormalizer protected function getNormalizerForIgnoredAttributes(): PropertyNormalizer { - $normalizer = new PropertyNormalizer(); + $normalizer = new PropertyNormalizer(null, null, null, null, null, [], true); // instantiate a serializer with the normalizer to handle normalizing recursive structures new Serializer([$normalizer]); @@ -392,7 +384,7 @@ public function testIgnoredAttributesContextDenormalizeInherit() protected function getNormalizerForMaxDepth(): PropertyNormalizer { $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); - $normalizer = new PropertyNormalizer($classMetadataFactory); + $normalizer = new PropertyNormalizer($classMetadataFactory, null, null, null, null, [], true); $serializer = new Serializer([$normalizer]); $normalizer->setSerializer($serializer); @@ -402,7 +394,7 @@ protected function getNormalizerForMaxDepth(): PropertyNormalizer protected function getDenormalizerForObjectToPopulate(): PropertyNormalizer { $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); - $normalizer = new PropertyNormalizer($classMetadataFactory, null, new PhpDocExtractor()); + $normalizer = new PropertyNormalizer($classMetadataFactory, null, new PhpDocExtractor(), null, null, [], true); new Serializer([$normalizer]); return $normalizer; @@ -411,7 +403,7 @@ protected function getDenormalizerForObjectToPopulate(): PropertyNormalizer protected function getDenormalizerForTypeEnforcement(): DenormalizerInterface { $extractor = new PropertyInfoExtractor([], [new PhpDocExtractor(), new ReflectionExtractor()]); - $normalizer = new PropertyNormalizer(null, null, $extractor); + $normalizer = new PropertyNormalizer(null, null, $extractor, null, null, [], true); $serializer = new Serializer([new ArrayDenormalizer(), $normalizer]); $normalizer->setSerializer($serializer); @@ -455,7 +447,8 @@ public function testNoTraversableSupport() public function testNoStaticPropertySupport() { - $this->assertFalse($this->normalizer->supportsNormalization(new StaticPropertyDummy())); + $normalizer = new PropertyNormalizer(null, null, null, null, null, [], false); + $this->assertFalse($normalizer->supportsNormalization(new StaticPropertyDummy())); } public function testInheritedPropertiesSupport() @@ -525,12 +518,12 @@ protected function getObjectCollectionWithExpectedArray(): array protected function getNormalizerForCacheableObjectAttributesTest(): AbstractObjectNormalizer { - return new PropertyNormalizer(); + return new PropertyNormalizer(null, null, null, null, null, [], true); } protected function getNormalizerForSkipUninitializedValues(): NormalizerInterface { - return new PropertyNormalizer(new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()))); + return new PropertyNormalizer(new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())), null, null, null, null, [], true); } } diff --git a/src/Symfony/Component/Serializer/Tests/SerializerTest.php b/src/Symfony/Component/Serializer/Tests/SerializerTest.php index 855289f745c2d..7dd401830c0af 100644 --- a/src/Symfony/Component/Serializer/Tests/SerializerTest.php +++ b/src/Symfony/Component/Serializer/Tests/SerializerTest.php @@ -194,7 +194,7 @@ public function testDenormalizeWithSupportOnData() public function testSerialize() { - $serializer = new Serializer([new GetSetMethodNormalizer()], ['json' => new JsonEncoder()]); + $serializer = new Serializer([new GetSetMethodNormalizer(null, null, null, null, null, [], true)], ['json' => new JsonEncoder()]); $data = ['title' => 'foo', 'numbers' => [5, 3]]; $result = $serializer->serialize(Model::fromArray($data), 'json'); $this->assertEquals(json_encode($data), $result); @@ -246,7 +246,7 @@ public function testSerializeNoNormalizer() public function testDeserialize() { - $serializer = new Serializer([new GetSetMethodNormalizer()], ['json' => new JsonEncoder()]); + $serializer = new Serializer([new GetSetMethodNormalizer(null, null, null, null, null, [], true)], ['json' => new JsonEncoder()]); $data = ['title' => 'foo', 'numbers' => [5, 3]]; $result = $serializer->deserialize(json_encode($data), Model::class, 'json'); $this->assertEquals($data, $result->toArray()); @@ -254,7 +254,7 @@ public function testDeserialize() public function testDeserializeUseCache() { - $serializer = new Serializer([new GetSetMethodNormalizer()], ['json' => new JsonEncoder()]); + $serializer = new Serializer([new GetSetMethodNormalizer(null, null, null, null, null, [], true)], ['json' => new JsonEncoder()]); $data = ['title' => 'foo', 'numbers' => [5, 3]]; $serializer->deserialize(json_encode($data), Model::class, 'json'); $data = ['title' => 'bar', 'numbers' => [2, 8]]; @@ -288,14 +288,14 @@ public function testDeserializeNoEncoder() public function testDeserializeSupported() { - $serializer = new Serializer([new GetSetMethodNormalizer()], []); + $serializer = new Serializer([new GetSetMethodNormalizer(null, null, null, null, null, [], true)], []); $data = ['title' => 'foo', 'numbers' => [5, 3]]; $this->assertTrue($serializer->supportsDenormalization(json_encode($data), Model::class, 'json')); } public function testDeserializeNotSupported() { - $serializer = new Serializer([new GetSetMethodNormalizer()], []); + $serializer = new Serializer([new GetSetMethodNormalizer(null, null, null, null, null, [], false)], []); $data = ['title' => 'foo', 'numbers' => [5, 3]]; $this->assertFalse($serializer->supportsDenormalization(json_encode($data), 'stdClass', 'json')); } @@ -327,8 +327,8 @@ public function testSupportsArrayDeserialization() { $serializer = new Serializer( [ - new GetSetMethodNormalizer(), - new PropertyNormalizer(), + new GetSetMethodNormalizer(null, null, null, null, null, [], true), + new PropertyNormalizer(null, null, null, null, null, [], true), new ObjectNormalizer(), new CustomNormalizer(), new ArrayDenormalizer(), @@ -354,7 +354,7 @@ public function testDeserializeArray() $serializer = new Serializer( [ - new GetSetMethodNormalizer(), + new GetSetMethodNormalizer(null, null, null, null, null, [], true), new ArrayDenormalizer(), ], [ @@ -529,14 +529,14 @@ public function testNotNormalizableValueExceptionMessageForAResource() public function testNormalizeTransformEmptyArrayObjectToArray() { $serializer = new Serializer( - [ - new PropertyNormalizer(), - new ObjectNormalizer(), - new ArrayDenormalizer(), - ], - [ - 'json' => new JsonEncoder(), - ] + [ + new PropertyNormalizer(null, null, null, null, null, [], true), + new ObjectNormalizer(), + new ArrayDenormalizer(), + ], + [ + 'json' => new JsonEncoder(), + ] ); $object = []; @@ -552,14 +552,14 @@ public function testNormalizeTransformEmptyArrayObjectToArray() public function provideObjectOrCollectionTests() { $serializer = new Serializer( - [ - new PropertyNormalizer(), - new ObjectNormalizer(), - new ArrayDenormalizer(), - ], - [ - 'json' => new JsonEncoder(), - ] + [ + new PropertyNormalizer(null, null, null, null, null, [], true), + new ObjectNormalizer(), + new ArrayDenormalizer(), + ], + [ + 'json' => new JsonEncoder(), + ] ); $data = [];