0

So I have a response class like this (simplified snippet):

/**
 * @Serializer\ExclusionPolicy("all")
 */
class MyResponse
{
    /**
     * @Serializer\Expose
     * @Serializer\Groups({"myGroup"})
     * @Serializer\Accessor("getMyName")
     */
    public $name;

    public function getMyName()
    {
        return 'Donald';
    }
}

The exclusion policy excludes all fields from being serialized unless they're explicitly exposed with @Serializer\Expose and the data group is a match.

This is working perfectly for all fields, except those with the @Serializer\Accessor annotation. These fields are always being included, regardless of group policy. I can't find anything in the docs or any other reports to suggest that the @Serializer\Accessor annotation overrides group policy, nor any way to manually check the data group within the accessor function.

Is there a way to accomplish this, or will I have to inline all my accessor functions in the response constructor? I was trying to avoid having a huge 100L+ constructor function, but it defeats the point of using the accessor annotation if I can't exclude certain fields...

Chris
  • 58
  • 6
  • Have you tried using `$serializer->serialize(new MyResponse(), 'json', \JMS\Serializer\SerializationContext::create()->setGroups(array('Default')));` ? – tftd Jul 24 '17 at 08:34

1 Answers1

0

I never used Accessor annotation so I don't know the reason why it's not working, but I could suggest you to put annotations directly on functions marking them as virtual properties:

/**
 * @Serializer\ExclusionPolicy("all")
 */
class MyResponse
{
    public $name;

    /**
     * @Serializer\VirtualProperty()
     * @Serializer\SerializedName("name")
     * @Serializer\Groups({"myGroup"})
     */
    public function getMyName()
    {
        return 'Donald';
    }
}
DrKey
  • 3,365
  • 2
  • 29
  • 46