Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7b64d88

Browse filesBrowse files
committed
[Serializer] Respect ignored attributes and camelcased attributes in cache key and update cache_key in nested structures
1 parent 69058e3 commit 7b64d88
Copy full SHA for 7b64d88

File tree

2 files changed

+35
-11
lines changed
Filter options

2 files changed

+35
-11
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php
+25-11Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function supportsNormalization($data, $format = null)
5858
public function normalize($object, $format = null, array $context = [])
5959
{
6060
if (!isset($context['cache_key'])) {
61-
$context['cache_key'] = $this->getCacheKey($format, $context);
61+
$context['cache_key'] = $this->getAttributeCacheKey($format, $context);
6262
}
6363

6464
if ($this->isCircularReference($object, $context)) {
@@ -128,15 +128,13 @@ protected function getAttributes($object, $format = null, array $context)
128128
return $allowedAttributes;
129129
}
130130

131-
if (isset($context['attributes'])) {
132-
return $this->extractAttributes($object, $format, $context);
133-
}
131+
$attributes = $this->extractAttributes($object, $format, $context);
134132

135-
if (isset($this->attributesCache[$class])) {
136-
return $this->attributesCache[$class];
133+
if ($context['cache_key']) {
134+
$this->attributesCache[$key] = $attributes;
137135
}
138136

139-
return $this->attributesCache[$class] = $this->extractAttributes($object, $format, $context);
137+
return $attributes;
140138
}
141139

142140
/**
@@ -176,7 +174,7 @@ public function supportsDenormalization($data, $type, $format = null)
176174
public function denormalize($data, $class, $format = null, array $context = [])
177175
{
178176
if (!isset($context['cache_key'])) {
179-
$context['cache_key'] = $this->getCacheKey($format, $context);
177+
$context['cache_key'] = $this->getAttributeCacheKey($format, $context);
180178
}
181179

182180
$allowedAttributes = $this->getAllowedAttributes($class, $context, true);
@@ -373,17 +371,33 @@ private function isMaxDepthReached(array $attributesMetadata, $class, $attribute
373371
}
374372

375373
/**
376-
* Gets the cache key to use.
374+
* Overwrite to update the cache key for the child.
375+
*
376+
* {@inheritdoc}
377+
*/
378+
protected function createChildContext(array $parentContext, $attribute)
379+
{
380+
$context = parent::createChildContext($parentContext, $attribute);
381+
// format is already included in the cache_key of the parent.
382+
$context['cache_key'] = $this->getAttributeCacheKey('', $context);
383+
384+
return $context;
385+
}
386+
387+
/**
388+
* Build the cache key for the attributes cache.
389+
*
390+
* The key must be different for every option in the context that could change which attributes should be handled.
377391
*
378392
* @param string|null $format
379393
* @param array $context
380394
*
381395
* @return bool|string
382396
*/
383-
private function getCacheKey($format, array $context)
397+
private function getAttributeCacheKey($format, array $context)
384398
{
385399
try {
386-
return md5($format.serialize($context));
400+
return md5($format.serialize($context).serialize($this->ignoredAttributes)).serialize($this->camelizedAttributes);
387401
} catch (\Exception $exception) {
388402
// The context cannot be serialized, skip the cache
389403
return false;

‎src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,16 @@ public function testIgnoredAttributes()
356356
['fooBar' => 'foobar'],
357357
$this->normalizer->normalize($obj, 'any')
358358
);
359+
360+
$this->normalizer->setIgnoredAttributes(['foo', 'baz', 'camelCase', 'object']);
361+
362+
$this->assertEquals(
363+
[
364+
'fooBar' => 'foobar',
365+
'bar' => 'bar',
366+
],
367+
$this->normalizer->normalize($obj, 'any')
368+
);
359369
}
360370

361371
public function testIgnoredAttributesDenormalize()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.