Skip to content

Commit db412fe

Browse files
amnenicolas-grekas
authored andcommitted
[Serializer] Rewrite AbstractObjectNormalizer::createChildContext() to use the provided cache_key from original context when creating child contexts
1 parent 840c5cc commit db412fe

File tree

2 files changed

+130
-1
lines changed

2 files changed

+130
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,11 @@ private function isMaxDepthReached(array $attributesMetadata, string $class, str
781781
protected function createChildContext(array $parentContext, string $attribute, ?string $format): array
782782
{
783783
$context = parent::createChildContext($parentContext, $attribute, $format);
784-
$context['cache_key'] = $this->getCacheKey($format, $context);
784+
if ($context['cache_key'] ?? false) {
785+
$context['cache_key'] .= '-'.$attribute;
786+
} else {
787+
$context['cache_key'] = $this->getCacheKey($format, $context);
788+
}
785789

786790
return $context;
787791
}

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

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,131 @@ public function testDenormalizeUntypedStringObject()
548548
$this->assertEquals(new DummyWithStringObject(new DummyString()), $actual);
549549
$this->assertEquals('', $actual->value->value);
550550
}
551+
552+
public function testProvidingContextCacheKeyGeneratesSameChildContextCacheKey()
553+
{
554+
$foobar = new Dummy();
555+
$foobar->foo = new EmptyDummy();
556+
$foobar->bar = 'bar';
557+
$foobar->baz = 'baz';
558+
$data = [
559+
'foo' => [],
560+
'bar' => 'bar',
561+
'baz' => 'baz',
562+
];
563+
564+
$normalizer = new class() extends AbstractObjectNormalizerDummy {
565+
public $childContextCacheKey;
566+
567+
protected function extractAttributes(object $object, string $format = null, array $context = []): array
568+
{
569+
return array_keys((array) $object);
570+
}
571+
572+
protected function getAttributeValue(object $object, string $attribute, string $format = null, array $context = [])
573+
{
574+
return $object->{$attribute};
575+
}
576+
577+
protected function createChildContext(array $parentContext, string $attribute, ?string $format): array
578+
{
579+
$childContext = parent::createChildContext($parentContext, $attribute, $format);
580+
$this->childContextCacheKey = $childContext['cache_key'];
581+
582+
return $childContext;
583+
}
584+
};
585+
586+
$serializer = new Serializer([$normalizer]);
587+
588+
$serializer->normalize($foobar, null, ['cache_key' => 'hardcoded', 'iri' => '/dummy/1']);
589+
$firstChildContextCacheKey = $normalizer->childContextCacheKey;
590+
591+
$serializer->normalize($foobar, null, ['cache_key' => 'hardcoded', 'iri' => '/dummy/2']);
592+
$secondChildContextCacheKey = $normalizer->childContextCacheKey;
593+
594+
$this->assertSame($firstChildContextCacheKey, $secondChildContextCacheKey);
595+
}
596+
597+
public function testChildContextKeepsOriginalContextCacheKey()
598+
{
599+
$foobar = new Dummy();
600+
$foobar->foo = new EmptyDummy();
601+
$foobar->bar = 'bar';
602+
$foobar->baz = 'baz';
603+
$data = [
604+
'foo' => [],
605+
'bar' => 'bar',
606+
'baz' => 'baz',
607+
];
608+
609+
$normalizer = new class() extends AbstractObjectNormalizerDummy {
610+
public $childContextCacheKey;
611+
612+
protected function extractAttributes(object $object, string $format = null, array $context = []): array
613+
{
614+
return array_keys((array) $object);
615+
}
616+
617+
protected function getAttributeValue(object $object, string $attribute, string $format = null, array $context = [])
618+
{
619+
return $object->{$attribute};
620+
}
621+
622+
protected function createChildContext(array $parentContext, string $attribute, ?string $format): array
623+
{
624+
$childContext = parent::createChildContext($parentContext, $attribute, $format);
625+
$this->childContextCacheKey = $childContext['cache_key'];
626+
627+
return $childContext;
628+
}
629+
};
630+
631+
$serializer = new Serializer([$normalizer]);
632+
$serializer->normalize($foobar, null, ['cache_key' => 'hardcoded', 'iri' => '/dummy/1']);
633+
634+
$this->assertSame('hardcoded-foo', $normalizer->childContextCacheKey);
635+
}
636+
637+
public function testChildContextCacheKeyStaysFalseWhenOriginalCacheKeyIsFalse()
638+
{
639+
$foobar = new Dummy();
640+
$foobar->foo = new EmptyDummy();
641+
$foobar->bar = 'bar';
642+
$foobar->baz = 'baz';
643+
$data = [
644+
'foo' => [],
645+
'bar' => 'bar',
646+
'baz' => 'baz',
647+
];
648+
649+
$normalizer = new class() extends AbstractObjectNormalizerDummy {
650+
public $childContextCacheKey;
651+
652+
protected function extractAttributes(object $object, string $format = null, array $context = []): array
653+
{
654+
return array_keys((array) $object);
655+
}
656+
657+
protected function getAttributeValue(object $object, string $attribute, string $format = null, array $context = [])
658+
{
659+
return $object->{$attribute};
660+
}
661+
662+
protected function createChildContext(array $parentContext, string $attribute, ?string $format): array
663+
{
664+
$childContext = parent::createChildContext($parentContext, $attribute, $format);
665+
$this->childContextCacheKey = $childContext['cache_key'];
666+
667+
return $childContext;
668+
}
669+
};
670+
671+
$serializer = new Serializer([$normalizer]);
672+
$serializer->normalize($foobar, null, ['cache_key' => false]);
673+
674+
$this->assertFalse($normalizer->childContextCacheKey);
675+
}
551676
}
552677

553678
class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer

0 commit comments

Comments
 (0)