Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

deserialize empty string setting value to nil when not string format

Christophe Bliardc6ff4be74fcf
diff --git a/app/models/setting.rb b/app/models/setting.rb
index 5888f26e1e5..9ccab7b9362 100644
--- a/app/models/setting.rb
+++ b/app/models/setting.rb
@@ -97,7 +97,7 @@ class Setting < ApplicationRecord
end
value = self[:#{name}]
- ActiveRecord::Type::Boolean.new.cast(value)
+ ActiveRecord::Type::Boolean.new.cast(value) || false
end
def self.#{name}=(value)
@@ -157,7 +157,7 @@ class Setting < ApplicationRecord
end
def value=(val)
- unless Settings::Definition[name].writable?
+ unless definition.writable?
raise NoMethodError, "#{name} is not writable but can be set through env vars or configuration.yml file."
end
@@ -167,7 +167,7 @@ class Setting < ApplicationRecord
def formatted_value(value)
return value if value.blank?
- if config.serialized?
+ if definition.serialized?
return value.to_yaml
end
@@ -180,7 +180,7 @@ class Setting < ApplicationRecord
end
def self.[]=(name, value)
- old_setting = cached_or_default(name)
+ old_value = cached_or_default(name)
new_setting = find_or_initialize_by(name: name)
new_setting.value = value
@@ -195,11 +195,11 @@ class Setting < ApplicationRecord
clear_cache(old_cache_key)
# fire callbacks for name and pass as much information as possible
- fire_callbacks(name, new_value, old_setting)
+ fire_callbacks(name, new_value, old_value)
new_value
else
- old_setting
+ old_value
end
end
@@ -273,7 +273,7 @@ class Setting < ApplicationRecord
definition = Settings::Definition[name]
value = if definition.writable?
- cached_settings.fetch(name) { Settings::Definition[name].value }
+ cached_settings.fetch(name) { definition.value }
else
definition.value
end
@@ -320,16 +320,16 @@ class Setting < ApplicationRecord
@settings_table_exists_yet ||= connection.data_source_exists?(table_name)
end
- # Unserialize a serialized settings value
+ # Deserialize a serialized settings value
def self.deserialize(name, value)
definition = Settings::Definition[name]
if definition.serialized? && value.is_a?(String)
YAML::safe_load(value, permitted_classes: [Symbol, ActiveSupport::HashWithIndifferentAccess, Date, Time])
- elsif value.present?
- read_formatted_setting value, definition.format
+ elsif value != '' && !value.nil?
+ read_formatted_setting(value, definition.format)
else
- value
+ definition.format == :string ? value : nil
end
end
@@ -352,10 +352,10 @@ class Setting < ApplicationRecord
protected
- def config
- @config ||= Settings::Definition[name]
+ def definition
+ @definition ||= Settings::Definition[name]
end
delegate :format,
- to: :config
+ to: :definition
end
diff --git a/config/constants/settings/definition.rb b/config/constants/settings/definition.rb
index 12469993644..8291ec9eb51 100644
--- a/config/constants/settings/definition.rb
+++ b/config/constants/settings/definition.rb
@@ -57,7 +57,7 @@ module Settings
when :float
@value.to_f
when :boolean
- @value.is_a?(Integer) ? ActiveRecord::Type::Boolean.new.cast(@value) : @value
+ ActiveRecord::Type::Boolean.new.cast(@value)
when :symbol
@value.to_sym
else
diff --git a/spec/lib/open_project/configuration_spec.rb b/spec/lib/open_project/configuration_spec.rb
index 26790d802bd..4e04211307a 100644
--- a/spec/lib/open_project/configuration_spec.rb
+++ b/spec/lib/open_project/configuration_spec.rb
@@ -28,18 +28,7 @@
require 'spec_helper'
-describe OpenProject::Configuration do
- let!(:definitions_before) { Settings::Definition.all.dup }
-
- before do
- Settings::Definition.send(:reset)
- end
-
- after do
- Settings::Definition.send(:reset)
- Settings::Definition.instance_variable_set(:@all, definitions_before)
- end
-
+describe OpenProject::Configuration, :settings_reset do
describe '.[setting]' do
it 'fetches the value' do
expect(described_class.app_title)
diff --git a/spec/models/setting_spec.rb b/spec/models/setting_spec.rb
index 92eb7adfca6..5d940d3ae61 100644
--- a/spec/models/setting_spec.rb
+++ b/spec/models/setting_spec.rb
@@ -111,6 +111,47 @@ describe Setting, type: :model do
expect(described_class.app_title)
.to eql('OpenProject')
end
+
+ context 'when value is blank but not nil' do
+ it 'is read correctly for array' do
+ expect(Settings::Definition['apiv3_cors_origins'].format).to eq(:array) # safeguard
+ expect(described_class['apiv3_cors_origins']).to eq([])
+ end
+
+ it 'is read correctly for hash' do
+ expect(Settings::Definition['fog'].format).to eq(:hash) # safeguard
+ expect(described_class['fog']).to eq({})
+ end
+ end
+
+ context 'when value was seeded as empty string in database', :settings_reset do
+ let(:setting_name) { "my_setting" }
+
+ subject { described_class[setting_name] }
+
+ before do
+ Settings::Definition.add(
+ setting_name,
+ value: nil,
+ format: setting_format
+ )
+ described_class.create!(name: setting_name, value: '')
+ end
+
+ %i[array boolean date datetime hash symbol].each do |setting_format|
+ context "for a #{setting_format} setting" do
+ let(:setting_format) { setting_format }
+
+ it { is_expected.to be_nil }
+ end
+ end
+
+ context 'for a string setting' do
+ let(:setting_format) { :string }
+
+ it { is_expected.to eq('') }
+ end
+ end
end
describe '.[setting]?' do
@@ -295,7 +336,7 @@ describe Setting, type: :model do
end
end
- context 'cache is not empty' do
+ context 'when cache is not empty' do
let(:cached_hash) do
{ 'available_languages' => "---\n- en\n- de\n" }
end
diff --git a/spec/support/shared/with_settings.rb b/spec/support/shared/with_settings.rb
index cdc3b319c17..9595558e17e 100644
--- a/spec/support/shared/with_settings.rb
+++ b/spec/support/shared/with_settings.rb
@@ -38,8 +38,29 @@ def aggregate_mocked_settings(example, settings)
settings
end
+RSpec.shared_context 'with settings reset' do
+ let(:storages_module_active) { true }
+
+ around do |example|
+ definitions_before = Settings::Definition.all.dup
+ Settings::Definition.send(:reset)
+ example.run
+ ensure
+ Settings::Definition.send(:reset)
+ Settings::Definition.instance_variable_set(:@all, definitions_before)
+ end
+
+ before do
+ allow(OpenProject::FeatureDecisions).to receive(:storages_module_active?).and_return(storages_module_active)
+ end
+end
+
RSpec.configure do |config|
- config.before(:each) do |example|
+ # examples tagged with `:settings_reset` will automatically have the settings
+ # reset before the example, and restored after.
+ config.include_context "with settings reset", :settings_reset
+
+ config.before do |example|
settings = example.metadata[:with_settings]
if settings.present?
settings = aggregate_mocked_settings(example, settings)