|
21 | 21 | use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; |
22 | 22 | use ApiPlatform\Metadata\ResourceClassResolverInterface; |
23 | 23 | use ApiPlatform\Metadata\Util\ClassInfoTrait; |
| 24 | +use ApiPlatform\Serializer\CacheKeyTrait; |
24 | 25 | use ApiPlatform\Serializer\ItemNormalizer as BaseItemNormalizer; |
25 | 26 | use ApiPlatform\Symfony\Security\ResourceAccessCheckerInterface; |
26 | 27 | use Psr\Log\LoggerInterface; |
|
37 | 38 | */ |
38 | 39 | final class ItemNormalizer extends BaseItemNormalizer |
39 | 40 | { |
| 41 | + use CacheKeyTrait; |
40 | 42 | use ClassInfoTrait; |
41 | 43 |
|
42 | 44 | public const FORMAT = 'graphql'; |
43 | 45 | public const ITEM_RESOURCE_CLASS_KEY = '#itemResourceClass'; |
44 | 46 | public const ITEM_IDENTIFIERS_KEY = '#itemIdentifiers'; |
45 | 47 |
|
| 48 | + private array $safeCacheKeysCache = []; |
| 49 | + |
46 | 50 | public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, IriConverterInterface $iriConverter, private readonly IdentifiersExtractorInterface $identifiersExtractor, ResourceClassResolverInterface $resourceClassResolver, PropertyAccessorInterface $propertyAccessor = null, NameConverterInterface $nameConverter = null, ClassMetadataFactoryInterface $classMetadataFactory = null, LoggerInterface $logger = null, ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null, ResourceAccessCheckerInterface $resourceAccessChecker = null) |
47 | 51 | { |
48 | 52 | parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter, $classMetadataFactory, $logger ?: new NullLogger(), $resourceMetadataCollectionFactory, $resourceAccessChecker); |
@@ -81,7 +85,11 @@ public function normalize(mixed $object, string $format = null, array $context = |
81 | 85 | return parent::normalize($object, $format, $context); |
82 | 86 | } |
83 | 87 |
|
84 | | - unset($context['operation_name'], $context['operation']); |
| 88 | + if ($this->isCacheKeySafe($context)) { |
| 89 | + $context['cache_key'] = $this->getCacheKey($format, $context); |
| 90 | + } |
| 91 | + |
| 92 | + unset($context['operation_name'], $context['operation']); // Remove operation and operation_name only when cache key has been created |
85 | 93 | $data = parent::normalize($object, $format, $context); |
86 | 94 | if (!\is_array($data)) { |
87 | 95 | throw new UnexpectedValueException('Expected data to be an array.'); |
@@ -140,4 +148,32 @@ protected function setAttributeValue($object, $attribute, $value, $format = null |
140 | 148 |
|
141 | 149 | parent::setAttributeValue($object, $attribute, $value, $format, $context); |
142 | 150 | } |
| 151 | + |
| 152 | + /** |
| 153 | + * Check if any property contains a security grants, which makes the cache key not safe, |
| 154 | + * as allowed_properties can differ for 2 instances of the same object. |
| 155 | + */ |
| 156 | + private function isCacheKeySafe(array $context): bool |
| 157 | + { |
| 158 | + if (!isset($context['resource_class']) || !$this->resourceClassResolver->isResourceClass($context['resource_class'])) { |
| 159 | + return false; |
| 160 | + } |
| 161 | + $resourceClass = $this->resourceClassResolver->getResourceClass(null, $context['resource_class']); |
| 162 | + if (isset($this->safeCacheKeysCache[$resourceClass])) { |
| 163 | + return $this->safeCacheKeysCache[$resourceClass]; |
| 164 | + } |
| 165 | + $options = $this->getFactoryOptions($context); |
| 166 | + $propertyNames = $this->propertyNameCollectionFactory->create($resourceClass, $options); |
| 167 | + |
| 168 | + $this->safeCacheKeysCache[$resourceClass] = true; |
| 169 | + foreach ($propertyNames as $propertyName) { |
| 170 | + $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName, $options); |
| 171 | + if (null !== $propertyMetadata->getSecurity()) { |
| 172 | + $this->safeCacheKeysCache[$resourceClass] = false; |
| 173 | + break; |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + return $this->safeCacheKeysCache[$resourceClass]; |
| 178 | + } |
143 | 179 | } |
0 commit comments