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: optional filters deserialization #771

Merged
merged 7 commits into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -29,15 +29,14 @@ public List<List<String>> deserialize(JsonParser p, DeserializationContext ctxt)

switch (currentToken) {
case START_ARRAY:
List list = p.readValueAs(List.class);
if (list.stream().allMatch(String.class::isInstance)) { // are all elements strings?
result = Collections.singletonList(list);
} else {
result = buildFilters(list);
}
List<Object> list = p.readValueAs(List.class);
result = buildFilters(list);
break;
case VALUE_STRING:
result = Collections.singletonList(Arrays.asList(p.getValueAsString().split(",")));
result =
Arrays.stream(p.getValueAsString().split(","))
.map(Collections::singletonList)
.collect(Collectors.toList());
break;
case VALUE_NULL:
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,18 +171,18 @@ void serializeFacetFilters() throws IOException {
@ValueSource(strings = {"facetFilters", "optionalFilters", "tagFilters", "numericFilters"})
void testLegacyFiltersFormat(String input) throws IOException {
Ant-hem marked this conversation as resolved.
Show resolved Hide resolved

// Testing "one string" legacy filters => should be converted to "ORED" nested filters
// [["color:green","color:yellow"]]
// 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);

assertOREDResult(
assertANDEDListResult(
extractFilters(
Defaults.getObjectMapper().readValue(stringFilters, ConsequenceParams.class), input));

// Testing "one array" legacy filters => should be converted to "ORED" nested filters
// [["color:green","color:yellow"]]
// Testing "one array" legacy filters => should be converted to "ANDED" filters
// [["color:green"],["color:yellow"]]
String arrayFilters = String.format("{\"%s\":[\"color:green\",\"color:yellow\"]}", input);
assertOREDResult(
assertANDEDListResult(
extractFilters(
Defaults.getObjectMapper().readValue(arrayFilters, ConsequenceParams.class), input));

Expand Down Expand Up @@ -242,6 +242,14 @@ void assertOREDResult(List<List<String>> result) {
assertThat(result.get(0)).containsSequence("color:yellow");
}

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

void assertOREDLatestResult(List<List<String>> result) {
assertThat(result).hasSize(2);
assertThat(result.get(0)).hasSize(2);
Expand Down