Skip to content

Commit 3857545

Browse files
committed
[Serializer] Fix ObjectNormalizer with property path
1 parent 2e0dafa commit 3857545

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,11 @@ protected function isAllowedAttribute($classOrObject, string $attribute, ?string
194194
$class = \is_object($classOrObject) ? \get_class($classOrObject) : $classOrObject;
195195

196196
if ($context['_read_attributes'] ?? true) {
197-
return $this->propertyInfoExtractor->isReadable($class, $attribute) || $this->hasAttributeAccessorMethod($class, $attribute);
197+
return (\is_object($classOrObject) && $this->propertyAccessor->isReadable($classOrObject, $attribute)) || $this->propertyInfoExtractor->isReadable($class, $attribute) || $this->hasAttributeAccessorMethod($class, $attribute);
198+
}
199+
200+
if (str_contains($attribute, '.')) {
201+
return true;
198202
}
199203

200204
if ($this->propertyInfoExtractor->isWritable($class, $attribute)) {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Symfony\Component\Serializer\Tests\Normalizer\ObjectOuter:
2+
attributes:
3+
inner.foo:
4+
serialized_name: inner_foo
5+
groups: [ 'read' ]

src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
2626
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
2727
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
28+
use Symfony\Component\Serializer\Mapping\Loader\YamlFileLoader;
2829
use Symfony\Component\Serializer\NameConverter\AdvancedNameConverterInterface;
2930
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
3031
use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
@@ -911,6 +912,40 @@ public function testDenormalizeWithIgnoreAnnotationAndPrivateProperties()
911912

912913
$this->assertEquals($expected, $obj);
913914
}
915+
916+
public function testNormalizeWithPropertyPath()
917+
{
918+
$classMetadataFactory = new ClassMetadataFactory(new YamlFileLoader(__DIR__.'/../Fixtures/property-path-mapping.yaml'));
919+
$normalizer = new ObjectNormalizer($classMetadataFactory, new MetadataAwareNameConverter($classMetadataFactory));
920+
921+
$dummyInner = new ObjectInner();
922+
$dummyInner->foo = 'foo';
923+
$dummy = new ObjectOuter();
924+
$dummy->setInner($dummyInner);
925+
926+
$this->assertSame(['inner_foo' => 'foo'], $normalizer->normalize($dummy, 'json', ['groups' => 'read']));
927+
}
928+
929+
public function testDenormalizeWithPropertyPath()
930+
{
931+
$classMetadataFactory = new ClassMetadataFactory(new YamlFileLoader(__DIR__.'/../Fixtures/property-path-mapping.yaml'));
932+
$normalizer = new ObjectNormalizer($classMetadataFactory, new MetadataAwareNameConverter($classMetadataFactory));
933+
934+
$dummy = new ObjectOuter();
935+
$dummy->setInner(new ObjectInner());
936+
937+
$obj = $normalizer->denormalize(['inner_foo' => 'foo'], ObjectOuter::class, 'json', [
938+
'object_to_populate' => $dummy,
939+
'groups' => 'read',
940+
]);
941+
942+
$expectedInner = new ObjectInner();
943+
$expectedInner->foo = 'foo';
944+
$expected = new ObjectOuter();
945+
$expected->setInner($expectedInner);
946+
947+
$this->assertEquals($expected, $obj);
948+
}
914949
}
915950

916951
class ProxyObjectDummy extends ObjectDummy

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy