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

MSEARCH-393 Fix date query validation to support ISO date/time formats #277

Merged
merged 5 commits into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
28 changes: 25 additions & 3 deletions src/main/java/org/folio/search/cql/CqlTermQueryConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import static java.util.Collections.unmodifiableMap;
import static java.util.stream.Collectors.joining;
import static org.apache.commons.lang3.StringUtils.lowerCase;
import static org.apache.commons.validator.GenericValidator.isDate;
import static org.folio.search.utils.SearchUtils.ASTERISKS_SIGN;
import static org.opensearch.index.query.QueryBuilders.matchAllQuery;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
Expand Down Expand Up @@ -34,7 +34,16 @@ public class CqlTermQueryConverter {
public static final String WILDCARD_OPERATOR = "wildcard";
private static final String MATCH_ALL_CQL_QUERY = "cql.allRecords = 1";
private static final String KEYWORD_ALL_CQL_QUERY = "keyword = *";
private static final String STRICT_DATE_PATTERN = "yyyy-MM-dd";

private static final List<DateTimeFormatter> SUPPORTED_DATE_FORMATS = List.of(
DateTimeFormatter.ISO_DATE,
DateTimeFormatter.ISO_DATE_TIME,
DateTimeFormatter.ISO_LOCAL_DATE,
DateTimeFormatter.ISO_LOCAL_DATE_TIME,
DateTimeFormatter.ISO_OFFSET_DATE,
DateTimeFormatter.ISO_OFFSET_DATE_TIME,
DateTimeFormatter.ISO_INSTANT
);

private final SearchFieldProvider searchFieldProvider;
private final Map<String, TermQueryBuilder> termQueryBuilders;
Expand Down Expand Up @@ -143,11 +152,24 @@ private static Map<String, TermQueryBuilder> getTermQueryProvidersAsMap(List<Ter

private void validateIndexFormat(String index, CQLTermNode termNode) {
var value = termNode.getTerm();
if (index.equals("date") && !isDate(value, STRICT_DATE_PATTERN, true)) {
if (index.equals("date") && !isValidDate(value)) {
throw new ValidationException("Invalid date format", termNode.getIndex(), value);
}
}

private boolean isValidDate(String value) {
boolean isValidDate = false;
for (DateTimeFormatter dateFormat : SUPPORTED_DATE_FORMATS) {
try {
dateFormat.parse(value);
isValidDate = true;
} catch (Exception ignored) {
// do nothing
}
}
return isValidDate;
psmagin marked this conversation as resolved.
Show resolved Hide resolved
}

private static boolean isMatchAllQuery(String cqlQuery) {
return MATCH_ALL_CQL_QUERY.equals(cqlQuery) || KEYWORD_ALL_CQL_QUERY.equals(cqlQuery);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ private static Stream<Arguments> filteredSearchQueriesProvider() {
arguments("(subjectHeadings==\"n\")", List.of(IDS[11], IDS[12])),

arguments("(metadata.createdDate >= 2021-03-01) ", List.of(IDS[0], IDS[1], IDS[2], IDS[3])),
arguments("(metadata.createdDate >= 2021-03-01T00:00:00.000) ", List.of(IDS[0], IDS[1], IDS[2], IDS[3])),
arguments("(metadata.createdDate >= 2021-03-01T00:00:00.000Z) ", List.of(IDS[0], IDS[1], IDS[2], IDS[3])),
arguments("(metadata.createdDate >= 2021-03-01T00:00:00.000+00:00) ", List.of(IDS[0], IDS[1], IDS[2], IDS[3])),
arguments("(metadata.createdDate > 2021-03-01) ", List.of(IDS[1], IDS[2], IDS[3])),
arguments("(metadata.createdDate >= 2021-03-01 and metadata.createdDate < 2021-03-10) ",
List.of(IDS[0], IDS[2])),
Expand All @@ -158,8 +161,7 @@ private static Stream<Arguments> invalidDateSearchQueriesProvider() {
arguments("metadata.createdDate", "2022-06-1"),
arguments("metadata.createdDate", "2022-06-40"),
arguments("metadata.updatedDate", "2022-15-01"),
arguments("metadata.updatedDate", "invalidDate"),
arguments("metadata.updatedDate", "2022-11-15T15:00:00.000")
arguments("metadata.updatedDate", "invalidDate")
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,7 @@ private static Stream<Arguments> invalidDateSearchQueriesProvider() {
arguments("holdings.metadata.createdDate", "2022-15-01"),
arguments("holdings.metadata.updatedDate", "2022-06-40"),

arguments("item.metadata.updatedDate", "invalidDate"),
arguments("item.metadata.createdDate", "2022-06-15T15:00:00.000")
arguments("item.metadata.updatedDate", "invalidDate")
);
}

Expand Down