diff --git a/plugins/chat/app/models/category_channel.rb b/plugins/chat/app/models/category_channel.rb index e95c3d5cff8..b205e82b4ac 100644 --- a/plugins/chat/app/models/category_channel.rb +++ b/plugins/chat/app/models/category_channel.rb @@ -28,16 +28,18 @@ class CategoryChannel < ChatChannel end def ensure_slug_ok - # if we don't unescape it first we strip the % from the encoded version - slug = SiteSetting.slug_generation_method == "encoded" ? CGI.unescape(self.slug) : self.slug - self.slug = Slug.for(slug, "", method: :encoded) + if self.slug.present? + # if we don't unescape it first we strip the % from the encoded version + slug = SiteSetting.slug_generation_method == "encoded" ? CGI.unescape(self.slug) : self.slug + self.slug = Slug.for(slug, "", method: :encoded) - if self.slug.blank? - errors.add(:slug, :invalid) - elsif SiteSetting.slug_generation_method == "ascii" && !CGI.unescape(self.slug).ascii_only? - errors.add(:slug, I18n.t("chat.category_channel.errors.slug_contains_non_ascii_chars")) - elsif duplicate_slug? - errors.add(:slug, I18n.t("chat.category_channel.errors.is_already_in_use")) + if self.slug.blank? + errors.add(:slug, :invalid) + elsif SiteSetting.slug_generation_method == "ascii" && !CGI.unescape(self.slug).ascii_only? + errors.add(:slug, I18n.t("chat.category_channel.errors.slug_contains_non_ascii_chars")) + elsif duplicate_slug? + errors.add(:slug, I18n.t("chat.category_channel.errors.is_already_in_use")) + end end end end diff --git a/plugins/chat/spec/models/category_channel_spec.rb b/plugins/chat/spec/models/category_channel_spec.rb index 17d9139f6e7..e78681eb8bc 100644 --- a/plugins/chat/spec/models/category_channel_spec.rb +++ b/plugins/chat/spec/models/category_channel_spec.rb @@ -124,6 +124,13 @@ RSpec.describe CategoryChannel do channel.validate expect(channel.errors.full_messages).to include("Slug is invalid") end + + it "does not add a validation error when autogenerated slug ends up empty" do + SiteSetting.slug_generation_method = "none" + channel.update(name: "は無効です", slug: nil) + expect(channel).to be_valid + expect(channel.slug).to eq("") + end end context "when there is a duplicate slug" do