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 98e0975

Browse filesBrowse files
committed
bug #31026 [Serializer] Add default object class resolver (jdecool)
This PR was squashed before being merged into the 4.2 branch (closes #31026). Discussion ---------- [Serializer] Add default object class resolver | Q | A | ------------- | --- | Branch? | 4.2 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - The commit 1d8b5af introduce a BC break because before that commit the `extractAttributes` the `$object` can be a string which contain the fully qualified name of an object. To fix the BC break and preserve the new feature, I suggest to create a default object class resolver if it is not set by the developer. Commits ------- dd5b8f1 [Serializer] Add default object class resolver
2 parents de53bd6 + dd5b8f1 commit 98e0975
Copy full SHA for 98e0975

File tree

2 files changed

+29
-2
lines changed
Filter options

2 files changed

+29
-2
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php
+5-2Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ public function __construct(ClassMetadataFactoryInterface $classMetadataFactory
4343
parent::__construct($classMetadataFactory, $nameConverter, $propertyTypeExtractor, $classDiscriminatorResolver, $objectClassResolver, $defaultContext);
4444

4545
$this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
46-
$this->objectClassResolver = $objectClassResolver;
46+
47+
$this->objectClassResolver = $objectClassResolver ?? function ($class) {
48+
return \is_object($class) ? \get_class($class) : $class;
49+
};
4750
}
4851

4952
/**
@@ -63,7 +66,7 @@ protected function extractAttributes($object, $format = null, array $context = [
6366
$attributes = [];
6467

6568
// methods
66-
$class = $this->objectClassResolver ? ($this->objectClassResolver)($object) : \get_class($object);
69+
$class = ($this->objectClassResolver)($object);
6770
$reflClass = new \ReflectionClass($class);
6871

6972
foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflMethod) {

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,6 +1043,30 @@ public function denormalize($propertyName, string $class = null, string $format
10431043
$this->assertArrayHasKey('foo-Symfony\Component\Serializer\Tests\Normalizer\ObjectDummy-json-bar', $normalizer->normalize(new ObjectDummy(), 'json', ['foo' => 'bar']));
10441044
}
10451045

1046+
public function testDefaultObjectClassResolver()
1047+
{
1048+
$normalizer = new ObjectNormalizer();
1049+
1050+
$obj = new ObjectDummy();
1051+
$obj->setFoo('foo');
1052+
$obj->bar = 'bar';
1053+
$obj->setBaz(true);
1054+
$obj->setCamelCase('camelcase');
1055+
$obj->unwantedProperty = 'notwanted';
1056+
1057+
$this->assertEquals(
1058+
[
1059+
'foo' => 'foo',
1060+
'bar' => 'bar',
1061+
'baz' => true,
1062+
'fooBar' => 'foobar',
1063+
'camelCase' => 'camelcase',
1064+
'object' => null,
1065+
],
1066+
$normalizer->normalize($obj, 'any')
1067+
);
1068+
}
1069+
10461070
public function testObjectClassResolver()
10471071
{
10481072
$classResolver = function ($object) {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.