Skip to content

Commit 1463159

Browse files
authored
CIF-1314 - Product list doesn't display product without small image (#226)
* fix NPE in product list when small image is missing for a product * fix NPE in product carousel when thumbnail is missing for a product
1 parent de52353 commit 1463159

File tree

6 files changed

+22
-11
lines changed

6 files changed

+22
-11
lines changed

bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/models/v1/productcarousel/ProductCarouselImpl.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import com.adobe.cq.commerce.core.components.utils.SiteNavigation;
4646
import com.adobe.cq.commerce.magento.graphql.ConfigurableProduct;
4747
import com.adobe.cq.commerce.magento.graphql.ConfigurableVariant;
48+
import com.adobe.cq.commerce.magento.graphql.ProductImage;
4849
import com.adobe.cq.commerce.magento.graphql.ProductInterface;
4950
import com.adobe.cq.commerce.magento.graphql.SimpleProduct;
5051
import com.day.cq.wcm.api.Page;
@@ -144,12 +145,13 @@ public List<ProductListItem> getProducts() {
144145

145146
try {
146147
Price price = new PriceImpl(product.getPriceRange(), locale);
148+
ProductImage thumbnail = product.getThumbnail();
147149
carouselProductList.add(new ProductListItemImpl(
148150
skus.getLeft(),
149151
slug,
150152
product.getName(),
151153
price,
152-
product.getThumbnail().getUrl(),
154+
thumbnail == null ? null : thumbnail.getUrl(),
153155
productPage,
154156
skus.getRight(),
155157
request));

bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/models/v1/productlist/ProductListImpl.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import com.adobe.cq.commerce.core.components.models.retriever.AbstractCategoryRetriever;
4545
import com.adobe.cq.commerce.core.components.utils.SiteNavigation;
4646
import com.adobe.cq.commerce.magento.graphql.CategoryProducts;
47+
import com.adobe.cq.commerce.magento.graphql.ProductImage;
4748
import com.adobe.cq.commerce.magento.graphql.ProductInterface;
4849
import com.adobe.cq.sightly.SightlyWCMMode;
4950
import com.day.cq.wcm.api.Page;
@@ -216,12 +217,13 @@ public Collection<ProductListItem> getProducts() {
216217
for (ProductInterface product : products.getItems()) {
217218
try {
218219
Price price = new PriceImpl(product.getPriceRange(), locale);
220+
ProductImage smallImage = product.getSmallImage();
219221
listItems.add(new ProductListItemImpl(
220222
product.getSku(),
221223
product.getUrlKey(),
222224
product.getName(),
223225
price,
224-
product.getSmallImage().getUrl(),
226+
smallImage == null ? null : smallImage.getUrl(),
225227
productPage,
226228
null,
227229
request));

bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/models/v1/productcarousel/ProductCarouselImplTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import com.adobe.cq.commerce.magento.graphql.ConfigurableProduct;
4040
import com.adobe.cq.commerce.magento.graphql.ConfigurableVariant;
4141
import com.adobe.cq.commerce.magento.graphql.Money;
42+
import com.adobe.cq.commerce.magento.graphql.ProductImage;
4243
import com.adobe.cq.commerce.magento.graphql.ProductInterface;
4344
import com.adobe.cq.commerce.magento.graphql.Query;
4445
import com.day.cq.wcm.api.Page;
@@ -134,7 +135,13 @@ public void getProducts() {
134135
priceFormatter.setCurrency(Currency.getInstance(amount.getCurrency().toString()));
135136
Assert.assertEquals(priceFormatter.format(amount.getValue()), item.getFormattedPrice());
136137

137-
Assert.assertEquals(productOrVariant.getThumbnail().getUrl(), item.getImageURL());
138+
ProductImage thumbnail = productOrVariant.getThumbnail();
139+
if (thumbnail == null) {
140+
// if thumbnail is missing for a product in GraphQL response then thumbnail is null for the related item
141+
Assert.assertNull(item.getImageURL());
142+
} else {
143+
Assert.assertEquals(thumbnail.getUrl(), item.getImageURL());
144+
}
138145
idx++;
139146
}
140147
}

bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/models/v1/productlist/ProductListImplTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import com.adobe.cq.commerce.graphql.client.GraphqlClient;
4141
import com.adobe.cq.commerce.magento.graphql.CategoryInterface;
4242
import com.adobe.cq.commerce.magento.graphql.CategoryTree;
43+
import com.adobe.cq.commerce.magento.graphql.ProductImage;
4344
import com.adobe.cq.commerce.magento.graphql.ProductInterface;
4445
import com.adobe.cq.commerce.magento.graphql.Query;
4546
import com.adobe.cq.commerce.magento.graphql.StoreConfig;
@@ -170,7 +171,13 @@ public void getProducts() {
170171
Assert.assertEquals(priceFormatter.format(productInterface.getPriceRange().getMinimumPrice().getFinalPrice().getValue()), item
171172
.getFormattedPrice());
172173

173-
Assert.assertEquals(productInterface.getSmallImage().getUrl(), item.getImageURL());
174+
ProductImage smallImage = productInterface.getSmallImage();
175+
if (smallImage == null) {
176+
// if small image is missing for a product in GraphQL response then image URL is null for the related item
177+
Assert.assertNull(item.getImageURL());
178+
} else {
179+
Assert.assertEquals(smallImage.getUrl(), item.getImageURL());
180+
}
174181
}
175182
}
176183

bundles/core/src/test/resources/graphql/magento-graphql-category-result.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,6 @@
130130
"id": 1099,
131131
"sku": "eqrusugor",
132132
"name": "GoMobile Fitness Monitor/Smart Music Player",
133-
"small_image": {
134-
"url": "https://some-hostname.magentosite.cloud/media/catalog/product/g/o/gomobile_wrist_2.jpg"
135-
},
136133
"url_key": "gomobile-fitness-monitor-smart-music-player",
137134
"price_range": {
138135
"minimum_price": {

bundles/core/src/test/resources/graphql/magento-graphql-productcarousel-result.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -442,10 +442,6 @@
442442
"__typename": "ConfigurableProduct",
443443
"sku": "WJ01",
444444
"name": "Stellar Solar Jacket",
445-
"thumbnail": {
446-
"label": "Stellar Solar Jacket",
447-
"url": "https://test-url.magentosite.cloud/media/wj01-red_main_6.jpg"
448-
},
449445
"url_key": "stellar-solar-jacket",
450446
"price_range": {
451447
"minimum_price": {

0 commit comments

Comments
 (0)