Skip to content

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

FIX: Error in client site settings JSON (#33640)

Martin Brennan1b3e17e9934a
diff --git a/lib/site_setting_extension.rb b/lib/site_setting_extension.rb
index 31de7f402ba..75438dcd524 100644
--- a/lib/site_setting_extension.rb
+++ b/lib/site_setting_extension.rb
@@ -200,41 +200,50 @@ module SiteSettingExtension
end
def client_settings_json_uncached
- MultiJson.dump(
- Hash[
- *@client_settings
- .flat_map do |name|
- # Themeable site settings require a theme ID, which we do not always
- # have when loading client site settings. They are excluded here,
- # to get them use theme_site_settings_json(:theme_id)
- next if themeable[name]
-
- value =
- if deprecated_settings.include?(name.to_s)
- public_send(name, warn: false)
- else
- public_send(name)
- end
-
- type = type_supervisor.get_type(name)
- if type == :upload
- value = value.to_s
- elsif type == :uploaded_image_list
- value = value.map(&:to_s).join("|")
- end
-
- [name, value]
+ uncached_json =
+ @client_settings.filter_map do |name|
+ # Themeable site settings require a theme ID, which we do not always
+ # have when loading client site settings. They are excluded here,
+ # to get them use theme_site_settings_json(:theme_id)
+ next if themeable[name]
+
+ value =
+ if deprecated_settings.include?(name.to_s)
+ public_send(name, warn: false)
+ else
+ public_send(name)
end
- .compact
- ],
- )
- rescue => e
- Rails.logger.error("Error while generating client_settings_json_uncached: #{e.message}")
+
+ type = type_supervisor.get_type(name)
+ if type == :upload
+ value = value.to_s
+ elsif type == :uploaded_image_list
+ value = value.map(&:to_s).join("|")
+ end
+
+ [name, value]
+ end
+ MultiJson.dump(Hash[uncached_json])
+ rescue => err
+ # If something goes wrong here we really need to be aware of it in tests.
+ raise err if Rails.env.test?
+
+ Rails.logger.error("Error while generating client_settings_json_uncached: #{err.message}")
nil
end
def theme_site_settings_json_uncached(theme_id)
- MultiJson.dump(theme_site_settings[theme_id])
+ begin
+ MultiJson.dump(theme_site_settings[theme_id])
+ rescue => err
+ # If something goes wrong here we really need to be aware of it in tests.
+ raise err if Rails.env.test?
+
+ Rails.logger.error(
+ "Error while generating theme_site_settings_json_uncached for theme ID #{theme_id}: #{err.message}",
+ )
+ nil
+ end
end
# Retrieve all settings
diff --git a/spec/lib/site_setting_extension_spec.rb b/spec/lib/site_setting_extension_spec.rb
index de35ecf8db2..d326c440a42 100644
--- a/spec/lib/site_setting_extension_spec.rb
+++ b/spec/lib/site_setting_extension_spec.rb
@@ -921,6 +921,12 @@ RSpec.describe SiteSettingExtension do
expect(client_settings["with_html"]).to eq("<script></script>rest")
end
+
+ it "does not include themeable site settings" do
+ SiteSetting.refresh!
+ expect(SiteSetting.client_settings_json_uncached).not_to include("enable_welcome_banner")
+ expect(SiteSetting.client_settings_json_uncached).not_to include("search_experience")
+ end
end
describe ".setup_methods" do
@@ -1041,6 +1047,15 @@ RSpec.describe SiteSettingExtension do
expect(SiteSetting.search_experience(theme_id: theme_1.id)).to eq("search_icon")
expect(SiteSetting.search_experience(theme_id: theme_2.id)).to eq("search_field")
end
+
+ describe ".theme_site_settings_json_uncached" do
+ it "returns the correct JSON" do
+ SiteSetting.refresh!
+ expect(SiteSetting.theme_site_settings_json_uncached(theme_1.id)).to eq(
+ %Q|{"enable_welcome_banner":false,"search_experience":"search_icon"}|,
+ )
+ end
+ end
end
describe "_map extension for list settings" do