-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
96af30f
[Serializer] Rewrite `AbstractObjectNormalizer::createChildContext()`…
amne 72aac24
changed assert approach for childContextCacheKey
amne 7a28a29
reverted to inline ifs before calling getCacheKey
amne 765d276
reverted unintended code style change
amne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)); | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function denormalize(mixed $data, string $type, string $format = null, array $context = []) | ||
{ | ||
if (!isset($context['cache_key'])) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
|
@@ -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; | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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([ | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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