8000 [Serializer] Rewrite `AbstractObjectNormalizer::createChildContext()` to use the provided `cache_key` from original context by amne · Pull Request #53523 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] Rewrite AbstractObjectNormalizer::createChildContext() to use the provided cache_key from original context #53523

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

Closed
wants to merge 4 commits into from
Closed
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 @@ -302,9 +302,6 @@ public function supportsDenormalization(mixed $data, string $type, string $forma
return class_exists($type) || (interface_exists($type, false) && null !== $this->classDiscriminatorResolver?->getMappingForClass($type));
}

/**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change should be reverted

* @return mixed
*/
public function denormalize(mixed $data, string $type, string $format = null, array $context = [])
{
if (!isset($context['cache_key'])) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

Expand Down Expand Up @@ -734,7 +731,13 @@ private function isMaxDepthReached(array $attributesMetadata, string $class, str
protected function createChildContext(array $parentContext, string $attribute, ?string $format): array
{
$context = parent::createChildContext($parentContext, $attribute, $format);
$context['cache_key'] = $this->getCacheKey($format, $context);
if (isset($context['cache_key'])) {
if (false !== $context['cache_key']) {
$context['cache_key'] .= '-'.$attribute;
}
} else {
$context['cache_key'] = $this->getCacheKey($format, $context);
}

return $context;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,46 @@ public function supportsNormalization(mixed $data, string $format = null, array
$this->assertSame('called', $object->bar);
}

public function testChildContextKeepsOriginalContextCacheKey()
{
$foobar = new Dummy();
$foobar->foo = new EmptyDummy();
$foobar->bar = 'bar';
$foobar->baz = 'baz';
$data = [
'foo' => [],
'bar' => 'bar',
'baz' => 'baz',
];

$normalizer = new class() extends AbstractObjectNormalizerDummy {
public ?string $childContextCacheKey = null;

protected function extractAttributes(object $object, string $format = null, array $context = []): array
{
return array_keys((array) $object);
}

protected function getAttributeValue(object $object, string $attribute, string $format = null, array $context = []): mixed
{
return $object->{$attribute};
}

protected function createChildContext(array $parentContext, string $attribute, ?string $format): array
{
$childContext = parent::createChildContext($parentContext, $attribute, $format);
$this->childContextCacheKey = $childContext['cache_key'];

return $childContext;
}
};

$serializer = new Serializer([$normalizer]);
$normalizedObject = $serializer->normalize($foobar, null, ['cache_key' => 'hardcoded']);
// $this->assertSame($data, $normalizedObject);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unexpected comment I guess

$this->assertSame('hardcoded-foo', $normalizer->childContextCacheKey);
}

public function testDenormalizeUnionOfEnums()
{
$serializer = new Serializer([
Expand Down
0