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.

Add primary variant to storefront and platform product serializer (#11267)

Szymon Iwacz1f33360591d7
diff --git a/api/app/serializers/spree/api/v2/platform/product_serializer.rb b/api/app/serializers/spree/api/v2/platform/product_serializer.rb
index b492f430b1..edc746321a 100644
--- a/api/app/serializers/spree/api/v2/platform/product_serializer.rb
+++ b/api/app/serializers/spree/api/v2/platform/product_serializer.rb
@@ -44,17 +44,17 @@ module Spree
belongs_to :tax_category
- has_one :master_variant,
- object_method_name: :master,
- id_method_name: :master_id,
- record_type: :variant,
- serializer: :variant
+ has_one :primary_variant,
+ object_method_name: :master,
+ id_method_name: :master_id,
+ record_type: :variant,
+ serializer: :variant
- has_one :default_variant,
- object_method_name: :default_variant,
- id_method_name: :default_variant_id,
- record_type: :variant,
- serializer: :variant
+ has_one :default_variant,
+ object_method_name: :default_variant,
+ id_method_name: :default_variant_id,
+ record_type: :variant,
+ serializer: :variant
has_many :variants
has_many :option_types
diff --git a/api/app/serializers/spree/v2/storefront/product_serializer.rb b/api/app/serializers/spree/v2/storefront/product_serializer.rb
index 409b0ca619..5444820875 100644
--- a/api/app/serializers/spree/v2/storefront/product_serializer.rb
+++ b/api/app/serializers/spree/v2/storefront/product_serializer.rb
@@ -54,16 +54,22 @@ module Spree
# all images from all variants
has_many :images,
- object_method_name: :variant_images,
- id_method_name: :variant_image_ids,
- record_type: :image,
- serializer: :image
-
- has_one :default_variant,
- object_method_name: :default_variant,
- id_method_name: :default_variant_id,
- record_type: :variant,
- serializer: :variant
+ object_method_name: :variant_images,
+ id_method_name: :variant_image_ids,
+ record_type: :image,
+ serializer: :image
+
+ has_one :default_variant,
+ object_method_name: :default_variant,
+ id_method_name: :default_variant_id,
+ record_type: :variant,
+ serializer: :variant
+
+ has_one :primary_variant,
+ object_method_name: :master,
+ id_method_name: :master_id,
+ record_type: :variant,
+ serializer: :variant
end
end
end
diff --git a/api/docs/v2/storefront/index.yaml b/api/docs/v2/storefront/index.yaml
index 6199bb0fa0..731d84d1fc 100644
--- a/api/docs/v2/storefront/index.yaml
+++ b/api/docs/v2/storefront/index.yaml
@@ -7604,11 +7604,11 @@ components:
Specify what related resources (relationships) you would like to receive in the response body. Eg.
```
- default_variant,variants,option_types,product_properties,taxons,images
+ default_variant,variants,option_types,product_properties,taxons,images,primary_variant
```
[More information](https://jsonapi.org/format/#fetching-includes)
- example: 'default_variant,variants,option_types,product_properties,taxons,images'
+ example: 'default_variant,variants,option_types,product_properties,taxons,images,primary_variant'
TaxonIncludeParam:
name: include
in: query
diff --git a/api/spec/requests/spree/api/v2/resource_includes_spec.rb b/api/spec/requests/spree/api/v2/resource_includes_spec.rb
index 8d8ecf2e7a..1ac07538ff 100644
--- a/api/spec/requests/spree/api/v2/resource_includes_spec.rb
+++ b/api/spec/requests/spree/api/v2/resource_includes_spec.rb
@@ -6,12 +6,14 @@ describe 'API v2 JSON API Resource Includes Spec', type: :request do
let!(:product) { create(:product, stores: [store]) }
let!(:product_option_type) { create(:product_option_type, product: product) }
let!(:option_value) { create(:option_value, option_type: product_option_type.option_type) }
- let(:default_variant) { product.master }
+ let(:primary_variant) { product.master }
+ let!(:default_variant) { create(:variant, product: product) }
shared_examples 'requested resources' do
it 'are returned' do
expect(json_response['included']).to be_present
expect(json_response['included']).to include(have_type('variant').and have_id(default_variant.id.to_s))
+ expect(json_response['included']).not_to include(have_type('variant').and have_id(primary_variant.id.to_s))
expect(json_response['included']).not_to include(have_type('option_type'))
end
end
@@ -20,6 +22,7 @@ describe 'API v2 JSON API Resource Includes Spec', type: :request do
it 'are returned' do
expect(json_response['included']).to be_present
expect(json_response['included']).not_to include(have_type('variant').and have_id(default_variant.id.to_s))
+ expect(json_response['included']).to include(have_type('variant').and have_id(primary_variant.id.to_s))
expect(json_response['included']).to include(have_type('option_type'))
expect(json_response['included']).to include(have_type('option_value'))
end
@@ -59,7 +62,7 @@ describe 'API v2 JSON API Resource Includes Spec', type: :request do
end
context 'with nested resources' do
- before { get "/api/v2/storefront/products/#{product.id}?include=option_types,option_types.option_values" }
+ before { get "/api/v2/storefront/products/#{product.id}?include=primary_variant,option_types,option_types.option_values" }
it_behaves_like 'nested requested resources'
end
@@ -89,7 +92,7 @@ describe 'API v2 JSON API Resource Includes Spec', type: :request do
end
context 'with nested resources' do
- before { get '/api/v2/storefront/products?include=option_types,option_types.option_values' }
+ before { get '/api/v2/storefront/products?include=primary_variant,option_types,option_types.option_values' }
it_behaves_like 'nested requested resources'
end
diff --git a/api/spec/serializers/spree/api/v2/platform/product_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/product_serializer_spec.rb
index ab63f9b5ce..dd723e14d1 100644
--- a/api/spec/serializers/spree/api/v2/platform/product_serializer_spec.rb
+++ b/api/spec/serializers/spree/api/v2/platform/product_serializer_spec.rb
@@ -56,7 +56,7 @@ describe Spree::Api::V2::Platform::ProductSerializer do
type: :tax_category
}
},
- master_variant: {
+ primary_variant: {
data: {
id: product.master.id.to_s,
type: :variant