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 handling product thumbnails in API and SDK

Damian Legawiec45b2aa8d631c
diff --git a/api/app/serializers/spree/api/v3/base_serializer.rb b/api/app/serializers/spree/api/v3/base_serializer.rb
index 285e7e21c2..f3a1b88a76 100644
--- a/api/app/serializers/spree/api/v3/base_serializer.rb
+++ b/api/app/serializers/spree/api/v3/base_serializer.rb
@@ -78,8 +78,10 @@ module Spree
return nil if image.nil?
return nil unless image.respond_to?(:attached?) && image.attached?
- # cdn_image_url expects an attachment (with .blob method), not a raw blob
- Rails.application.routes.url_helpers.cdn_image_url(image)
+ # Handle Spree::Asset models (like Spree::Image) which have attachment inside
+ # vs direct ActiveStorage attachments (like taxon.image)
+ attachment = image.is_a?(Spree::Asset) ? image.attachment : image
+ Rails.application.routes.url_helpers.cdn_image_url(attachment)
end
end
end
diff --git a/api/app/serializers/spree/api/v3/image_serializer.rb b/api/app/serializers/spree/api/v3/image_serializer.rb
index 0373af4c1f..cbb464d6fb 100644
--- a/api/app/serializers/spree/api/v3/image_serializer.rb
+++ b/api/app/serializers/spree/api/v3/image_serializer.rb
@@ -16,7 +16,7 @@ module Spree
created_at: :iso8601, updated_at: :iso8601
attribute :original_url do |image|
- image_url(image)
+ image_url_for(image)
end
# Dynamically define attributes for each configured image variant
@@ -30,12 +30,6 @@ module Spree
private
- def image_url(image)
- return nil unless image&.attachment&.attached?
-
- Rails.application.routes.url_helpers.cdn_image_url(image.attachment)
- end
-
def variant_url(image, variant_name)
return nil unless image&.attachment&.attached?
diff --git a/api/app/serializers/spree/api/v3/product_serializer.rb b/api/app/serializers/spree/api/v3/product_serializer.rb
index d23733583a..fd7c3d17e0 100644
--- a/api/app/serializers/spree/api/v3/product_serializer.rb
+++ b/api/app/serializers/spree/api/v3/product_serializer.rb
@@ -8,7 +8,7 @@ module Spree
meta_description: 'string | null', meta_keywords: 'string | null',
variant_count: :number,
default_variant_id: :string,
- thumbnail: 'string | null',
+ thumbnail_url: 'string | null',
available_on: 'string | null',
purchasable: :boolean, in_stock: :boolean, backorderable: :boolean, available: :boolean,
price: 'StorePrice',
@@ -41,7 +41,7 @@ module Spree
end
# Main product image URL for listings (cached thumbnail)
- attribute :thumbnail do |product|
+ attribute :thumbnail_url do |product|
image_url_for(product.thumbnail)
end
diff --git a/api/spec/serializers/spree/api/v3/product_serializer_spec.rb b/api/spec/serializers/spree/api/v3/product_serializer_spec.rb
index effef31242..b2d85ff2be 100644
--- a/api/spec/serializers/spree/api/v3/product_serializer_spec.rb
+++ b/api/spec/serializers/spree/api/v3/product_serializer_spec.rb
@@ -15,7 +15,7 @@ RSpec.describe Spree::Api::V3::ProductSerializer do
'slug' => product.slug,
'default_variant_id' => product.default_variant.prefix_id
)
- expect(subject).to have_key('thumbnail')
+ expect(subject).to have_key('thumbnail_url')
expect(subject).to have_key('price')
expect(subject).to have_key('original_price')
end
diff --git a/sdk/src/types/generated/AdminProduct.ts b/sdk/src/types/generated/AdminProduct.ts
index 39318e7948..6fc0449bfd 100644
--- a/sdk/src/types/generated/AdminProduct.ts
+++ b/sdk/src/types/generated/AdminProduct.ts
@@ -1,4 +1,4 @@
-// Typelizer digest 99239c683d5d2457aa1a63a3bcab0d25
+// Typelizer digest facd41b13b43734ecb884a0cf6c4df37
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
import type {StorePrice, StoreImage, AdminVariant, StoreOptionType, StoreTaxon, AdminMetafield} from '@/types'
@@ -19,7 +19,7 @@ type AdminProduct = {
backorderable: boolean;
available: boolean;
default_variant_id: string;
- thumbnail: string | null;
+ thumbnail_url: string | null;
tags: Array<string>;
price: StorePrice;
original_price: StorePrice;
diff --git a/sdk/src/types/generated/StoreProduct.ts b/sdk/src/types/generated/StoreProduct.ts
index 111b433e6e..2cca97758f 100644
--- a/sdk/src/types/generated/StoreProduct.ts
+++ b/sdk/src/types/generated/StoreProduct.ts
@@ -1,4 +1,4 @@
-// Typelizer digest e55c23f637d5910bae0b278a2bd564ce
+// Typelizer digest d5fc34d9f1b52472b66918d452bdf2f9
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
import type {StorePrice, StoreImage, StoreVariant, StoreOptionType, StoreTaxon, StoreMetafield} from '@/types'
@@ -19,7 +19,7 @@ type StoreProduct = {
backorderable: boolean;
available: boolean;
default_variant_id: string;
- thumbnail: string | null;
+ thumbnail_url: string | null;
tags: Array<string>;
price: StorePrice;
original_price: StorePrice;