diff --git a/algoliasearch-core/src/main/java/com/algolia/search/models/rules/AutomaticFacetFilter.java b/algoliasearch-core/src/main/java/com/algolia/search/models/rules/AutomaticFacetFilter.java index c53f19d7f..ec52865d7 100644 --- a/algoliasearch-core/src/main/java/com/algolia/search/models/rules/AutomaticFacetFilter.java +++ b/algoliasearch-core/src/main/java/com/algolia/search/models/rules/AutomaticFacetFilter.java @@ -6,7 +6,11 @@ import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.ObjectCodec; import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.*; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonDeserializer; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectReader; import java.io.IOException; import java.io.Serializable; import java.util.List; @@ -84,7 +88,7 @@ public Object deserialize(JsonParser jp, DeserializationContext ctxt) throws IOE ObjectMapper objectMapper = Defaults.getObjectMapper(); if ((!node.isNull() && node.size() > 0)) { - if (node.get(0).has("disjunctive") || node.get(0).has("score")) { + if (node.get(0).has("facet") || node.get(0).has("disjunctive") || node.get(0).has("score")) { ObjectReader reader = objectMapper.readerFor(new TypeReference>() {}); return reader.readValue(node); diff --git a/algoliasearch-core/src/test/java/com/algolia/search/serialization/TestAutomaticFacetFilterDeserializer.java b/algoliasearch-core/src/test/java/com/algolia/search/serialization/TestAutomaticFacetFilterDeserializer.java new file mode 100644 index 000000000..969d60f9e --- /dev/null +++ b/algoliasearch-core/src/test/java/com/algolia/search/serialization/TestAutomaticFacetFilterDeserializer.java @@ -0,0 +1,64 @@ +package com.algolia.search.serialization; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +import com.algolia.search.Defaults; +import com.algolia.search.models.rules.AutomaticFacetFilter; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import java.util.List; +import org.junit.jupiter.api.Test; + +public class TestAutomaticFacetFilterDeserializer { + + @Test + void testDeserializer() throws JsonProcessingException { + String json = "{\"facet\":\"brand_name\",\"disjunctive\":true,\"score\":10}"; + AutomaticFacetFilter automaticFacetFilter = + Defaults.getObjectMapper().readValue(json, AutomaticFacetFilter.class); + + assertThat(automaticFacetFilter.getFacet()).isEqualTo("brand_name"); + assertThat(automaticFacetFilter.getDisjunctive()).isTrue(); + assertThat(automaticFacetFilter.getScore()).isEqualTo(10); + } + + @Test + void testDeserializerOneAttribute() throws JsonProcessingException { + String json = "{\"facet\":\"brand_name\"}"; + AutomaticFacetFilter automaticFacetFilter = + Defaults.getObjectMapper().readValue(json, AutomaticFacetFilter.class); + + assertThat(automaticFacetFilter.getFacet()).isEqualTo("brand_name"); + assertThat(automaticFacetFilter.getDisjunctive()).isFalse(); + assertThat(automaticFacetFilter.getScore()).isNull(); + } + + @Test + void testDeserializerLegacy() throws JsonProcessingException { + String json = "\"brand_name\""; + AutomaticFacetFilter automaticFacetFilter = + Defaults.getObjectMapper().readValue(json, AutomaticFacetFilter.class); + + assertThat(automaticFacetFilter.getFacet()).isEqualTo("brand_name"); + assertThat(automaticFacetFilter.getDisjunctive()).isFalse(); + assertThat(automaticFacetFilter.getScore()).isNull(); + } + + @Test + void testDeserializerLegacyList() throws JsonProcessingException { + String json = "[\"lastname\",\"firstname\"]"; + List deserialized = + Defaults.getObjectMapper() + .readValue(json, new TypeReference>() {}); + + AutomaticFacetFilter lastname = deserialized.get(0); + assertThat(lastname.getFacet()).isEqualTo("lastname"); + assertThat(lastname.getDisjunctive()).isFalse(); + assertThat(lastname.getScore()).isNull(); + + AutomaticFacetFilter firstname = deserialized.get(1); + assertThat(firstname.getFacet()).isEqualTo("firstname"); + assertThat(firstname.getDisjunctive()).isFalse(); + assertThat(firstname.getScore()).isNull(); + } +}