Skip to content

Commit 99ca1ab

Browse files
committed
feature #35596 [Serializer] Add support for stdClass (dunglas)
This PR was merged into the 5.1-dev branch. Discussion ---------- [Serializer] Add support for stdClass | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #33047, #33894 | License | MIT | Doc PR | n/a Add support for `stdClass`. Alternative to #33894. Commits ------- d7bca80 [Serializer] Add support for stdClass
2 parents 33e2735 + d7bca80 commit 99ca1ab

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ protected function getAttributes($object, string $format = null, array $context)
262262
array_unshift($attributes, $mapping->getTypeProperty());
263263
}
264264

265-
if ($context['cache_key']) {
265+
if ($context['cache_key'] && \stdClass::class !== $class) {
266266
$this->attributesCache[$key] = $attributes;
267267
}
268268

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

+4
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ public function hasCacheableSupportsMethod(): bool
6262
*/
6363
protected function extractAttributes(object $object, string $format = null, array $context = [])
6464
{
65+
if (\stdClass::class === \get_class($object)) {
66+
return array_keys((array) $object);
67+
}
68+
6569
// If not using groups, detect manually
6670
$attributes = [];
6771

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

+14
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,20 @@ public function testObjectClassResolver()
696696
$normalizer->normalize($obj, 'any')
697697
);
698698
}
699+
700+
public function testNormalizeStdClass()
701+
{
702+
$o1 = new \stdClass();
703+
$o1->foo = 'f';
704+
$o1->bar = 'b';
705+
706+
$this->assertSame(['foo' => 'f', 'bar' => 'b'], $this->normalizer->normalize($o1));
707+
708+
$o2 = new \stdClass();
709+
$o2->baz = 'baz';
710+
711+
$this->assertSame(['baz' => 'baz'], $this->normalizer->normalize($o2));
712+
}
699713
}
700714

701715
class ProxyObjectDummy extends ObjectDummy

0 commit comments

Comments
 (0)