From a382d97cd78566b039d15339b4f3d803c3459b77 Mon Sep 17 00:00:00 2001 From: dbojdo Date: Tue, 21 Mar 2023 09:34:51 +0000 Subject: [PATCH 1/2] Fix #923 Enabled support for DefaultValuePropertyDriver --- .../JMSSerializerExtension.php | 9 ++++++++ Resources/config/services.xml | 6 ++++++ .../Fixture/DefaultValuePropObject.php | 8 +++++++ .../JMSSerializerExtensionTest.php | 21 +++++++++++++++++++ 4 files changed, 44 insertions(+) create mode 100644 Tests/DependencyInjection/Fixture/DefaultValuePropObject.php diff --git a/DependencyInjection/JMSSerializerExtension.php b/DependencyInjection/JMSSerializerExtension.php index 35ebc51e..b8764b81 100644 --- a/DependencyInjection/JMSSerializerExtension.php +++ b/DependencyInjection/JMSSerializerExtension.php @@ -264,6 +264,15 @@ private function loadInternal(array $config, ScopedContainer $container, array $ $container->removeDefinition('jms_serializer.enum_subscriber'); } + // enable the default value property reader on php 8.0+ + if (PHP_VERSION_ID >= 80000) { + $container->getDefinition('jms_serializer.metadata.default_value_property_driver') + ->setDecoratedService('jms_serializer.metadata_driver') + ->setPublic(false); + } else { + $container->removeDefinition('jms_serializer.metadata.default_value_property_driver'); + } + $container ->getDefinition('jms_serializer.metadata_factory') ->replaceArgument(2, $config['metadata']['debug']) diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 9ef4b3b6..162bfef2 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -135,6 +135,12 @@ + + + + + + diff --git a/Tests/DependencyInjection/Fixture/DefaultValuePropObject.php b/Tests/DependencyInjection/Fixture/DefaultValuePropObject.php new file mode 100644 index 00000000..74bf63a7 --- /dev/null +++ b/Tests/DependencyInjection/Fixture/DefaultValuePropObject.php @@ -0,0 +1,8 @@ +propertyMetadata['foo']->type['name']); } + public function testDefaultValuePropertyDriverIsEnabled() + { + if (PHP_VERSION_ID < 80000) { + $this->markTestSkipped(sprintf('%s requires PHP 8.0', __METHOD__)); + } + + if (!class_exists(DefaultValuePropertyDriver::class)) { + $this->markTestSkipped(sprintf('%s requires %s', __METHOD__, DefaultValuePropertyDriver::class)); + } + + $container = $this->getContainerForConfig([[]]); + + $metadata = $container->get('jms_serializer.metadata_driver') + ->loadMetadataForClass(new \ReflectionClass(DefaultValuePropObject::class)); + + self::assertSame('some-value', $metadata->propertyMetadata['foo']->defaultValue); + } + public function testIncludeInterfaces() { $container = $this->getContainerForConfig([ From fe76185917e6a9a3f1351bc80a22de1ff1572ec7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Koz=C3=A1k?= Date: Thu, 20 Jul 2023 16:53:41 +0200 Subject: [PATCH 2/2] Add default_value_property_reader_support config --- DependencyInjection/Configuration.php | 1 + .../JMSSerializerExtension.php | 2 +- Resources/doc/configuration.rst | 3 ++- .../Fixture/DefaultValuePropObject.php | 3 +++ .../JMSSerializerExtensionTest.php | 26 +++++++++++++++++-- 5 files changed, 31 insertions(+), 4 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index fa883f65..173129a9 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -57,6 +57,7 @@ public function getConfigTreeBuilder() private function addConfigNodes($root): void { $root->scalarNode('enum_support')->defaultValue(false)->end(); + $root->scalarNode('default_value_property_reader_support')->defaultValue(false)->end(); $this->addHandlersSection($root); $this->addSubscribersSection($root); $this->addObjectConstructorsSection($root); diff --git a/DependencyInjection/JMSSerializerExtension.php b/DependencyInjection/JMSSerializerExtension.php index b8764b81..256cd6b5 100644 --- a/DependencyInjection/JMSSerializerExtension.php +++ b/DependencyInjection/JMSSerializerExtension.php @@ -265,7 +265,7 @@ private function loadInternal(array $config, ScopedContainer $container, array $ } // enable the default value property reader on php 8.0+ - if (PHP_VERSION_ID >= 80000) { + if (PHP_VERSION_ID >= 80000 && $config['default_value_property_reader_support']) { $container->getDefinition('jms_serializer.metadata.default_value_property_driver') ->setDecoratedService('jms_serializer.metadata_driver') ->setPublic(false); diff --git a/Resources/doc/configuration.rst b/Resources/doc/configuration.rst index 7bb5546a..7d43cbfc 100644 --- a/Resources/doc/configuration.rst +++ b/Resources/doc/configuration.rst @@ -195,7 +195,8 @@ values: # config.yml jms_serializer: profiler: %kernel.debug% - enum_support: true # BHP 8.1 Enums support, false by default for backward compatibility + enum_support: true # PHP 8.1 Enums support, false by default for backward compatibility + default_value_property_reader_support: true # PHP 8.0 Constructor Promotion default value support, false by default for backward compatibility twig_enabled: 'default' # on which instance is twig enabled handlers: datetime: diff --git a/Tests/DependencyInjection/Fixture/DefaultValuePropObject.php b/Tests/DependencyInjection/Fixture/DefaultValuePropObject.php index 74bf63a7..c91783dd 100644 --- a/Tests/DependencyInjection/Fixture/DefaultValuePropObject.php +++ b/Tests/DependencyInjection/Fixture/DefaultValuePropObject.php @@ -5,4 +5,7 @@ class DefaultValuePropObject { private string $foo = "some-value"; + + public function __construct(private string $boo = "another-value") { + } } diff --git a/Tests/DependencyInjection/JMSSerializerExtensionTest.php b/Tests/DependencyInjection/JMSSerializerExtensionTest.php index 4a799b53..9631c1b9 100644 --- a/Tests/DependencyInjection/JMSSerializerExtensionTest.php +++ b/Tests/DependencyInjection/JMSSerializerExtensionTest.php @@ -793,7 +793,7 @@ public function testTypedDriverIsEnabled() self::assertSame('int', $metadata->propertyMetadata['foo']->type['name']); } - public function testDefaultValuePropertyDriverIsEnabled() + public function testDefaultValuePropertyDriverCanBeEnabled() { if (PHP_VERSION_ID < 80000) { $this->markTestSkipped(sprintf('%s requires PHP 8.0', __METHOD__)); @@ -803,12 +803,34 @@ public function testDefaultValuePropertyDriverIsEnabled() $this->markTestSkipped(sprintf('%s requires %s', __METHOD__, DefaultValuePropertyDriver::class)); } - $container = $this->getContainerForConfig([[]]); + $container = $this->getContainerForConfig([ + ['default_value_property_reader_support' => true], + ]); $metadata = $container->get('jms_serializer.metadata_driver') ->loadMetadataForClass(new \ReflectionClass(DefaultValuePropObject::class)); self::assertSame('some-value', $metadata->propertyMetadata['foo']->defaultValue); + self::assertSame('another-value', $metadata->propertyMetadata['boo']->defaultValue); + + $serializer = $container->get('jms_serializer'); + $object = new DefaultValuePropObject(); + $this->assertEquals('{"foo":"some-value","boo":"another-value"}', $serializer->serialize($object, 'json')); + $this->assertEquals($object, $serializer->deserialize('{}', DefaultValuePropObject::class, 'json')); + } + + public function testDefaultValuePropertyDriverIsDisabledByDefault() + { + if (PHP_VERSION_ID < 80000) { + $this->markTestSkipped(sprintf('%s requires PHP 8.0', __METHOD__)); + } + + $container = $this->getContainerForConfig([[]]); + + $serializer = $container->get('jms_serializer'); + $object = new DefaultValuePropObject(); + + $this->assertNotEquals($object, $serializer->deserialize('{}', DefaultValuePropObject::class, 'json')); } public function testIncludeInterfaces()