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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
deserialize empty string setting value to nil when not string format
Christophe Bliardc6ff4be74fcf
diff --git a/app/models/setting.rb b/app/models/setting.rbindex 5888f26e1e5..9ccab7b9362 100644--- a/app/models/setting.rb+++ b/app/models/setting.rb@@ -97,7 +97,7 @@ class Setting < ApplicationRecordendvalue = self[:#{name}]- ActiveRecord::Type::Boolean.new.cast(value)+ ActiveRecord::Type::Boolean.new.cast(value) || falseenddef self.#{name}=(value)@@ -157,7 +157,7 @@ class Setting < ApplicationRecordenddef 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 < ApplicationRecorddef formatted_value(value)return value if value.blank?- if config.serialized?+ if definition.serialized?return value.to_yamlend@@ -180,7 +180,7 @@ class Setting < ApplicationRecordenddef 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 < ApplicationRecordclear_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_valueelse- old_setting+ old_valueendend@@ -273,7 +273,7 @@ class Setting < ApplicationRecorddefinition = Settings::Definition[name]value = if definition.writable?- cached_settings.fetch(name) { Settings::Definition[name].value }+ cached_settings.fetch(name) { definition.value }elsedefinition.valueend@@ -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 valuedef 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 : nilendend@@ -352,10 +352,10 @@ class Setting < ApplicationRecordprotected- def config- @config ||= Settings::Definition[name]+ def definition+ @definition ||= Settings::Definition[name]enddelegate :format,- to: :config+ to: :definitionenddiff --git a/config/constants/settings/definition.rb b/config/constants/settings/definition.rbindex 12469993644..8291ec9eb51 100644--- a/config/constants/settings/definition.rb+++ b/config/constants/settings/definition.rb@@ -57,7 +57,7 @@ module Settingswhen :float@value.to_fwhen :boolean- @value.is_a?(Integer) ? ActiveRecord::Type::Boolean.new.cast(@value) : @value+ ActiveRecord::Type::Boolean.new.cast(@value)when :symbol@value.to_symelsediff --git a/spec/lib/open_project/configuration_spec.rb b/spec/lib/open_project/configuration_spec.rbindex 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 dodescribe '.[setting]' doit 'fetches the value' doexpect(described_class.app_title)diff --git a/spec/models/setting_spec.rb b/spec/models/setting_spec.rbindex 92eb7adfca6..5d940d3ae61 100644--- a/spec/models/setting_spec.rb+++ b/spec/models/setting_spec.rb@@ -111,6 +111,47 @@ describe Setting, type: :model doexpect(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+ endenddescribe '.[setting]?' do@@ -295,7 +336,7 @@ describe Setting, type: :model doendend- context 'cache is not empty' do+ context 'when cache is not empty' dolet(:cached_hash) do{ 'available_languages' => "---\n- en\n- de\n" }enddiff --git a/spec/support/shared/with_settings.rb b/spec/support/shared/with_settings.rbindex 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)settingsend+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)