Skip to content

Do not add the same tag multiple times #423

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

Merged
merged 3 commits into from
Jul 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ See also the [GitHub releases page](https://github.com/FriendsOfSymfony/FOSHttpC

* Added: `CleanupCacheTagsListener` to remove the cache tags header from the final
response that is sent to the client. Add this listener to your cache kernel.

### Tagging

* Improved: The `ResponseTagger` does now remove duplicate tags.

2.3.1
-----
Expand Down
2 changes: 1 addition & 1 deletion src/ResponseTagger.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function addTags(array $tags)
throw new InvalidTagException('Empty tags are not allowed');
}

$this->tags = array_merge($this->tags, $filtered);
$this->tags = array_unique(array_merge($this->tags, $filtered));
Copy link
Contributor

Choose a reason for hiding this comment

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

i am pondering whether we should do the unique only at the end when building the tags header. not sure whats better performance - doing unique only at the end in getTagsHeaderValue would add less overhead for people who's system is not adding duplicate tags. on the other hand, a system with massive duplicates would profit from a rolling de-duplication. also depends on whether addTags is called a lot or only a few times... wdyt?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I asked myself the same question but in the end I guess it's such a minor thing, nobody will ever notice 😄 I put it into the addTags() because there's already some filtering going on there. I just figured it would be better to add it there so it's all in one place. But again, at the end of the day it doesn't really matter I think 😊

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah, agreed


return $this;
}
Expand Down
12 changes: 12 additions & 0 deletions tests/Unit/ResponseTaggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,16 @@ public function testNonStrictEmptyTag()
$this->assertTrue($tagHandler->hasTags());
$this->assertEquals('post-1', $tagHandler->getTagsHeaderValue());
}

public function testUniqueTags()
{
$headerFormatter = new CommaSeparatedTagHeaderFormatter('FOS-Tags');

$tagHandler = new ResponseTagger(['header_formatter' => $headerFormatter, 'strict' => true]);
$tagHandler->addTags(['post-1', 'post-2', 'post-1']);
Copy link
Contributor

Choose a reason for hiding this comment

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

for the sake of completeness, i'd like to have another line here that does $tagHandler->addTags(['post-2', 'post-3']);. with the current test, array_merge($this->tags, array_unique($filtered)) would pass but its not correct.

$tagHandler->addTags(['post-2', 'post-3']);
$this->assertTrue($tagHandler->hasTags());

$this->assertEquals('post-1,post-2,post-3', $tagHandler->getTagsHeaderValue());
}
}