From e3605a57c98f9905ad525352610b8f134440b4b6 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Thu, 2 May 2024 10:49:46 +0200 Subject: [PATCH 1/2] add missing assertion --- Tests/Encoder/CsvEncoderTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/Encoder/CsvEncoderTest.php b/Tests/Encoder/CsvEncoderTest.php index 3d2163c0..9b1bbfb2 100644 --- a/Tests/Encoder/CsvEncoderTest.php +++ b/Tests/Encoder/CsvEncoderTest.php @@ -223,8 +223,8 @@ public function testDecodeEmptyData() public function testMultipleEmptyHeaderNamesWithSeparator() { - $this->encoder->decode(',. -,', 'csv'); + $this->assertSame([['', [1 => '']]], $this->encoder->decode(',. +,', 'csv')); } public function testEncodeVariableStructure() From 703ec139ddd9505979aba06338f67e276e7b21e0 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Thu, 2 May 2024 11:21:14 +0200 Subject: [PATCH 2/2] separate the property info and write info extractors --- Normalizer/ObjectNormalizer.php | 13 +++++++++++-- Tests/Normalizer/ObjectNormalizerTest.php | 16 ++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/Normalizer/ObjectNormalizer.php b/Normalizer/ObjectNormalizer.php index a1ab1117..6a5413f6 100644 --- a/Normalizer/ObjectNormalizer.php +++ b/Normalizer/ObjectNormalizer.php @@ -36,6 +36,7 @@ class ObjectNormalizer extends AbstractObjectNormalizer protected $propertyAccessor; protected $propertyInfoExtractor; + private $writeInfoExtractor; private $objectClassResolver; @@ -54,6 +55,7 @@ public function __construct(?ClassMetadataFactoryInterface $classMetadataFactory }; $this->propertyInfoExtractor = $propertyInfoExtractor ?: new ReflectionExtractor(); + $this->writeInfoExtractor = new ReflectionExtractor(); } /** @@ -195,8 +197,15 @@ protected function isAllowedAttribute($classOrObject, string $attribute, ?string return $this->propertyInfoExtractor->isReadable($class, $attribute) || $this->hasAttributeAccessorMethod($class, $attribute); } - return $this->propertyInfoExtractor->isWritable($class, $attribute) - || ($writeInfo = $this->propertyInfoExtractor->getWriteInfo($class, $attribute)) && PropertyWriteInfo::TYPE_NONE !== $writeInfo->getType(); + if ($this->propertyInfoExtractor->isWritable($class, $attribute)) { + return true; + } + + if (($writeInfo = $this->writeInfoExtractor->getWriteInfo($class, $attribute)) && PropertyWriteInfo::TYPE_NONE !== $writeInfo->getType()) { + return true; + } + + return false; } private function hasAttributeAccessorMethod(string $class, string $attribute): bool diff --git a/Tests/Normalizer/ObjectNormalizerTest.php b/Tests/Normalizer/ObjectNormalizerTest.php index 830817b8..5f888449 100644 --- a/Tests/Normalizer/ObjectNormalizerTest.php +++ b/Tests/Normalizer/ObjectNormalizerTest.php @@ -274,6 +274,22 @@ public function testConstructorWithObjectDenormalize() $this->assertEquals('bar', $obj->bar); } + public function testConstructorWithObjectDenormalizeUsingPropertyInfoExtractor() + { + $serializer = $this->createMock(ObjectSerializerNormalizer::class); + $normalizer = new ObjectNormalizer(null, null, null, null, null, null, [], new PropertyInfoExtractor()); + $normalizer->setSerializer($serializer); + + $data = new \stdClass(); + $data->foo = 'foo'; + $data->bar = 'bar'; + $data->baz = true; + $data->fooBar = 'foobar'; + $obj = $normalizer->denormalize($data, ObjectConstructorDummy::class, 'any'); + $this->assertEquals('foo', $obj->getFoo()); + $this->assertEquals('bar', $obj->bar); + } + public function testConstructorWithObjectTypeHintDenormalize() { $data = [