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.

Fixed Countries/States API endpoints

Damian Legawiec139ef639939f
diff --git a/api/app/controllers/spree/api/v3/store/countries_controller.rb b/api/app/controllers/spree/api/v3/store/countries_controller.rb
index 1163f93db9..7068ce8500 100644
--- a/api/app/controllers/spree/api/v3/store/countries_controller.rb
+++ b/api/app/controllers/spree/api/v3/store/countries_controller.rb
@@ -33,22 +33,18 @@ module Spree
end
def serialize_country(country, include_states: false)
- data = {
- iso: country.iso,
- iso3: country.iso3,
- name: country.name,
- states_required: country.states_required,
- zipcode_required: country.zipcode_required
- }
+ data = Spree.api.country_serializer.new(country, params: { includes: [] }).to_h
if include_states
- data[:states] = country.states.order(:name).map do |state|
- { abbr: state.abbr, name: state.name }
- end
+ data[:states] = serialize_collection(country.states.order(:name), Spree.api.state_serializer)
end
data
end
+
+ def serialize_collection(collection, serializer_class)
+ collection.map { |item| serializer_class.new(item).to_h }
+ end
end
end
end
diff --git a/api/spec/controllers/spree/api/v3/store/countries_controller_spec.rb b/api/spec/controllers/spree/api/v3/store/countries_controller_spec.rb
index 2c185f631e..c4c2e0c05b 100644
--- a/api/spec/controllers/spree/api/v3/store/countries_controller_spec.rb
+++ b/api/spec/controllers/spree/api/v3/store/countries_controller_spec.rb
@@ -56,7 +56,7 @@ RSpec.describe Spree::Api::V3::Store::CountriesController, type: :controller do
expect(response).to have_http_status(:ok)
expect(json_response['states']).to be_present
- expect(json_response['states'].first['iso']).to eq(state.abbr)
+ expect(json_response['states'].first['abbr']).to eq(state.abbr)
expect(json_response['states'].first['name']).to eq(state.name)
end