Skip to content

Commit

Permalink
bug #36340 [Serializer] Fix configuration of the cache key (dunglas)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 4.4 branch.

Discussion
----------

[Serializer] Fix configuration of the cache key

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | Fix #35574 doctrine/orm#8030 (partially)
| License       | MIT
| Doc PR        | n/a

Currently, a bug prevents to configure the context keys to exclude from the cache key computation. The value is always replaced in the constructor. This PR fixes the problem.

Commits
-------

3b034cb [Serializer] Fix configuration of the cache key
  • Loading branch information
fabpot committed Aug 17, 2020
2 parents df3ab76 + 3b034cb commit 66b9fef
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
Expand Up @@ -115,7 +115,7 @@ public function __construct(ClassMetadataFactoryInterface $classMetadataFactory
throw new InvalidArgumentException(sprintf('The "%s" given in the default context is not callable.', self::MAX_DEPTH_HANDLER));
}

$this->defaultContext[self::EXCLUDE_FROM_CACHE_KEY] = [self::CIRCULAR_REFERENCE_LIMIT_COUNTERS];
$this->defaultContext[self::EXCLUDE_FROM_CACHE_KEY] = array_merge($this->defaultContext[self::EXCLUDE_FROM_CACHE_KEY] ?? [], [self::CIRCULAR_REFERENCE_LIMIT_COUNTERS]);

$this->propertyTypeExtractor = $propertyTypeExtractor;

Expand Down Expand Up @@ -360,7 +360,7 @@ public function denormalize($data, $type, $format = null, array $context = [])
try {
$this->setAttributeValue($object, $attribute, $value, $format, $context);
} catch (InvalidArgumentException $e) {
throw new NotNormalizableValueException(sprintf('Failed to denormalize attribute "%s" value for class "%s": ', $attribute, $type).$e->getMessage(), $e->getCode(), $e);
throw new NotNormalizableValueException(sprintf('Failed to denormalize attribute "%s" value for class "%s": '.$e->getMessage(), $attribute, $type), $e->getCode(), $e);
}
}

Expand Down
Expand Up @@ -741,6 +741,17 @@ public function testNormalizeNotSerializableContext()
}]));
}

public function testDefaultExcludeFromCacheKey()
{
$normalizer = new class(null, null, null, null, null, null, [ObjectNormalizer::EXCLUDE_FROM_CACHE_KEY => ['foo']]) extends ObjectNormalizer {
protected function isCircularReference($object, &$context)
{
ObjectNormalizerTest::assertContains('foo', $this->defaultContext[ObjectNormalizer::EXCLUDE_FROM_CACHE_KEY]);
}
};
$normalizer->normalize(new ObjectDummy());
}

public function testThrowUnexpectedValueException()
{
$this->expectException('Symfony\Component\Serializer\Exception\UnexpectedValueException');
Expand Down

0 comments on commit 66b9fef

Please sign in to comment.