Do not cache cache attributes if attributes is in the context

This commit is contained in:
Samuel ROZE 2017-11-28 09:21:56 +00:00
parent b568e16d8f
commit 6e87382bfa
No known key found for this signature in database
GPG Key ID: 835426F55A19FB84
2 changed files with 35 additions and 0 deletions

View File

@ -126,6 +126,10 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
return $allowedAttributes;
}
if (isset($context['attributes'])) {
return $this->extractAttributes($object, $format, $context);
}
if (isset($this->attributesCache[$class])) {
return $this->attributesCache[$class];
}

View File

@ -713,6 +713,37 @@ class ObjectNormalizerTest extends TestCase
'inner' => array('foo' => 'foo', 'bar' => 'bar'),
), DummyWithConstructorObjectAndDefaultValue::class, null, $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(
'attributes' => array(
'foo' => array('bar'),
'bar' => array('foo'),
),
)));
}
}
class ObjectDummy