A platform for community discussion. Free, open, simple.
FIX: validate YAML before save to avoid race condition
Kyle Zhao61dcd7c52c23
diff --git a/app/models/theme_field.rb b/app/models/theme_field.rbindex 7b235f281ee..3a393c22e01 100644--- a/app/models/theme_field.rb+++ b/app/models/theme_field.rb@@ -171,10 +171,7 @@ COMPILEDerrors << e.messageend- self.error = errors.join("\n").presence unless self.destroyed?- if will_save_change_to_error?- update_columns(error: self.error)- end+ self.error = errors.join("\n").presenceenddef self.guess_type(name)@@ -237,6 +234,8 @@ COMPILEDendbefore_save do+ validate_yaml!+if will_save_change_to_value? && !will_save_change_to_value_baked?self.value_baked = nilend@@ -245,7 +244,6 @@ COMPILEDafter_commit doensure_baked!ensure_scss_compiles!- validate_yaml!theme.clear_cached_settings!Stylesheet::Manager.clear_theme_cache! if self.name.include?("scss")diff --git a/spec/models/theme_spec.rb b/spec/models/theme_spec.rbindex 859421ce044..26c706ba79d 100644--- a/spec/models/theme_spec.rb+++ b/spec/models/theme_spec.rb@@ -355,6 +355,12 @@ HTMLexpect(theme_field.javascript_cache.content).to eq(transpiled.strip)end+ it 'is empty when the settings are invalid' do+ theme.set_field(target: :settings, name: :yaml, value: 'nil_setting: ')+ theme.save!++ expect(theme.settings).to be_empty+ endendit 'correctly caches theme ids' do