Skip to content

Fix: generating iri cache tag for collection operation with path para… #7152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

JakovKnezovicc
Copy link

Q A
Branch? main
Tickets -
License MIT
Doc PR -

Fix: Generating IRI Cache Tag for collection operation with path parameter
This pull request addresses an issue in the PurgeHttpCacheListener where the IRI cache tag for collection operations was not being generated when path parameters were involved.

Changes:

  • Updated the gatherResourceAndItemTags method to use the entity directly when generating the IRI for a collection operation, instead of resolving the resource class. I think it should not be an issue as IriConverter handles non resource objects.
  • Added ApiPlatform\Metadata\Exception\InvalidArgumentException to catch as it will fail on object deletion since it is called onFlush.

Simplified Use Case:
Consider an entity A with a OneToMany relationship to entity B. The collection operation URI template for entity B might look like /a/{id}/b. Since entity B is only relevant in the context of its parent entity A, when updating A, we want to purge the cache using the tag /a/{id}/b instead of /b. This ensures that we avoid purging cache for all responses containing B tags, maintaining more precise cache invalidation.

@@ -111,14 +111,13 @@ public function postFlush(): void
private function gatherResourceAndItemTags(object $entity, bool $purgeItem): void
{
try {
$resourceClass = $this->resourceClassResolver->getResourceClass($entity);
$iri = $this->iriConverter->getIriFromResource($resourceClass, UrlGeneratorInterface::ABS_PATH, new GetCollection());
$iri = $this->iriConverter->getIriFromResource($entity, UrlGeneratorInterface::ABS_PATH, new GetCollection());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the patch looks fine though we should add some non-regression test

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the patch looks fine though we should add some non-regression test

Can you give me some examples/pointers for non-regression tests? Do you need me to do those tests?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants