8000 Do not cache cache attributes if `attributes` is in the context · symfony/symfony@303280d · GitHub
[go: up one dir, main page]

Skip to content

Commit 303280d

Browse files
committed
Do not cache cache attributes if attributes is in the context
1 parent b568e16 commit 303280d

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ protected function getAttributes($object, $format = null, array $context)
126126
return $allowedAttributes;
127127
}
128128

129-
if (isset($this->attributesCache[$class])) {
129+
if (isset($this->attributesCache[$class]) && !isset($context['attributes'])) {
130130
return $this->attributesCache[$class];
131131
}
132132

src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,37 @@ public function testAttributesContextDenormalizeConstructor()
713713
'inner' => array('foo' => 'foo', 'bar' => 'bar'),
714714
), DummyWithConstructorObjectAndDefaultValue::class, null, $context));
715715
}
716+
717+
public function testNormalizeSameObjectWithDifferentAttributes()
718+
{
719+
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
720+
$this->normalizer = new ObjectNormalizer($classMetadataFactory);
721+
$serializer = new Serializer(array($this->normalizer));
722+
$this->normalizer->setSerializer($serializer);
723+
724+
$dummy = new ObjectOuter();
725+
$dummy->foo = new ObjectInner();
726+
$dummy->foo->foo = 'foo.foo';
727+
$dummy->foo->bar = 'foo.bar';
728+
729+
$dummy->bar = new ObjectInner();
730+
$dummy->bar->foo = 'bar.foo';
731+
$dummy->bar->bar = 'bar.bar';
732+
733+
$this->assertEquals(array(
734+
'foo' => array(
735+
'bar' => 'foo.bar',
736+
),
737+
'bar' => array(
738+
'foo' => 'bar.foo',
739+
),
740+
), $this->normalizer->normalize($dummy, 'json', array(
741+
'attributes' => array(
742+
'foo' => array('bar'),
743+
'bar' => array('foo'),
744+
),
745+
)));
746+
}
716747
}
717748

718749
class ObjectDummy

0 commit comments

Comments
 (0)
0