Skip to content

A platform for community discussion. Free, open, simple.

FIX: Support tag query param on `/tag/{name}` routes (#20742)

Isaac Janzenca4b73d20c4a
diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb
index 5604b401d29..ff6d31efe9f 100644
--- a/app/controllers/tags_controller.rb
+++ b/app/controllers/tags_controller.rb
@@ -545,6 +545,6 @@ class TagsController < ::ApplicationController
end
def tag_params
- [@tag_id].concat(Array(@additional_tags))
+ Array(params[:tags]).concat(Array(@additional_tags))
end
end
diff --git a/lib/topic_query_params.rb b/lib/topic_query_params.rb
index 291886604d6..9e6d69395c2 100644
--- a/lib/topic_query_params.rb
+++ b/lib/topic_query_params.rb
@@ -3,7 +3,7 @@
module TopicQueryParams
def build_topic_list_options
options = {}
- params[:tags] = [params[:tag_id]] if params[:tag_id].present? && guardian.can_tag_pms?
+ params[:tags] = [params[:tag_id], *Array(params[:tags])].uniq if params[:tag_id].present?
TopicQuery.public_valid_options.each do |key|
if params.key?(key)
diff --git a/spec/requests/tags_controller_spec.rb b/spec/requests/tags_controller_spec.rb
index 63b60c5c315..7cb2c7aa4e0 100644
--- a/spec/requests/tags_controller_spec.rb
+++ b/spec/requests/tags_controller_spec.rb
@@ -434,6 +434,28 @@ RSpec.describe TagsController do
expect(response.status).to eq(200)
end
+ it "can handle additional tags in query params" do
+ tag2 = Fabricate(:tag)
+ topic_with_two_tags = Fabricate(:topic, tags: [tag, tag2])
+
+ get "/tag/test.json?match_all_tags=true&tags[]=#{tag2.name}"
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["topic_list"]["topics"].map { |t| t["id"] }).to contain_exactly(
+ topic_with_two_tags.id,
+ )
+ end
+
+ it "can handle duplicate tags in query params" do
+ tag2 = Fabricate(:tag)
+ topic_with_two_tags = Fabricate(:topic, tags: [tag, tag2])
+
+ get "/tag/test.json?match_all_tags=true&tags[]=test&tags[]=#{tag2.name}"
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["topic_list"]["topics"].map { |t| t["id"] }).to contain_exactly(
+ topic_with_two_tags.id,
+ )
+ end
+
it "handles special tag 'none'" do
SiteSetting.pm_tags_allowed_for_groups = "1|2|3"