JMSSerializerBundle icon indicating copy to clipboard operation
JMSSerializerBundle copied to clipboard

Exclude annotation does not work as documented for private or protected properties

Open ianef opened this issue 2 years ago • 0 comments

| Bug report? | yes

According to the documentation in the exclusion strategies following should work: http://jmsyst.com/libs/serializer/master/cookbook/exclusion_strategies#dynamic-exclusion-strategy

    /**
     * @Exclude(if="null")
     */
    private ?string $name;

It doesn't, it fails with an exception stating that a boolean was expected but found null.

If you try and use the private or protected property that also fails with access rights issues:

    /**
     * @Exclude(if="id==null")
     */

You can use the 'object' as a reference to call a getter method which works for serialisation as follows:

    /**
     * @Exclude(if="object.getName()==null")
     */

But this then fails when de-serialising because the 'object' does not yet exist at decision time. You can overcome it by adding a direction test but this is getting somewhat ridiculous and not at all a nice and straightforward to exclude a property.

    /**
     * @Serialize\Exclude(if="context.getDirection() === constant('JMS\\Serializer\\GraphNavigatorInterface::DIRECTION_SERIALIZATION') && object.getName()==null")
     */

I'm using jms/serializer-bundle 4.0.2 Symfony 3.4.49 (yes I know, it's currently being upgraded!) PHP 7.4.27

ianef avatar May 11 '22 17:05 ianef