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 87d5cab

Browse filesBrowse files
committed
[Serializer] Add support for stdClass
1 parent 33e2735 commit 87d5cab
Copy full SHA for 87d5cab

File tree

3 files changed

+20
-1
lines changed
Filter options

3 files changed

+20
-1
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php
+1-1Lines changed: 1 addition & 1 deletion
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'] && $class !== \stdClass::class) {
266266
$this->attributesCache[$key] = $attributes;
267267
}
268268

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php
+4Lines changed: 4 additions & 0 deletions
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 (\get_class($object) === \stdClass::class) {
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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,21 @@ 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+
713+
}
699714
}
700715

701716
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.