Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit7b64d88

Browse files
committed
[Serializer] Respect ignored attributes and camelcased attributes in cache key and update cache_key in nested structures
1 parent69058e3 commit7b64d88

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

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

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function supportsNormalization($data, $format = null)
5858
publicfunctionnormalize($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
publicfunctiondenormalize($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+
protectedfunctioncreateChildContext(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-
privatefunctiongetCacheKey($format,array$context)
397+
privatefunctiongetAttributeCacheKey($format,array$context)
384398
{
385399
try {
386-
returnmd5($format.serialize($context));
400+
returnmd5($format.serialize($context).serialize($this->ignoredAttributes)).serialize($this->camelizedAttributes);
387401
}catch (\Exception$exception) {
388402
// The context cannot be serialized, skip the cache
389403
returnfalse;

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

Lines 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
publicfunctiontestIgnoredAttributesDenormalize()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp