Skip to content

Add support for serializing stdClass objects #33894

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

henry2778
Copy link
Contributor

Q A
Branch? 4.4
Bug fix? no
New feature? yes
Deprecations? no
Tickets Fix #33047
License MIT
Doc PR todo

Updated PR for issue #33047 :) Adding support for serializing stdClass objects, because it was missing. Thanks!

@@ -81,7 +82,7 @@ public function testNormalizeNoMatch()
{
$this->expectException('Symfony\Component\Serializer\Exception\UnexpectedValueException');
$serializer = new Serializer([$this->getMockBuilder('Symfony\Component\Serializer\Normalizer\CustomNormalizer')->getMock()]);
$serializer->normalize(new \stdClass(), 'xml');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change looks like a tiny BC break, on the other hand this test was relying on an (undocumented) edge case. WDYT @symfony/mergers?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What’s the change? That previously you would get an exception with stdClass and now it would work? Or am I missing the point?

Copy link
Contributor Author

@henry2778 henry2778 Oct 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that test covers the case when we have no matching normalizer for the class that is not normalizable. So its not really important which not-normalizable class we are trying to normalize

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think what @dunglas means is that currently an instance of stdClass can be properly serialized if you register a custom normalizer that supports it, with this PR such normalizer wouldn't be called anymore.
IMHO it's ok but only if we don't match classes extending stdClass (yes, one can do that..), I suggest using \stdClass::class === \get_class($data) instead of instanceof.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not making it part of the object normalizer instead? It has a very low priority that would mitigate this issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ogizanagi thanks, good idea. Will check that tomorrow, I think that will work

Copy link
Member

@chalasr chalasr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my comment above

@henry2778
Copy link
Contributor Author

@dunglas @chalasr please have a look at my implementation of @ogizanagi suggestion. Thanks!

@henry2778 henry2778 requested a review from chalasr October 12, 2019 09:00
@henry2778 henry2778 requested a review from dunglas October 12, 2019 09:00
@henry2778 henry2778 force-pushed the feature/serailizer-std-class-support branch from 3ffe9d6 to 31b83fe Compare October 19, 2019 09:58
@henry2778 henry2778 force-pushed the feature/serailizer-std-class-support branch from 31b83fe to 7a06dbf Compare December 8, 2019 19:24
@yoshz
Copy link

yoshz commented Jan 15, 2020

Any update on this MR? This is a quite annoying bug.

@fabpot
Copy link
Member

fabpot commented Feb 4, 2020

ping @dunglas

@dunglas
Copy link
Member

dunglas commented Feb 5, 2020

The new implementation doesn't look good to me. It doesn't support any of the feature of ObjectNormalizer (the circular references and max depth handler, the callbacks...), while it should. Maybe than extracting stdClass's properties could be handled in extractAttributes instead.

@dunglas
Copy link
Member

dunglas commented Feb 5, 2020

I just opened an alternative PR with what I've in mind: #35596

@chalasr
Copy link
Member

chalasr commented Feb 5, 2020

Thank you for pushing this @henry2778.
Closing in favor of #35596

@chalasr chalasr closed this Feb 5, 2020
chalasr added a commit that referenced this pull request Feb 5, 2020
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
@nicolas-grekas nicolas-grekas modified the milestones: next, 5.1 May 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants