Skip to content

Commit

Permalink
CIF-1955 - Venia ITs fail for productlist (#491)
Browse files Browse the repository at this point in the history
- re-fetch resource from request
- temporary fix to set proper resource types and paths to product items
  • Loading branch information
cjelger authored Feb 24, 2021
1 parent 265c331 commit 35bb7ba
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -430,11 +430,13 @@ public String getCanonicalUrl() {

@Override
public ComponentData getComponentData() {
resource = request.getResource();
return new ProductDataImpl(this, resource);
}

@Override
protected String generateId() {
resource = request.getResource();
return StringUtils.join("product", ID_SEPARATOR, StringUtils.substring(DigestUtils.sha256Hex(getSku()), 0, 10));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
package com.adobe.cq.commerce.core.components.internal.models.v1.productlist;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;
Expand All @@ -25,6 +27,7 @@
import javax.annotation.Nullable;
import javax.annotation.PostConstruct;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.sling.api.SlingHttpServletRequest;
Expand All @@ -35,6 +38,7 @@
import org.slf4j.LoggerFactory;

import com.adobe.cq.commerce.core.components.client.MagentoGraphqlClient;
import com.adobe.cq.commerce.core.components.internal.models.v1.common.ProductListItemImpl;
import com.adobe.cq.commerce.core.components.internal.models.v1.productcollection.ProductCollectionImpl;
import com.adobe.cq.commerce.core.components.models.common.ProductListItem;
import com.adobe.cq.commerce.core.components.models.productlist.ProductList;
Expand All @@ -49,6 +53,7 @@
import com.adobe.cq.commerce.magento.graphql.CategoryProducts;
import com.adobe.cq.commerce.magento.graphql.ProductInterfaceQuery;
import com.adobe.cq.sightly.SightlyWCMMode;
import com.adobe.cq.wcm.core.components.models.datalayer.ComponentData;

@Model(
adaptables = SlingHttpServletRequest.class,
Expand Down Expand Up @@ -183,7 +188,7 @@ public Collection<ProductListItem> getProducts() {
.filter(Objects::nonNull) // the converter returns null if the conversion fails
.collect(Collectors.toList());
} else {
return getSearchResultsSet().getProductListItems();
return fixProducts(getSearchResultsSet().getProductListItems());
}
}

Expand Down Expand Up @@ -242,4 +247,46 @@ public String getMetaTitle() {
public String getCanonicalUrl() {
return canonicalUrl;
}

// TODO: This a temporary fix to solve an issue caused by SlingModel caching
private Collection<ProductListItem> fixedProducts;

private Collection<ProductListItem> fixProducts(Collection<ProductListItem> products) {
if (fixedProducts != null) {
return fixedProducts;
} else if (CollectionUtils.isEmpty(products)) {
fixedProducts = Collections.emptyList();
return fixedProducts;
}

resource = request.getResource();

fixedProducts = new ArrayList<>();
for (ProductListItem product : products) {
ProductListItem productListItem = new ProductListItemImpl(product.getSKU(),
product.getSlug(),
product.getTitle(),
product.getPriceRange(),
product.getImageURL(),
productPage,
null, // search results aren't targeting specific variant
request,
urlProvider,
this.getId());
fixedProducts.add(productListItem);
}
return fixedProducts;
}

@Override
protected String generateId() {
resource = request.getResource();
return super.generateId();
}

@Override
protected ComponentData getComponentData() {
resource = request.getResource();
return super.getComponentData();
}
}

0 comments on commit 35bb7ba

Please sign in to comment.