Skip to content

[Serializer] Serializing empty class without properties serializes to [] when preserve_empty_objects is enabled #56875

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
ruudk opened this issue May 22, 2024 · 5 comments

Comments

@ruudk
Copy link
Contributor

ruudk commented May 22, 2024

Symfony version(s) affected

7.1-RC1

Description

While testing 7.1-RC1 I noticed a change in behavior.

I have a test suite that serializes all commands (for a command bus) and confirms that they can be deserialized.

There is a command that does not have any properties:

final class DoSomethingCommand {}

When this is serialized with Symfony 7.0, it produces {}.

But with Symfony 7.1-RC1 it becomes [].

How to reproduce

See above.

Possible Solution

While debugging the situation, I notice that in 7.0 the empty object gets normalized into ArrayObject (empty).
Screenshot 2024-05-22 at 09 29 47@2x

But on 7.1-RC1 it becomes an empty array.
Screenshot 2024-05-22 at 09 31 35@2x

Additional Context

No response

@ruudk
Copy link
Contributor Author

ruudk commented May 22, 2024

I have enabled the feature in the default context in the Framework:

            'serializer' => [
                'enabled' => true,
                'name_converter' => 'serializer.name_converter.camel_case_to_snake_case',
                'default_context' => [
                    AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS => true,
                    Serializer::EMPTY_ARRAY_AS_OBJECT => true,
                ],
            ],

It might be the case, that this default context is no longer used. Not sure.

@HypeMC
Copy link
Contributor

HypeMC commented May 26, 2024

@ruudk Could you see if #57186 fixes your issue?

@ruudk
Copy link
Contributor Author

ruudk commented May 28, 2024

@HypeMC This does solve my issue indeed, thanks!

@HypeMC
Copy link
Contributor

HypeMC commented May 31, 2024

@ruudk After some feed back I realized that my initial fix was completely wrong. Even though it work, it was not the right place for it, so could you try #57273 instead.

@ruudk
Copy link
Contributor Author

ruudk commented Jun 3, 2024

@HypeMC It does solve the issue! 🎉

fabpot added a commit that referenced this issue Jun 15, 2024
…normalizers (HypeMC)

This PR was merged into the 5.4 branch.

Discussion
----------

[FrameworkBundle] Fix setting default context for certain normalizers

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #56875, fix #57316
| License       | MIT

Caused by #54791. The main problem is that `$context` defaults to `[]` instead of `$defaultContext`. There's a test to check this, but it didn't work when `circular_reference_handler` or `max_depth_handler` were not `null`.

I also found an issue with `serializer.normalizer.property`. Since it’s not tagged with `serializer.normalizer`, which, to my understanding, is intentional, it would never have the default context bound to it.

Commits
-------

f903893 [FrameworkBundle] Fix setting default context for certain normalizers
@fabpot fabpot closed this as completed Jun 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants