Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

[Serializer] Add support for preserving empty object in object property#42240

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
fabpot merged 1 commit intosymfony:5.4fromlyrixx:serializer-map
Jul 25, 2021

Conversation

@lyrixx
Copy link
Member

QA
Branch?5.4
Bug fix?no
New feature?yes
Deprecations?
TicketsFix#38192
LicenseMIT
Doc PR

This PR leveragehttps://symfony.com/blog/new-in-symfony-5-3-inlined-serialization-context tofix#38192.

Example:

class MyDto{publicfunction__construct(        #[Context([AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS =>true ])]publicarray$mapWithOption = [],        #[Context([AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS =>true ])]publicarray$mapWithOptionAndData = ['foo' =>'bar'],publicarray$mapWithoutOption = [],publicarray$mapWithoutOptionAndData = ['foo' =>'bar'],    ) {    }}

Will produce:

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

dunglas reacted with heart emoji
Copy link
Member

@chalasrchalasr left a comment

Choose a reason for hiding this comment

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

👍

Copy link
Member

@NyholmNyholm left a comment

Choose a reason for hiding this comment

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

I read the PR title and I was mentally preparing for reviewing a 500+ lines PR of complicated Serializer code.

This looks great. Well done.

Maybe we should take a short moment to discuss the namePRESERVE_EMPTY_OBJECTS. Im not sure it is perfect, but I don't have any suggestions Im more comfortable with.
MaybeFORCE_EMPTY_ARRAYS_AS_OBJECT.. OrEMPTY_MAP_AS_OBJECT... hm..

Did you consider anything else before deciding onPRESERVE_EMPTY_OBJECTS?

dunglas reacted with thumbs up emoji
@lyrixx
Copy link
MemberAuthor

lyrixx commentedJul 25, 2021
edited
Loading

Did you consider anything else before deciding on PRESERVE_EMPTY_OBJECTS?

I did not introduce this constant. It exists for ages, with this exact purpose. 😁

dunglas reacted with thumbs up emoji

@Nyholm
Copy link
Member

Oh. Sorry. You are correct. Im all 👍🏽

@fabpot
Copy link
Member

Thank you@lyrixx.

@Foxprodev
Copy link
Contributor

Foxprodev commentedJul 26, 2021
edited
Loading

Well. It's weird, starting from 5.4 I have to move from

class A {    #[Context(normalizationContext: [AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS => true])]    public ArrayObject $map;    public function __construct()    {        $this->map = new ArrayObject();        $this->map->append(new B());    }}class B {    public array $list = [];    public array $anotherListMayBeEmpty;    public function __construct()    {        $this->anotherListMayBeEmpty[] = new C();    }}class C {    public ArrayObject $index;    public ArrayObject $settings;    public ArrayObject $andSoOnObjects;    public function __construct()    {        $this->index = new ArrayObject();        $this->settings = new ArrayObject();        $this->andSoOnObjects = new ArrayObject();    }}

to

class A {    #[Context(normalizationContext: [AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS => true])]    public ArrayObject $map;    public function __construct()    {        $this->map = new ArrayObject();        $this->map->append(new B());    }}class B {    #[Context(normalizationContext: [AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS => false])]    public array $list = [];    #[Context(normalizationContext: [AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS => false])]    public array $anotherListMayBeEmpty;    public function __construct()    {        $this->anotherListMayBeEmpty[] = new C();    }}class C {    #[Context(normalizationContext: [AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS => true])]    public ArrayObject $index;    #[Context(normalizationContext: [AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS => true])]    public ArrayObject $settings;    #[Context(normalizationContext: [AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS => true])]    public ArrayObject $andSoOnObjects;    public function __construct()    {        $this->index = new ArrayObject();        $this->settings = new ArrayObject();        $this->andSoOnObjects = new ArrayObject();    }}

Is it an expected complication?@lyrixx@chalasr@Nyholm
It was pretty expected behavior for me to separate list array's from indexed array's using OOP
That's whyPRESERVE_EMPTY_OBJECTS was introduced ignoring just empty arrays.

if (is_iterable($data)) {
if (($context[AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS] ??false) ===true &&$datainstanceof \Countable &&0 ===$data->count()) {
return$data;
}

This option is even namedPRESERVE notFORCE likeJSON_FORCE_OBJECT

P.S. I am using api-platform and there are so much serializeable nested classes in my projects...

@lyrixx
Copy link
MemberAuthor

I'll answer in the issue.

This was referencedNov 5, 2021
javiereguiluz added a commit to symfony/symfony-docs that referenced this pull requestAug 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 exampleThis 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#42297Commits-------cc54517 fix(serializer): missing empty_array_as_object in example
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@fabpotfabpotfabpot approved these changes

@NyholmNyholmNyholm approved these changes

@chalasrchalasrchalasr approved these changes

@dunglasdunglasAwaiting requested review from dunglasdunglas is a code owner

+1 more reviewer

@faizanakram99faizanakram99faizanakram99 approved these changes

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

[Serializer] Add a annotation (or something) to serialize empty collection as an object and not an array

7 participants

@lyrixx@Nyholm@fabpot@Foxprodev@chalasr@faizanakram99@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp