Skip to content
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

852 - setSerialiseNull(true) + exclusion strategies still include data #886

Conversation

fsevestre
Copy link

@fsevestre fsevestre commented Mar 9, 2018

Q A
Bug fix? yes
New feature? no
Doc updated no
BC breaks? no
Deprecations? no
Tests pass? wip
Fixed tickets #852 #821
License Apache-2.0

First commit: the failing test.
Second commit: a fix.

Given an array containing an object,
when I use an exclusion strategy to exclude this object,
I shouln't get any data about this object after serialization
even if I define null values to be serialize.

@fsevestre fsevestre force-pushed the 852-serialise-null-with-exclusion-strategy branch from 2c99b3e to 3281391 Compare March 22, 2018 19:06
@fsevestre fsevestre force-pushed the 852-serialise-null-with-exclusion-strategy branch from 3281391 to 20938d9 Compare March 22, 2018 19:18
@fsevestre
Copy link
Author

fsevestre commented Mar 22, 2018

Just updated the first (better example and same set of tests for setSerializeNull(true) and setSerializeNull(false) and second commits.

The tests pass now, but I'm not sure that the way I check if the exclusion rule has skipped the class is good.

Any help with this will be well appreciated.

@goetas
Copy link
Collaborator

goetas commented Mar 23, 2018

Thanks a lot for the PR. This was a long-standing feature request and I'm interested in merging it.

Im just afraid this could be considered a BC break. Is it? Should we have a "flag" to disable the serialization of null values inside a collection?

@goetas goetas self-assigned this Mar 23, 2018
@goetas goetas added this to the v1.12 milestone Mar 23, 2018
@fsevestre
Copy link
Author

Personally, I consider this as a bug fix since the BC break was added in 1.4 and it broke my code when I tried to upgrade an old app.
But the bug was introduced in October 2016 so it may indeed break newer applications (I don't know in which case but it can be possible I think).

If you think it's safer to add a way to enable this change I will add it.


if (null === $v && $context->shouldSerializeNull() !== true) {
if (null === $val && ($context->shouldSerializeNull() !== true
|| (is_object($v) && null !== $context->getExclusionStrategy() && $context->getMetadataStack()->isEmpty()))
Copy link
Collaborator

Choose a reason for hiding this comment

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

can you explain me why (is_object($v) && null !== $context->getExclusionStrategy() && $context->getMetadataStack()->isEmpty()) is necessary? isnt (null === $v && $context->shouldSerializeNull() !== true) enough?

Copy link
Author

Choose a reason for hiding this comment

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

In fact, it's the $context->shouldSerializeNull() !== true condition the problem because I want null values to be serialized (->setSerializeNull(true)) but not objects serialized to null using an exclusion strategy.

Copy link
Author

Choose a reason for hiding this comment

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

But as I said in my comment on the PR, I'm not sure that is_object($v) && null !== $context->getExclusionStrategy() && $context->getMetadataStack()->isEmpty() is the right check to do. I came to this condition because I went in the GraphNavigator class and saw this https://github.com/schmittjoh/serializer/blob/master/src/JMS/Serializer/GraphNavigator.php#L222-L228
But in case of some refactoring, the condition might be wrong :/

Copy link
Collaborator

Choose a reason for hiding this comment

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

To me makes much more sense to not serialize nulls, independently from the reason why they are null (simple nulls or exclusion strategies).

What do you think

Copy link
Author

@fsevestre fsevestre Mar 23, 2018

Choose a reason for hiding this comment

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

In my case it will be complicated because it will break my clients' fronts because they don't check for the key's existence but only the value :/

--

Maybe it's more a "returned value" issue, which shouldn't be considered as the same null value ?

I may be wrong (I don't work a lot with serializers) but in my mind array(new Object(toSkip), new Object(doNotSkip)) (with an exclusion strategy removing the first object) and array(new Object(doNotSkip)) should return the same result. FYI it's not totally the case anyway because of the array key: in the first example, the second object will have the key 1 and in the second the key 0 in the json/yaml/... result.

--

Just to add my use case, we generate a big tree (no limit of level, depends on the configuration) of objects, do lots of things on it, and update a display (bool) property which is used in an exclusion strategy to skip the class (shouldSkipClass) with display at false and remove the property (shouldSkipProperty).
All the objects are very important during the process (even the ones that are supposed to be hidden) but we don't want to return some of them (display false) through the API because it's a business requirement.

Copy link
Collaborator

Choose a reason for hiding this comment

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

hmmm, will have to think about it 😟

@goetas goetas removed this from the v1.12 milestone Mar 23, 2018
@goetas goetas added this to the v2.0 milestone Apr 5, 2018
@goetas
Copy link
Collaborator

goetas commented Apr 15, 2018

Fixed in 94c319d

@goetas goetas closed this Apr 15, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants