8000 [Serializer] Fix `ObjectNormalizer` with property path by HypeMC · Pull Request #57187 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] Fix ObjectNormalizer with property path #57187

8000 New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,11 @@ protected function isAllowedAttribute($classOrObject, string $attribute, ?string
$class = \is_object($classOrObject) ? \get_class($classOrObject) : $classOrObject;

if ($context['_read_attributes'] ?? true) {
return $this->propertyInfoExtractor->isReadable($class, $attribute) || $this->hasAttributeAccessorMethod($class, $attribute);
return (\is_object($classOrObject) && $this->propertyAccessor->isReadable($classOrObject, $attribute)) || $this->propertyInfoExtractor->isReadable($class, $attribute) || $this->hasAttributeAccessorMethod($class, $attribute);
}

if (str_contains($attribute, '.')) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know a better way to fix this. The $this->propertyAccessor->isWritable() method expects an object, but the isAllowedAttribute() method is called before the object is instantiated.

Perhaps the $this->propertyAccessor->isReadable() check should be removed as well, and the method should simply return true if the attribute contains a .?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, I think that when can only rely on the . in the first place, as it'll stick a bit more with the legacy behavior.

But later on I do think that it can be great to be able to know if foo.bar.baz is readable/writable only based on reflection, so we have a more accurate behavior.

return true;
}

if ($this->propertyInfoExtractor->isWritable($class, $attribute)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Symfony\Component\Serializer\Tests\Normalizer\ObjectOuter:
attributes:
inner.foo:
serialized_name: inner_foo
groups: [ 'read' ]
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Serializer\Mapping\Loader\YamlFileLoader;
use Symfony\Component\Serializer\NameConverter\AdvancedNameConverterInterface;
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
Expand Down Expand Up @@ -911,6 +912,40 @@ public function testDenormalizeWithIgnoreAnnotationAndPrivateProperties()

$this->assertEquals($expected, $obj);
}

public function testNormalizeWithPropertyPath()
{
$classMetadataFactory = new ClassMetadataFactory(new YamlFileLoader(__DIR__.'/../Fixtures/property-path-mapping.yaml'));
$normalizer = new ObjectNormalizer($classMetadataFactory, new MetadataAwareNameConverter($classMetadataFactory));

$dummyInner = new ObjectInner();
$dummyInner->foo = 'foo';
$dummy = new ObjectOuter();
$dummy->setInner($dummyInner);

$this->assertSame(['inner_foo' => 'foo'], $normalizer->normalize($dummy, 'json', ['groups' => 'read']));
}

public function testDenormalizeWithPropertyPath()
{
$classMetadataFactory = new ClassMetadataFactory(new YamlFileLoader(__DIR__.'/../Fixtures/property-path-mapping.yaml'));
$normalizer = new ObjectNormalizer($classMetadataFactory, new MetadataAwareNameConverter($classMetadataFactory));

$dummy = new ObjectOuter();
$dummy->setInner(new ObjectInner());

$obj = $normalizer->denormalize(['inner_foo' => 'foo'], ObjectOuter::class, 'json', [
'object_to_populate' => $dummy,
'groups' => 'read',
]);

$expectedInner = new ObjectInner();
$expectedInner->foo = 'foo';
$expected = new ObjectOuter();
$expected->setInner($expectedInner);

$this->assertEquals($expected, $obj);
}
}

class ProxyObjectDummy extends ObjectDummy
Expand Down
0