-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Conversation
ffb4266
to
8bb0b30
Compare
@@ -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(); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Why not following the suggestion made by @meyerbaptiste in #23019 and use an |
889fa6a
to
a4d808f
Compare
Ok, At first I was thinking So I have basically extended encoders where needed to make sure they support encoding Also changed I think this is now completely BC:
|
7938c13
to
bfb55b4
Compare
@dunglas Can you review this one again? Thank you. |
There was a problem hiding this 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.
859eb91
to
d50a17b
Compare
src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php
Outdated
Show resolved
Hide resolved
8459f82
to
4341fea
Compare
I've love to see it merged, ill have to look at rebasing it i guess as it has some conflicts now. |
There was a problem hiding this 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.
src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php
Show resolved
Hide resolved
69e81e9
to
ee86eb0
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
src/Symfony/Component/Serializer/Normalizer/NormalizerInterface.php
Outdated
Show resolved
Hide resolved
@mcfedr Can you take my comment into account and rebase on current 4.4? That's the last steps before merging IMO. Thank you. |
ee86eb0
to
d458148
Compare
Allows Normalizers to return a representation of an empty object that the encoder recognizes as such.
d458148
to
f28e826
Compare
@fabpot All done! |
Thank you @mcfedr. |
…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
@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? |
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.