Skip to content

Commit

Permalink
Merge branch '7.0' into 7.1
Browse files Browse the repository at this point in the history
* 7.0:
  separate the property info and write info extractors
  add missing assertion
  [Filesystem] Run high-deps tests with Process 7
  • Loading branch information
fabpot committed May 2, 2024
2 parents a5e1313 + bb28f4c commit 9cfe3e9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
13 changes: 11 additions & 2 deletions src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php
Expand Up @@ -36,6 +36,7 @@ final class ObjectNormalizer extends AbstractObjectNormalizer

protected PropertyAccessorInterface $propertyAccessor;
protected $propertyInfoExtractor;
private $writeInfoExtractor;

private readonly \Closure $objectClassResolver;

Expand All @@ -51,6 +52,7 @@ public function __construct(?ClassMetadataFactoryInterface $classMetadataFactory

$this->objectClassResolver = ($objectClassResolver ?? static fn ($class) => \is_object($class) ? $class::class : $class)(...);
$this->propertyInfoExtractor = $propertyInfoExtractor ?: new ReflectionExtractor();
$this->writeInfoExtractor = new ReflectionExtractor();
}

public function getSupportedTypes(?string $format): array
Expand Down Expand Up @@ -174,8 +176,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
Expand Down
Expand Up @@ -214,8 +214,8 @@ public function testDecodeEmptyData()

public function testMultipleEmptyHeaderNamesWithSeparator()
{
$this->encoder->decode(',.
,', 'csv');
$this->assertSame([['', [1 => '']]], $this->encoder->decode(',.
,', 'csv'));
}

public function testEncodeVariableStructure()
Expand Down
Expand Up @@ -269,6 +269,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 = [
Expand Down

0 comments on commit 9cfe3e9

Please sign in to comment.