FIX: error when changing a topic's category and creating a tag

FIX: error when changing a topic’s category and creating a tag

diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb
index 99ff5fb..238746f 100644
--- a/app/controllers/topics_controller.rb
+++ b/app/controllers/topics_controller.rb
@@ -342,6 +342,8 @@ class TopicsController < ApplicationController
             end
           end
 
+          invalid_tags = Tag.where_name(invalid_tags).pluck(:name)
+
           if !invalid_tags.empty?
             if (invalid_tags & DiscourseTagging.hidden_tag_names).present?
               return render_json_error(I18n.t('category.errors.disallowed_tags_generic'))
diff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rb
index 947d244..c5ff3ec 100644
--- a/spec/requests/topics_controller_spec.rb
+++ b/spec/requests/topics_controller_spec.rb
@@ -1038,6 +1038,32 @@ RSpec.describe TopicsController do
             expect(topic.tags.pluck(:id)).to contain_exactly(tag.id)
           end
 
+          it "can create a tag" do
+            SiteSetting.min_trust_to_create_tag = 0
+            expect do
+              put "/t/#{topic.slug}/#{topic.id}.json", params: {
+                tags: ["newtag"]
+              }
+            end.to change { topic.reload.first_post.revisions.count }.by(1)
+
+            expect(response.status).to eq(200)
+            expect(topic.reload.tags.pluck(:name)).to contain_exactly("newtag")
+          end
+
+          it "can change the category and create a new tag" do
+            SiteSetting.min_trust_to_create_tag = 0
+            category = Fabricate(:category)
+            expect do
+              put "/t/#{topic.slug}/#{topic.id}.json", params: {
+                tags: ["newtag"],
+                category_id: category.id
+              }
+            end.to change { topic.reload.first_post.revisions.count }.by(1)
+
+            expect(response.status).to eq(200)
+            expect(topic.reload.tags.pluck(:name)).to contain_exactly("newtag")
+          end
+
           it "can add a tag to wiki topic" do
             SiteSetting.min_trust_to_edit_wiki_post = 2
             topic.first_post.update!(wiki: true)

GitHub sha: 29e73383

1 Like