Skip to content

Commit

Permalink
feature #35596 [Serializer] Add support for stdClass (dunglas)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
chalasr committed Feb 5, 2020
2 parents 33e2735 + d7bca80 commit 99ca1ab
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
Expand Up @@ -262,7 +262,7 @@ protected function getAttributes($object, string $format = null, array $context)
array_unshift($attributes, $mapping->getTypeProperty());
}

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

Expand Down
Expand Up @@ -62,6 +62,10 @@ public function hasCacheableSupportsMethod(): bool
*/
protected function extractAttributes(object $object, string $format = null, array $context = [])
{
if (\stdClass::class === \get_class($object)) {
return array_keys((array) $object);
}

// If not using groups, detect manually
$attributes = [];

Expand Down
Expand Up @@ -696,6 +696,20 @@ public function testObjectClassResolver()
$normalizer->normalize($obj, 'any')
);
}

public function testNormalizeStdClass()
{
$o1 = new \stdClass();
$o1->foo = 'f';
$o1->bar = 'b';

$this->assertSame(['foo' => 'f', 'bar' => 'b'], $this->normalizer->normalize($o1));

$o2 = new \stdClass();
$o2->baz = 'baz';

$this->assertSame(['baz' => 'baz'], $this->normalizer->normalize($o2));
}
}

class ProxyObjectDummy extends ObjectDummy
Expand Down

0 comments on commit 99ca1ab

Please sign in to comment.