Skip to content

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

FIX: Preserve case when bulk creating tags (#37593)

Natalie Tay2474fc87e730
diff --git a/app/services/tags/bulk_create.rb b/app/services/tags/bulk_create.rb
index ca72f9785a7..d600c79be6a 100644
--- a/app/services/tags/bulk_create.rb
+++ b/app/services/tags/bulk_create.rb
@@ -67,7 +67,9 @@ class Tags::BulkCreate
raw_tag_names.each do |raw_name|
next if raw_name.blank?
- normalized_input = raw_name.strip.downcase.gsub(/[[:space:]]+/, "-")
+ normalized_input = raw_name.strip
+ normalized_input = normalized_input.downcase if SiteSetting.force_lowercase_tags
+ normalized_input = normalized_input.gsub(/[[:space:]]+/, "-")
if normalized_input.length > SiteSetting.max_tag_length
results[:failed][raw_name] = I18n.t(
diff --git a/spec/services/tags/bulk_create_spec.rb b/spec/services/tags/bulk_create_spec.rb
index 17e06b96717..b113cda3a9f 100644
--- a/spec/services/tags/bulk_create_spec.rb
+++ b/spec/services/tags/bulk_create_spec.rb
@@ -103,6 +103,28 @@ RSpec.describe(Tags::BulkCreate) do
end
end
+ context "when force_lowercase_tags is disabled" do
+ let(:tag_names) { ["CamelCase", "UPPERCASE", "Tag With Spaces"] }
+
+ before { SiteSetting.force_lowercase_tags = false }
+
+ it { is_expected.to run_successfully }
+
+ it "preserves case in tag names" do
+ result
+ expect(Tag.where(name: %w[CamelCase UPPERCASE Tag-With-Spaces]).count).to eq(3)
+ end
+
+ it "returns preserved-case names" do
+ expect(result.results[:created]).to contain_exactly(
+ "CamelCase",
+ "UPPERCASE",
+ "Tag-With-Spaces",
+ )
+ expect(result.results[:failed]).to be_empty
+ end
+ end
+
context "when tag names are numbers" do
let(:tag_names) { [123, 456, 789] }