Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 296df0c

Browse filesBrowse files
committed
bug #57187 [Serializer] Fix ObjectNormalizer with property path (HypeMC)
This PR was merged into the 5.4 branch. Discussion ---------- [Serializer] Fix `ObjectNormalizer` with property path | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #54983 | License | MIT Caused by #52917. The `ObjectNormalizer::isAllowedAttribute()` method doesn't work with property paths, this is an attempt to fix the problem. Commits ------- 3857545d33 [Serializer] Fix `ObjectNormalizer` with property path
2 parents 8cdef77 + d1ffba3 commit 296df0c
Copy full SHA for 296df0c

File tree

Expand file treeCollapse file tree

3 files changed

+45
-1
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+45
-1
lines changed

‎Normalizer/ObjectNormalizer.php

Copy file name to clipboardExpand all lines: Normalizer/ObjectNormalizer.php
+5-1Lines 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)) {
+5Lines 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' ]

‎Tests/Normalizer/ObjectNormalizerTest.php

Copy file name to clipboardExpand all lines: Tests/Normalizer/ObjectNormalizerTest.php
+35Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.