From 2a00803b43d6411562a19d011fd07d96fac7c52b Mon Sep 17 00:00:00 2001 From: Mouaad Aallam Date: Tue, 1 Feb 2022 12:17:29 +0100 Subject: [PATCH] support legacy string with parenthesis --- .../indexing/FiltersJsonDeserializer.java | 20 +++++++++++++++---- .../com/algolia/search/JacksonParserTest.java | 8 +++++++- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/algoliasearch-core/src/main/java/com/algolia/search/models/indexing/FiltersJsonDeserializer.java b/algoliasearch-core/src/main/java/com/algolia/search/models/indexing/FiltersJsonDeserializer.java index 0f0e66b4f..df8f20a86 100644 --- a/algoliasearch-core/src/main/java/com/algolia/search/models/indexing/FiltersJsonDeserializer.java +++ b/algoliasearch-core/src/main/java/com/algolia/search/models/indexing/FiltersJsonDeserializer.java @@ -5,6 +5,7 @@ import com.fasterxml.jackson.core.JsonToken; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonDeserializer; + import java.io.IOException; import java.util.Arrays; import java.util.Collections; @@ -33,10 +34,8 @@ public List> deserialize(JsonParser p, DeserializationContext ctxt) result = buildFilters(list); break; case VALUE_STRING: - result = - Arrays.stream(p.getValueAsString().split(",")) - .map(Collections::singletonList) - .collect(Collectors.toList()); + String string = p.getValueAsString(); + result = buildFilters(string); break; case VALUE_NULL: break; @@ -48,6 +47,7 @@ public List> deserialize(JsonParser p, DeserializationContext ctxt) return result; } + /** Build filters from a list */ @SuppressWarnings("unchecked") private List> buildFilters(List list) { return (List>) @@ -62,4 +62,16 @@ private List> buildFilters(List list) { }) .collect(Collectors.toList()); } + + /** Build filters from (legacy) string */ + private List> buildFilters(String string) { + if (string.startsWith("(") && string.endsWith(")")) { + String input = string.substring(1, string.length() - 1); + return Collections.singletonList(Arrays.asList(input.split(","))); + } else { + return Arrays.stream(string.split(",")) + .map(Collections::singletonList) + .collect(Collectors.toList()); + } + } } diff --git a/algoliasearch-core/src/test/java/com/algolia/search/JacksonParserTest.java b/algoliasearch-core/src/test/java/com/algolia/search/JacksonParserTest.java index ef0816058..c1b898cbd 100644 --- a/algoliasearch-core/src/test/java/com/algolia/search/JacksonParserTest.java +++ b/algoliasearch-core/src/test/java/com/algolia/search/JacksonParserTest.java @@ -174,7 +174,6 @@ void testLegacyFiltersFormat(String input) throws IOException { // Testing "one string" legacy filters => should be converted to "ANDED" filters // [["color:green"],["color:yellow"]] String stringFilters = String.format("{\"%s\":\"color:green,color:yellow\"}", input); - assertANDEDListResult( extractFilters( Defaults.getObjectMapper().readValue(stringFilters, ConsequenceParams.class), input)); @@ -193,6 +192,13 @@ void testLegacyFiltersFormat(String input) throws IOException { Defaults.getObjectMapper().readValue(nestedArrayFilters, ConsequenceParams.class), input)); + // Testing "one string with parenthesis" legacy filters => should be converted to "ORED" filters + // [["color:green", "color:yellow"]] + String stringParenthesisFilters = String.format("{\"%s\":\"(color:green,color:yellow)\"}", input); + assertOREDResult( + extractFilters( + Defaults.getObjectMapper().readValue(stringParenthesisFilters, ConsequenceParams.class), input)); + // Testing mixed case with array and string // [["color:green","color:yellow"],"color:blue"] String stringAndArrayFilters =