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

Properties with @Groups annotations included in output when no SerializationContext given. #1149

Closed
pdugas opened this issue Dec 16, 2019 · 2 comments

Comments

@pdugas
Copy link

pdugas commented Dec 16, 2019

Q A
Bug report? Yes
Feature request? No
BC Break report? No
RFC? No

Steps required to reproduce the problem

  1. Define a class with one or more properties with no annotations and one or more with @Groups.
use JMS\Serializer\Annotation\Groups;

class Eg
{   
    private $id = 1;

    /** @Groups({"WithFoo"}) */
    private $foo = 'foo';
}
  1. Serialize an instance of the class without specifying a SerializationContext.
$this->assertSame(['id'=>1], json_decode($serializer->serialize(new Eg(), 'json')));
  1. Check the output.
Failed asserting that two arrays are identical.
--- Expected
+++ Actual
@@ @@
 Array &0 (
     'id' => 1
+    'foo' => 'foo'
 )

Expected Result

  • The foo property should not have been included in the result.

Actual Result

  • See above. foo property is incorrectly included.
  • I can make the test pass by explicitly adding the Default group.
$this->assertSame(
    ['id'=>1], 
    json_decode($serializer->serialize(
        new Eg(), 'json',
        SerializationContext::create()->setGroups(['Default'])
    ))
);

The way I read the documentation, only the Default group should be serialized by default. Am I wrong?

@goetas
Copy link
Collaborator

goetas commented Dec 17, 2019

If you don't pass to the context some groups, the serialization groups exclusion strategy is not used at all, thus all props are serialized

@goetas goetas closed this as completed Dec 17, 2019
@pdugas
Copy link
Author

pdugas commented Dec 23, 2019

Got it @goetas. I've adjusted my usage so I always specify the Default group when serializing the returned data from my API and optionally add groups like "withfoo" and "withbar" based on a query parameter from the client. Works great. Thanks.

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

No branches or pull requests

2 participants