diff --git a/api/app/views/spree/api/images/_image.json.jbuilder b/api/app/views/spree/api/images/_image.json.jbuilder index c6c35b59891..7045b5bdb38 100644 --- a/api/app/views/spree/api/images/_image.json.jbuilder +++ b/api/app/views/spree/api/images/_image.json.jbuilder @@ -3,5 +3,5 @@ json.(image, *image_attributes) json.(image, :viewable_type, :viewable_id) Spree::Image.attachment_definitions[:attachment][:styles].each do |key, _value| - json.set! "#{key}_url", image.attachment.url(key) + json.set! "#{key}_url", image.url(key) end diff --git a/api/spec/requests/spree/api/images_controller_spec.rb b/api/spec/requests/spree/api/images_controller_spec.rb index fe9ca029d49..74ad540ec81 100644 --- a/api/spec/requests/spree/api/images_controller_spec.rb +++ b/api/spec/requests/spree/api/images_controller_spec.rb @@ -74,6 +74,16 @@ module Spree expect(response.status).to eq(204) expect { product_image.reload }.to raise_error(ActiveRecord::RecordNotFound) end + + it "It returns nil attribute values and noimage urls when the image cannot be found", + if: Spree::Config.image_attachment_module == Spree::Image::ActiveStorageAttachment do + product_image.attachment.blob.update(key: 11) + get spree.api_variant_images_path(product.master.id) + expect(response.status).to eq(200) + expect(json_response[:images].first[:attachment_width]).to be_nil + expect(json_response[:images].first[:attachment_height]).to be_nil + expect(json_response[:images].first[:product_url]).to include("noimage") + end end context 'when image belongs to another product' do diff --git a/core/app/models/concerns/spree/active_storage_adapter/attachment.rb b/core/app/models/concerns/spree/active_storage_adapter/attachment.rb index d78587cea70..b20d2110626 100644 --- a/core/app/models/concerns/spree/active_storage_adapter/attachment.rb +++ b/core/app/models/concerns/spree/active_storage_adapter/attachment.rb @@ -35,11 +35,11 @@ def variant(style = nil) end def height - metadata[:height] + metadata["height"] end def width - metadata[:width] + metadata["width"] end def destroy @@ -53,8 +53,9 @@ def destroy def metadata analyze unless analyzed? - @attachment.metadata + rescue ActiveStorage::FileNotFoundError + {"identified"=>nil, "width"=>nil, "height"=>nil, "analyzed"=>false} end def normalize_styles(styles)