Skip to content

Commit 3f9a4b8

Browse files
authored
fix(jsonapi): skip cache for uriTemplate links (#8851)
1 parent 4d78b98 commit 3f9a4b8

2 files changed

Lines changed: 56 additions & 1 deletion

File tree

‎src/JsonApi/Serializer/ItemNormalizer.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ private function getComponents(object $object, ?string $format, array $context):
294294
);
295295

296296
$components['links'][$attribute] = $this->iriConverter->getIriFromResource($object, UrlGeneratorInterface::ABS_PATH, $operation, $childContext);
297+
$cacheKey = null;
297298
}
298299

299300
$components['relationships'][] = $relation;
@@ -304,7 +305,7 @@ private function getComponents(object $object, ?string $format, array $context):
304305
}
305306
}
306307

307-
if (false !== $context['cache_key']) {
308+
if ($cacheKey && false !== $context['cache_key']) {
308309
$this->componentsCache[$cacheKey] = $components;
309310
}
310311

‎src/JsonApi/Tests/Serializer/ItemNormalizerTest.php‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,60 @@ public function testCacheKeyIsFalseWhenAPropertyHasSecurity(): void
183183
$this->assertSame([], $componentsCacheRef->getValue($normalizer), 'componentsCache must not be populated when a property has security set');
184184
}
185185

186+
public function testUriTemplateLinkIsNotCachedAcrossObjects(): void
187+
{
188+
$first = new Dummy();
189+
$first->setId(1);
190+
$second = new Dummy();
191+
$second->setId(2);
192+
193+
$propertyNameCollectionFactory = $this->createStub(PropertyNameCollectionFactoryInterface::class);
194+
$propertyNameCollectionFactory->method('create')->willReturn(new PropertyNameCollection(['relatedDummy']));
195+
196+
$propertyMetadataFactory = $this->createStub(PropertyMetadataFactoryInterface::class);
197+
$propertyMetadataFactory->method('create')->willReturn(
198+
(new ApiProperty())
199+
->withNativeType(Type::object(RelatedDummy::class))
200+
->withReadable(true)
201+
->withUriTemplate('/related_dummies/{id}')
202+
);
203+
204+
$iriConverter = $this->createStub(IriConverterInterface::class);
205+
$iriConverter->method('getIriFromResource')->willReturnCallback(static fn (object $resource): string => \sprintf('/dummies/%d/related', $resource->getId()));
206+
207+
$resourceClassResolver = $this->createStub(ResourceClassResolverInterface::class);
208+
$resourceClassResolver->method('isResourceClass')->willReturn(true);
209+
$resourceClassResolver->method('getResourceClass')->willReturn(RelatedDummy::class);
210+
211+
$propertyAccessor = $this->createStub(PropertyAccessorInterface::class);
212+
$propertyAccessor->method('getValue')->willReturn(new RelatedDummy());
213+
214+
$resourceMetadataCollectionFactory = $this->createStub(ResourceMetadataCollectionFactoryInterface::class);
215+
$resourceMetadataCollectionFactory->method('create')->willReturn(new ResourceMetadataCollection(RelatedDummy::class, [
216+
(new ApiResource())->withOperations(new Operations([
217+
new Get(name: '/related_dummies/{id}', uriTemplate: '/related_dummies/{id}', shortName: 'RelatedDummy'),
218+
])),
219+
]));
220+
221+
$normalizer = new ItemNormalizer(
222+
$propertyNameCollectionFactory,
223+
$propertyMetadataFactory,
224+
$iriConverter,
225+
$resourceClassResolver,
226+
$propertyAccessor,
227+
new ReservedAttributeNameConverter(),
228+
null,
229+
[],
230+
$resourceMetadataCollectionFactory,
231+
);
232+
233+
$getComponents = new \ReflectionMethod(ItemNormalizer::class, 'getComponents');
234+
$context = ['resource_class' => Dummy::class, 'cache_key' => 'shared', 'api_normalize' => true];
235+
236+
$this->assertSame(['relatedDummy' => '/dummies/1/related'], $getComponents->invoke($normalizer, $first, ItemNormalizer::FORMAT, $context)['links']);
237+
$this->assertSame(['relatedDummy' => '/dummies/2/related'], $getComponents->invoke($normalizer, $second, ItemNormalizer::FORMAT, $context)['links']);
238+
}
239+
186240
public function testNormalizeCircularReference(): void
187241
{
188242
$circularReferenceEntity = new CircularReference();

0 commit comments

Comments
 (0)