Skip to content

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.rb
index 7b235f281ee..3a393c22e01 100644
--- a/app/models/theme_field.rb
+++ b/app/models/theme_field.rb
@@ -171,10 +171,7 @@ COMPILED
errors << e.message
end
- 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").presence
end
def self.guess_type(name)
@@ -237,6 +234,8 @@ COMPILED
end
before_save do
+ validate_yaml!
+
if will_save_change_to_value? && !will_save_change_to_value_baked?
self.value_baked = nil
end
@@ -245,7 +244,6 @@ COMPILED
after_commit do
ensure_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.rb
index 859421ce044..26c706ba79d 100644
--- a/spec/models/theme_spec.rb
+++ b/spec/models/theme_spec.rb
@@ -355,6 +355,12 @@ HTML
expect(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
+ end
end
it 'correctly caches theme ids' do