Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 020d78a

Browse filesBrowse files
committed
bug #25185 [Serializer] Do not cache attributes if attributes in context (sroze)
This PR was merged into the 3.3 branch. Discussion ---------- [Serializer] Do not cache attributes if `attributes` in context | Q | A | ------------- | --- | Branch? | 3.3 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #25108 | License | MIT | Doc PR | ø Caching attributes based on the class works only when these attributes are not overwritten. This disables the cache when they are. To me, this `extractAttributes` method should actually be a `AttributeResolver` dependency that can be decorated using different caching strategies I'd say but... that's a much bigger refactoring that needs more reflection with @dunglas. Commits ------- 6e87382 Do not cache cache attributes if `attributes` is in the context
2 parents b568e16 + 6e87382 commit 020d78a
Copy full SHA for 020d78a

File tree

2 files changed

+35
-0
lines changed
Filter options

2 files changed

+35
-0
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ protected function getAttributes($object, $format = null, array $context)
126126
return $allowedAttributes;
127127
}
128128

129+
if (isset($context['attributes'])) {
130+
return $this->extractAttributes($object, $format, $context);
131+
}
132+
129133
if (isset($this->attributesCache[$class])) {
130134
return $this->attributesCache[$class];
131135
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php
+31Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.