Skip to content

[Serializer] Add support for serializing empty array as object #42297

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

Merged
merged 1 commit into from
Jul 30, 2021

Conversation

lyrixx
Copy link
Member

@lyrixx lyrixx commented Jul 27, 2021

Q A
Branch? 5.4
Bug fix? yes (fix a new feature)
New feature? yes (fix a new feature)
Deprecations? no
Tickets Fix #42282
License MIT
Doc PR symfony/symfony-docs#15554

New usage:

    public function __construct(
        #[Context([Serializer::EMPTY_ARRAY_AS_OBJECT => true ])]
        public array $mapWithOption = [],
        #[Context([Serializer::EMPTY_ARRAY_AS_OBJECT => true ])]
        public array $mapWithOptionAndData = ['foo' => 'bar'],
        public array $mapWithoutOption = [],
        public array $mapWithoutOptionAndData = ['foo' => 'bar'],
    ) {
    }

=>

{"mapWithOption":{},"mapWithOptionAndData":{"foo":"bar"},"mapWithoutOption":[],"mapWithoutOptionAndData":{"foo":"bar"}}

@derrabus derrabus added this to the 5.4 milestone Jul 29, 2021
@lyrixx lyrixx force-pushed the serializer-empty branch 4 times, most recently from e6e2719 to 00e8270 Compare July 29, 2021 16:16
@lyrixx lyrixx changed the title [Serializer] Add support for serializing empty collection as object [Serializer] Add support for serializing empty array as object Jul 29, 2021
@lyrixx lyrixx force-pushed the serializer-empty branch from 8170507 to e5c8f14 Compare July 29, 2021 17:29
Copy link
Contributor

@Foxprodev Foxprodev left a comment

Choose a reason for hiding this comment

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

Great! Looks reasonable and correct for me now. Thanks for the time!

@derrabus
Copy link
Member

Please make fabbot happy.

@fabpot fabpot force-pushed the serializer-empty branch from e5c8f14 to c940b74 Compare July 30, 2021 10:32
@fabpot
Copy link
Member

fabpot commented Jul 30, 2021

Thank you @lyrixx.

@fabpot fabpot merged commit 62d4f44 into symfony:5.4 Jul 30, 2021
@lyrixx lyrixx deleted the serializer-empty branch July 30, 2021 10:37
This was referenced Nov 5, 2021
javiereguiluz added a commit to symfony/symfony-docs that referenced this pull request Aug 8, 2022
…ect in example (alanpoulain)

This PR was merged into the 5.4 branch.

Discussion
----------

[Serializer] fix(serializer): missing empty_array_as_object in example

This PR:
- Replaces `empty_iterable_as_object` by `preserve_empty_objects`, like it should be.
- Adds `empty_array_as_object` to example in Serializer Context.
- Removes the mention `by default`, since it's `false` by default.

Related:
- #15554
- #15580
- symfony/symfony#42240
- symfony/symfony#42297

Commits
-------

cc54517 fix(serializer): missing empty_array_as_object in example
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.

[Serializer] preserving empty object breaks current array lists
6 participants