Skip to content

[Serializer] Rewrite AbstractObjectNormalizer::createChildContext() to use the provided cache_key from original context when creating child contexts #53530

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 1 commit into from
Jan 29, 2024
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 @@ -781,7 +781,11 @@ 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 ($context['cache_key'] ?? false) {
$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 @@ -548,6 +548,131 @@ public function testDenormalizeUntypedStringObject()
$this->assertEquals(new DummyWithStringObject(new DummyString()), $actual);
$this->assertEquals('', $actual->value->value);
}

public function testProvidingContextCacheKeyGeneratesSameChildContextCacheKey()
{
$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 $childContextCacheKey;

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 = [])
{
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]);

$serializer->normalize($foobar, null, ['cache_key' => 'hardcoded', 'iri' => '/dummy/1']);
$firstChildContextCacheKey = $normalizer->childContextCacheKey;

$serializer->normalize($foobar, null, ['cache_key' => 'hardcoded', 'iri' => '/dummy/2']);
$secondChildContextCacheKey = $normalizer->childContextCacheKey;

$this->assertSame($firstChildContextCacheKey, $secondChildContextCacheKey);
}

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 $childContextCacheKey;

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 = [])
{
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]);
$serializer->normalize($foobar, null, ['cache_key' => 'hardcoded', 'iri' => '/dummy/1']);

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

public function testChildContextCacheKeyStaysFalseWhenOriginalCacheKeyIsFalse()
{
$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 $childContextCacheKey;

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 = [])
{
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]);
$serializer->normalize($foobar, null, ['cache_key' => false]);

$this->assertFalse($normalizer->childContextCacheKey);
}
}

class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer
Expand Down