Skip to content

deserialize product search facets #624

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Optional;

import com.commercetools.api.models.product.AttributeImpl;
import com.commercetools.api.models.product_search.ProductSearchFacetResult;
import com.commercetools.api.models.type.FieldContainerImpl;
import com.fasterxml.jackson.databind.module.SimpleModule;

Expand Down Expand Up @@ -35,6 +36,7 @@ public ApiModule(ModuleOptions options) {
Optional.ofNullable(options.getOption(ApiModuleOptions.DESERIALIZE_CUSTOM_FIELD_NUMBER_AS_DOUBLE))
.orElse(System.getProperty(ApiModuleOptions.DESERIALIZE_CUSTOM_FIELD_NUMBER_AS_DOUBLE)));

setMixInAnnotation(ProductSearchFacetResult.class, ProductSearchFacetResultMixin.class);
if (attributeAsJsonNode) {
setMixInAnnotation(AttributeImpl.class, AttributeJsonNodeMixin.class);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

package com.commercetools.api.json;

import java.io.IOException;
import java.util.List;

import com.commercetools.api.models.product_search.*;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeType;

public class ProductSearchFacetResultDeserializer extends JsonDeserializer<ProductSearchFacetResult> {

@Override
public ProductSearchFacetResult deserialize(JsonParser p, DeserializationContext ctx) throws IOException {

JsonNode node = p.readValueAsTree();

if (node.has("buckets")) {
return ProductSearchFacetResultBucket.builder()
.name(node.get("name").asText())
.buckets(p.getCodec()
.readValue(node.get("buckets").traverse(),
new TypeReference<List<ProductSearchFacetResultBucketEntry>>() {
}))
.build();
}
else if (node.has("value")) {
return ProductSearchFacetResultCount.builder()
.name(node.get("name").asText())
.value(node.get("value").asLong())
.build();
}
return ProductSearchFacetResult.builder().name(node.get("name").asText()).build();
}

private TypeReference<? extends ProductSearchFacetResult> typeRef(JsonNode valueNode) {
JsonNodeType valueNodeType = valueNode.getNodeType();
if (valueNodeType == JsonNodeType.OBJECT) {
if (valueNode.has("buckets")) {
return new TypeReference<ProductSearchFacetResultBucket>() {
};
}
if (valueNode.has("value")) {
return new TypeReference<ProductSearchFacetResultCount>() {
};
}
}
return new TypeReference<ProductSearchFacetResult>() {
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

package com.commercetools.api.json;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

@JsonDeserialize(using = ProductSearchFacetResultDeserializer.class)
public interface ProductSearchFacetResultMixin {
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import com.commercetools.api.models.common.LocalizedStringBuilder;
import com.commercetools.api.models.product.*;
import com.commercetools.api.models.product_search.*;
import com.commercetools.api.models.product_type.ProductTypeResourceIdentifierBuilder;
import com.fasterxml.jackson.core.JsonProcessingException;

Expand Down Expand Up @@ -53,6 +54,17 @@ public void serializeProduct() throws JsonProcessingException {
.isEqualTo("{\"masterVariant\":{\"id\":1,\"sku\":\"foo\"},\"variants\":[{\"id\":2,\"sku\":\"bar\"}]}");
}

@Test
public void productSearchFacetDeserialize() {
ProductPagedSearchResponse result = JsonUtils.fromJsonString(stringFromResource("search-facet.json"),
ProductPagedSearchResponse.class);

Assertions.assertThat(result).isNotNull();
Assertions.assertThat(result.getFacets().get(0)).isExactlyInstanceOf(ProductSearchFacetResultImpl.class);
Assertions.assertThat(result.getFacets().get(1)).isExactlyInstanceOf(ProductSearchFacetResultBucketImpl.class);
Assertions.assertThat(result.getFacets().get(2)).isExactlyInstanceOf(ProductSearchFacetResultCountImpl.class);
}

@Test
public void deepCopy() {
Product product = JsonUtils.fromJsonString(stringFromResource("product.json"), Product.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"facets": [
{
"name": "variants.attributes.result"
},
{
"name": "variants.attributes.bucket",
"buckets": [
{
"key": "red",
"count": 33
},
{
"key": "blue",
"count": 165
},
{
"key": "green",
"count": 82
}
]
},
{
"name": "variants.attributes.count",
"value": 123
}
]
}