Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Fix: an empty nullable integer or boolean setting reads back as null

Damian Legawiecb47e5fe2cf79
diff --git a/spree/core/lib/spree/core/preferences/preferable.rb b/spree/core/lib/spree/core/preferences/preferable.rb
index 2728263d63..2eb638c421 100644
--- a/spree/core/lib/spree/core/preferences/preferable.rb
+++ b/spree/core/lib/spree/core/preferences/preferable.rb
@@ -196,6 +196,7 @@ module Spree::Preferences::Preferable
# @return [Object]
def wire_preference_value(value, definition)
return BigDecimal(value.to_s).as_json if definition[:type] == :decimal && value.is_a?(Numeric)
+ return nil if value == '' && definition[:nullable] && %i[integer boolean].include?(definition[:type])
return cast_preference_item(value, :integer) if definition[:type] == :integer && value.is_a?(String)
return convert_preference_value(value, :boolean, nullable: definition[:nullable]) if definition[:type] == :boolean && value.is_a?(String)
return value if value.nil? || (definition[:type] == :array && !value.is_a?(Array))
diff --git a/spree/core/spec/models/spree/preferences/preferable_spec.rb b/spree/core/spec/models/spree/preferences/preferable_spec.rb
index c74cf48ce9..592b9fa807 100644
--- a/spree/core/spec/models/spree/preferences/preferable_spec.rb
+++ b/spree/core/spec/models/spree/preferences/preferable_spec.rb
@@ -527,6 +527,7 @@ describe Spree::Preferences::Preferable, type: :model do
it 'reads back an integer or boolean stored as a string by older code as its type' do
expect(record.wire_preference_value('5', type: :integer)).to eq(5)
expect(record.wire_preference_value('true', type: :boolean)).to be(true)
+ expect(record.wire_preference_value('', type: :integer, nullable: true)).to be_nil
end
it 'warns about a declaration that does not state its full type' do