serializer icon indicating copy to clipboard operation
serializer copied to clipboard

[WIP] Allow deserializing null

Open goetas opened this issue 6 years ago • 16 comments

Q A
Bug fix? no
New feature? yes
Doc updated no
BC breaks? yes
Deprecations? no
Tests pass? yes
Fixed tickets -
License MIT

Alternative implementation of https://github.com/schmittjoh/serializer/pull/1004

goetas avatar Oct 31 '18 11:10 goetas

The main trick here are the interaction between the changes in the graph navigator and the visitor

goetas avatar Oct 31 '18 12:10 goetas

The only problem I see with this that it will make all fields in the request deserialize null, not sure if that is a good thing.

kunicmarko20 avatar Nov 01 '18 00:11 kunicmarko20

@kunicmarko20 I've found some time to work on this. Te current approach allows to set the property type as nullable (like php 7.4 typed props).

Something as:


class BlogPost
{
    /**
     * @Type("?string")
     */
    private $id = 'what_a_nice_id';

    /**
     * @Type("?DateTime")
     */
    private $createdAt;

    /**
     * @Type("?array<JMS\Serializer\Tests\Fixtures\Comment>")
     */
    private $comments2;

    /**
     * @Type("array<string,?string>")
     */
    private $metadata;
}

When ? is used and setDeserializeNull is specified, then null properties are deserialized as null instead of having the default object value.

goetas avatar May 02 '19 21:05 goetas

Well, this looks amazing. Now you can choose to deserialize null for everything or per prop?

kunicmarko20 avatar May 03 '19 08:05 kunicmarko20

If $context->shouldDeserializeNull() is not called, ? is just ignored. When $context->shouldDeserializeNull() is called then the props having ? in the type declaration will be null if the JSON contains null.

goetas avatar May 03 '19 08:05 goetas

ah, so you need to enable it and then select which properties this works on?

kunicmarko20 avatar May 03 '19 09:05 kunicmarko20

ah, so you need to enable it and then select which properties this works on?

yes. main reason backward compatibility.

Thinking it twice, having ? could be enough, so shouldDeserializeNull() could be not needed....

just trying to understand if there might have other implications...

goetas avatar May 03 '19 09:05 goetas

Ye, it looks like shouldDeserializeNull is not needed anymore since you need to add ? to your configuration and that should be BC?

kunicmarko20 avatar May 03 '19 09:05 kunicmarko20

Ye, it looks like shouldDeserializeNull is not needed anymore since you need to add ? to your configuration and that should be BC?

No, the ? is not a BC break. This solution is good enough IMO. I still need to "let some time pass" to see if there is something I'might have missed, since this was the result of some "late night" coding :smile:

goetas avatar May 03 '19 09:05 goetas

What could be an issue or a problem with merging this? Also the target doesn't have to be master anymore

kunicmarko20 avatar May 03 '19 09:05 kunicmarko20

Also the target doesn't have to be master anymore

What do you mean?

goetas avatar May 03 '19 09:05 goetas

Well, it is not a BC break, so no need to merge it on master? (assuming master is next major and not minor?)

kunicmarko20 avatar May 03 '19 10:05 kunicmarko20

The master branch is targeting 3.1 and not 4.x.

There are not yet any plans for a new major, so I'm keeping one branch less to manage

goetas avatar May 03 '19 10:05 goetas

My bad, then we just wait. :smile:

kunicmarko20 avatar May 03 '19 10:05 kunicmarko20

I would guess some time is passed now :)

pscheit-lillydoo avatar Mar 02 '21 18:03 pscheit-lillydoo

Any plans to release this?

scaytrase avatar Sep 10 '21 13:09 scaytrase