10000 [Serializer] Do not cache attributes if `attributes` in context by sroze · Pull Request #25185 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] Do not cache attributes if attributes in context #25185

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ protected function getAttributes($object, $format = null, array $context)
return $allowedAttributes;
}

if (isset($context['attributes'])) {
return $this->extractAttributes($object, $format, $context);
}

if (isset($this->attributesCache[$class])) {
return $this->attributesCache[$class];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,37 @@ public function testAttributesContextDenormalizeConstructor()
'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
Expand Down
0