Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Serializer] Encode empty objects as objects, not arrays #28363

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
Aug 13, 2019

Conversation

mcfedr
Copy link
Contributor

@mcfedr mcfedr commented Sep 4, 2018

Allows Normalizers to return a representation of an empty object that the encoder recognizes as such.

Often PHP code is relaxed about the difference betweens arrays and objects, and particularly empty arrays are ambiguous. This preserves objects that would otherwise have turned into arrays as objects in the encoding.

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? No
Deprecations? No
Tests pass? yes
Fixed tickets #23019
License MIT
Doc PR I'll do it if/when this might be merged

@@ -119,6 +120,10 @@ public function normalize($object, $format = null, array $context = array())
$data = $this->updateData($data, $attribute, $this->serializer->normalize($attributeValue, $format, $this->createChildContext($context, $attribute)));
}

if (!\count($data)) {
return new EmptyObject();
Copy link
Member

Choose a reason for hiding this comment

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

I fear that it's a BC break: code may rely on the old behavior and not except that this custom object is returned.
A solution may be to make this behavior optin through a context option.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I like the idea of using a context option to avoid the BC. I think its only an issue for custom Encoders - but it does seem like a bug fix, it was certainly unexpected behaviour for me when trying to get {} in my JSON.

@dunglas
Copy link
Member

dunglas commented Sep 5, 2018

Why not following the suggestion made by @meyerbaptiste in #23019 and use an ArrayObject instead of the custom EmptyObject class?
Also, the NormalizerInterface must be updated to document that returning something else than an array may be returned.

@mcfedr mcfedr force-pushed the serialize-empty-object branch 2 times, most recently from 889fa6a to a4d808f Compare September 5, 2018 08:41
@mcfedr
Copy link
Contributor Author

mcfedr commented Sep 5, 2018

Ok, At first I was thinking ArrayObject is supposed to be an object representation of an array, but actually I see this makes sense as its more an implementation of array features but for objects.

So I have basically extended encoders where needed to make sure they support encoding ArrayObject in general.

Also changed Normalizer to allow returning ArrayObject - currently used only for empty objects, so they stay as objects. They only do this behind the context flag array_object for BC.

I think this is now completely BC:

  • Encoders additionally support encoding ArrayObject
  • Normalizers only change output with array_object flag.

@mcfedr mcfedr force-pushed the serialize-empty-object branch 2 times, most recently from 7938c13 to bfb55b4 Compare September 5, 2018 15:18
@nicolas-grekas nicolas-grekas added this to the next milestone Sep 6, 2018
@fabpot
Copy link
Member

fabpot commented Oct 10, 2018

@dunglas Can you review this one again? Thank you.

Copy link
Member

@dunglas dunglas left a comment

Choose a reason for hiding this comment

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

Almost good on my side! Only small changes left.

@mcfedr mcfedr force-pushed the serialize-empty-object branch 2 times, most recently from 859eb91 to d50a17b Compare October 12, 2018 11:33
@mcfedr mcfedr force-pushed the serialize-empty-object branch 3 times, most recently from 8459f82 to 4341fea Compare October 23, 2018 12:39
@mcfedr
Copy link
Contributor Author

mcfedr commented Feb 27, 2019

I've love to see it merged, ill have to look at rebasing it i guess as it has some conflicts now.

Copy link
Contributor

@dbu dbu left a comment

Choose a reason for hiding this comment

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

looks like an important feature to have when building rest apis that non-php clients should be able to consume.

@mcfedr mcfedr force-pushed the serialize-empty-object branch 2 times, most recently from 69e81e9 to ee86eb0 Compare May 24, 2019 09:37
@mcfedr
Copy link
Contributor Author

mcfedr commented May 24, 2019

I've rebased this on master, and made changes suggested by @dbu, I think should be good now, @fabpot

Copy link
Member

@fabpot fabpot left a comment

Choose a reason for hiding this comment

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

LGTM

@fabpot
Copy link
Member

fabpot commented Aug 9, 2019

@mcfedr Can you take my comment into account and rebase on current 4.4? That's the last steps before merging IMO. Thank you.

@mcfedr mcfedr force-pushed the serialize-empty-object branch from ee86eb0 to d458148 Compare August 12, 2019 13:05
Allows Normalizers to return a representation of an empty object that the encoder recognizes as such.
@mcfedr mcfedr force-pushed the serialize-empty-object branch from d458148 to f28e826 Compare August 12, 2019 13:06
@mcfedr mcfedr changed the base branch from master to 4.4 August 12, 2019 13:07
@mcfedr
Copy link
Contributor Author

mcfedr commented Aug 12, 2019

@fabpot All done!

@fabpot
Copy link
Member

fabpot commented Aug 13, 2019

Thank you @mcfedr.

@fabpot fabpot merged commit f28e826 into symfony:4.4 Aug 13, 2019
fabpot added a commit that referenced this pull request Aug 13, 2019
…ys (mcfedr)

This PR was merged into the 4.4 branch.

Discussion
----------

[Serializer] Encode empty objects as objects, not arrays

Allows Normalizers to return a representation of an empty object that the encoder recognizes as such.

Often PHP code is relaxed about the difference betweens arrays and objects, and particularly empty arrays are ambiguous. This preserves objects that would otherwise have turned into arrays as objects in the encoding.

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | No
| Deprecations? | No
| Tests pass?   | yes
| Fixed tickets | #23019
| License       | MIT
| Doc PR        | I'll do it if/when this might be merged

Commits
-------

f28e826 [Serializer] Encode empty objects as objects, not arrays
@mcfedr mcfedr deleted the serialize-empty-object branch August 13, 2019 07:29
@mcfedr
Copy link
Contributor Author

mcfedr commented Sep 26, 2019

@dunglas I'm thinking of making a PR to make this the default behaviour in Symfony 5, and deprecate Symfony for 4.4, do you think that would be a good change?

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