Skip to content

Commit

Permalink
fix(rules): deserialize optional filters (#758)
Browse files Browse the repository at this point in the history
  • Loading branch information
aallam authored Oct 14, 2021
1 parent e362d90 commit 89251ff
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class PersonalizationTest

private static final PersonalizationClient personalizationClient =
DefaultPersonalizationClient.create(
TestHelpers.ALGOLIA_APPLICATION_ID_1, TestHelpers.ALGOLIA_ADMIN_KEY_1, "eu");
TestHelpers.ALGOLIA_APPLICATION_ID_1, TestHelpers.ALGOLIA_ADMIN_KEY_1, "us");

PersonalizationTest() {
super(personalizationClient);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

class FiltersJsonDeserializer extends JsonDeserializer {
class FiltersJsonDeserializer extends JsonDeserializer<List<List<String>>> {

/**
* Algolia's specific deserializer handling multiple form of (legacy) filters This reader is
Expand All @@ -19,7 +20,8 @@ class FiltersJsonDeserializer extends JsonDeserializer {
*/
@Override
@SuppressWarnings("unchecked")
public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
public List<List<String>> deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException {

JsonToken currentToken = p.getCurrentToken();

Expand All @@ -28,10 +30,10 @@ public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOEx
switch (currentToken) {
case START_ARRAY:
List list = p.readValueAs(List.class);
if (list.get(0) instanceof String) {
if (list.stream().allMatch(String.class::isInstance)) { // are all elements strings?
result = Collections.singletonList(list);
} else {
result = list;
result = buildFilters(list);
}
break;
case VALUE_STRING:
Expand All @@ -46,4 +48,19 @@ public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOEx

return result;
}

@SuppressWarnings("unchecked")
private List<List<String>> buildFilters(List list) {
return (List<List<String>>)
list.stream()
.map(
element -> {
if (element instanceof String) {
return Collections.singletonList((String) element);
} else {
return element; // we suppose it's a List<String>
}
})
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,16 @@ void testLegacyFiltersFormat(String input) throws IOException {
Defaults.getObjectMapper().readValue(nestedArrayFilters, ConsequenceParams.class),
input));

// Testing mixed case with array and string
// [["color:green","color:yellow"],"color:blue"]
String stringAndArrayFilters =
String.format("{\"%s\":[[\"color:green\",\"color:yellow\"],\"color:blue\"]}", input);
List<List<String>> mixedDeserialized =
extractFilters(
Defaults.getObjectMapper().readValue(stringAndArrayFilters, ConsequenceParams.class),
input);
assertOREDLatestResult(mixedDeserialized);

// Testing the latest format of filters i.e nested arrays
String nestedAndedArrayFilters =
String.format("{\"%s\":[[\"color:green\",\"color:yellow\"],[\"color:blue\"]]}", input);
Expand All @@ -220,13 +230,7 @@ void testLegacyFiltersFormat(String input) throws IOException {
extractFilters(
Defaults.getObjectMapper().readValue(nestedAndedArrayFilters, ConsequenceParams.class),
input);

assertThat(deserialized).hasSize(2);
assertThat(deserialized.get(0)).hasSize(2);
assertThat(deserialized.get(0).get(0)).containsSubsequence("color:green");
assertThat(deserialized.get(0).get(1)).containsSubsequence("color:yellow");
assertThat(deserialized.get(1)).hasSize(1);
assertThat(deserialized.get(1).get(0)).containsSubsequence("color:blue");
assertOREDLatestResult(deserialized);
}

List<List<String>> extractFilters(ConsequenceParams consequenceParams, String input) {
Expand Down Expand Up @@ -257,6 +261,15 @@ void assertOREDResult(List<List<String>> result) {
assertThat(result.get(0)).containsSequence("color:yellow");
}

void assertOREDLatestResult(List<List<String>> result) {
assertThat(result).hasSize(2);
assertThat(result.get(0)).hasSize(2);
assertThat(result.get(0).get(0)).containsSubsequence("color:green");
assertThat(result.get(0).get(1)).containsSubsequence("color:yellow");
assertThat(result.get(1)).hasSize(1);
assertThat(result.get(1).get(0)).containsSubsequence("color:blue");
}

@Test
void testFiltersJSONDeserializerNonRegression() throws IOException {

Expand Down

0 comments on commit 89251ff

Please sign in to comment.