-
-
Notifications
You must be signed in to change notification settings - Fork 586
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
Behavior serializeNull -> not always honored in 2.* (but was in 1.*) #1101
Comments
Hi, Thanks for reporting this. I'm talking as example this, and the 2.x or 3.x version should look like: use JMS\Serialzier\Exception\NotAcceptableException
// ...
function serializeExtReferenceToJson(
JsonSerializationVisitor $visitor,
ExtReference $extReference,
array $type,
Context $context
) {
if (null === $extReference && !$context->shouldSerializeNull()) {
throw new NotAcceptableException();
} elseif (null === $extReference) {
return $visitor->visitNull(null, $type, $context);
}
try {
//return $visitor->visitString($this->converter->getUrl($extReference), $type, $context);
return $this->converter->getUrl($extReference);
} catch (\InvalidArgumentException $e) {
if (!$context->shouldSerializeNull()) {
throw new NotAcceptableException();
} else {
return $visitor->visitNull(null, $type, $context);
}
}
} Same check should be done for the 'EmptyType' handler. |
In the next days will update the UPGRADING document |
@goetas Hm, OK, I see, the If you don't mind me asking, I'm still confused how the rendering of empty arrays has changed from 1.*. In 1.*, those were always rendered (empty arrays deep in the object graph).. In newer versions, it is a kinda mixed bag. "General" serialized empty arrays are not rendered anymore. But in the context of I updated my example with your fix and it to render an empty array ( It's not really a bug, but the behavior is a bit inconsistent - any way possibly that Or in short: Why is an |
In 2.x arrays have been refactored and the ability to explicitly decide is rendering an empty array has been added via @SkipWhenEmpty annotation.
If the changes done to the array handling are inconsistent in your opinion, feel free to submit a failing test case with the expected behavior. Will be happy to check it and see what can be done to make it more consistent. 😃 |
Background information
Hi guys..
Over at libgraviton/graviton#720 we are still trying to migrate from serializer 1.* to 2.*. Our project is a JSON REST backend server, so serializer output is paramount for us. We don't want to change what we output after the change to Serializer version update.
We have two main edge case requirements when rendering:
{}
null
sIn Serializer 1.*,
serializeNull
is honored all the time - this seems to have changed in 2.*.With 1.*, empty objects still were rendered sometimes, but we worked round that by having special
Handler
s that the serialize tonull
- and that didn't render. This doesn't work anymore in 2.*.Steps required to reproduce the problem
As we use the Serializer in a quite complex configuration, I created a demonstration repository that shows our problem isolated.
Please clone https://github.com/narcoticfresh/serializer2-problem-testcase
Run
composer install
, then runphp test.php
What you will see:
{"members":[{"name":"member1"},{}]}
or{"members":[{"name":"member1"},null]}
Why is the empty object (or
null
) rendered?Expected Result
As in serializer 1.*, we expect that
null
s whenserializeNull
isfalse
So if we have this setup
pre_serialize
stage =>EmptyType
is a handler that serializes tonull
null
value rendered now in version 2.0?Actual Result
null
s are renderedThe text was updated successfully, but these errors were encountered: