Skip to content
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

fix(rules): automatic facet filter deserialization #753

Merged
merged 4 commits into from
Aug 20, 2021
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 @@ -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;
Expand Down Expand Up @@ -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<List<AutomaticFacetFilter>>() {});
return reader.readValue(node);
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
aallam marked this conversation as resolved.
Show resolved Hide resolved
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<AutomaticFacetFilter> deserialized =
Defaults.getObjectMapper()
.readValue(json, new TypeReference<List<AutomaticFacetFilter>>() {});

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();
}
}