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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Fixed Store serializer generator decimal values handling
Damian Legawiec54716affa192
diff --git a/spree/core/lib/generators/spree/api_resource/templates/store_serializer.rb.tt b/spree/core/lib/generators/spree/api_resource/templates/store_serializer.rb.ttindex 30aebe44c0..cbb80e2de5 100644--- a/spree/core/lib/generators/spree/api_resource/templates/store_serializer.rb.tt+++ b/spree/core/lib/generators/spree/api_resource/templates/store_serializer.rb.tt@@ -1,10 +1,13 @@+<%# Decimal columns serialize as strings on the wire (Alba's oj_rails backend+ casts BigDecimal via as_json), so they must be typed :string — only+ integer/float are real JSON numbers. -%><% typelizable = attributes.reject(&:reference?).select { |a| %i[boolean integer decimal float].include?(a.type) } -%>module Spreemodule Apimodule V3class <%= bare_class_name %>Serializer < BaseSerializer<% if typelizable.any? -%>- typelize <%= typelizable.map { |a| "#{a.name}: :#{a.type == :boolean ? 'boolean' : 'number'}" }.join(', ') %>+ typelize <%= typelizable.map { |a| type = a.type == :boolean ? 'boolean' : (a.type == :decimal ? 'string' : 'number'); "#{a.name}: :#{type}" }.join(', ') %><% end -%>attributes <%= permitted_attribute_names.map { |a| ":#{a}" }.join(', ') %>diff --git a/spree/core/spec/lib/generators/spree/api_resource/api_resource_generator_spec.rb b/spree/core/spec/lib/generators/spree/api_resource/api_resource_generator_spec.rbindex 3a8c72edf8..0420916f15 100644--- a/spree/core/spec/lib/generators/spree/api_resource/api_resource_generator_spec.rb+++ b/spree/core/spec/lib/generators/spree/api_resource/api_resource_generator_spec.rb@@ -137,6 +137,12 @@ RSpec.describe Spree::ApiResourceGenerator, type: :generator doexpect(result[:admin_serializer]).to include('< V3::BrandSerializer')end+ it 'typelizes decimal columns as :string (oj_rails serializes BigDecimal as a JSON string)' do+ result = run_generator(['Brand', 'name:string', 'price:decimal', 'rank:integer', 'active:boolean'])++ expect(result[:store_serializer]).to include('typelize price: :string, rank: :number, active: :boolean')+ end+it 'does not redeclare :id in the Store serializer (BaseSerializer already defines it as the prefixed ID)' doresult = run_generator(['Brand', 'name:string', 'active:boolean'])