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
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.rbindex 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 Spreebelongs_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: :varianthas_many :variantshas_many :option_typesdiff --git a/api/app/serializers/spree/v2/storefront/product_serializer.rb b/api/app/serializers/spree/v2/storefront/product_serializer.rbindex 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 variantshas_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: :variantendendenddiff --git a/api/docs/v2/storefront/index.yaml b/api/docs/v2/storefront/index.yamlindex 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: includein: querydiff --git a/api/spec/requests/spree/api/v2/resource_includes_spec.rb b/api/spec/requests/spree/api/v2/resource_includes_spec.rbindex 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 dolet!(: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' doit 'are returned' doexpect(json_response['included']).to be_presentexpect(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'))endend@@ -20,6 +22,7 @@ describe 'API v2 JSON API Resource Includes Spec', type: :request doit 'are returned' doexpect(json_response['included']).to be_presentexpect(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 doendcontext '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 doendcontext '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'enddiff --git a/api/spec/serializers/spree/api/v2/platform/product_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/product_serializer_spec.rbindex 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 dotype: :tax_category}},- master_variant: {+ primary_variant: {data: {id: product.master.id.to_s,type: :variant