8000 [Serializer] Object attributes are cached when there are attributes and groups fields in context · Issue #25444 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
[Serializer] Object attributes are cached when there are attributes and groups fields in context #25444
Closed
@baptadn

Description

@baptadn
Q A
Bug report yes
Feature request no
BC Break report no
RFC no
Symfony version 3.3.14

Issue related to #25185

With group context filled, attributes are cached even if there is the attributes within context. Steps to reproduce (based on this test):

Add groups annotations:

class ObjectOuter
{
    /**
     * @Groups({"api"})
     */
    public $foo;

    /**
     * @Groups({"api"})
     */
    public $bar;
}

class ObjectInner
{
    /**
     * @Groups({"api"})
     */
    public $foo;

    /**
     * @Groups({"api"})
     */
    public $bar;
}

Add groups within context:

public function testNormalizeSameObjectWithDifferentAttributes()
    {
        $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
        $this->normalizer = new ObjectNormalizer($classMetadataFactory);
        $serializer = new Serializer(array($this->normalizer));
        $this->normalizer->setSerializer($serializer);

        $dummy = new ObjectOuter();
        $dummy->foo = new ObjectInner();
        $dummy->foo->foo = 'foo.foo';
        $dummy->foo->bar = 'foo.bar';

        $dummy->bar = new ObjectInner();
        $dummy->bar->foo = 'bar.foo';
        $dummy->bar->bar = 'bar.bar';

        $this->assertEquals(array(
            'foo' => array(
                'bar' => 'foo.bar',
            ),
            'bar' => array(
                'foo' => 'bar.foo',
            ),
        ), $this->normalizer->normalize($dummy, 'json', array(
            'groups' => ['api'],
            'attributes' => array(
                'foo' => array('bar'),
                'bar' => array('foo'),
            ),
        )));
    }

Result:

1) Symfony\Component\Serializer\Tests\Normalizer\ObjectNormalizerTest::testNormalizeSameObjectWithDifferentAttributes
Failed asserting that two arrays are equal.
--- Expected
+++ Actual
@@ @@
 Array (
     'foo' => Array (...)
     'bar' => Array (
-        'foo' => 'bar.foo'
+        'bar' => 'bar.bar'
     )
 )

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0