Skip to content

Commit

Permalink
Server responds with 500 when filter contains empty conditions array #87
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitaShchienko committed Feb 18, 2020
1 parent 0bb8bc0 commit 374d307
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,15 @@ public EntitiesSearchResult searchEntities(String entityName,
throw new RestAPIException("Cannot parse entities filter", e.getMessage(), HttpStatus.BAD_REQUEST, e);
}

String jpqlWhere = filterParseResult.getJpqlWhere().replace("{E}", "e");
String jpqlWhere = filterParseResult.getJpqlWhere();
Map<String, Object> queryParameters = filterParseResult.getQueryParameters();

String queryString = "select e from " + entityName + " e where " + jpqlWhere;
String queryString = "select e from " + entityName + " e";

if (jpqlWhere != null) {
queryString += " where " + jpqlWhere.replace("{E}", "e");
}

String json = _loadEntitiesList(queryString, viewName, limit, offset, sort, returnNulls,
dynamicAttributes, modelVersion, metaClass, queryParameters);
Long count = null;
Expand Down Expand Up @@ -225,11 +230,18 @@ public Long countSearchEntities(String entityName,
throw new RestAPIException("Cannot parse entities filter", e.getMessage(), HttpStatus.BAD_REQUEST, e);
}

String jpqlWhere = filterParseResult.getJpqlWhere().replace("{E}", "e");
String jpqlWhere = filterParseResult.getJpqlWhere();
Map<String, Object> queryParameters = filterParseResult.getQueryParameters();

String queryString = "select count(e) from " + entityName + " e where " + jpqlWhere;
return dataManager.loadValue(queryString, Long.class).setParameters(queryParameters).one();
String queryString = "select count(e) from " + entityName + " e";

if (jpqlWhere != null) {
queryString += " where " + jpqlWhere.replace("{E}", "e");
}

return dataManager.loadValue(queryString, Long.class)
.setParameters(queryParameters)
.one();
}

public EntitiesSearchResult searchEntities(String entityName, String searchRequestBody) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,23 @@ public RestFilterParseResult parse(String filterJson, MetaClass metaClass) throw
rootCondition.setType(RestFilterGroupCondition.Type.AND);

JsonObject filterObject = new JsonParser().parse(filterJson).getAsJsonObject();
JsonArray conditions = filterObject.get("conditions").getAsJsonArray();
if (conditions != null) {
for (JsonElement conditionElement : conditions) {
JsonObject conditionObject = conditionElement.getAsJsonObject();
RestFilterCondition restFilterCondition = parseConditionObject(conditionObject, metaClass);
rootCondition.getConditions().add(restFilterCondition);
JsonElement conditions = filterObject.get("conditions");
if (conditions != null && conditions.isJsonArray()) {
JsonArray conditionsJsonArray = conditions.getAsJsonArray();
if (conditionsJsonArray.size() != 0) {
for (JsonElement conditionElement : conditionsJsonArray) {
JsonObject conditionObject = conditionElement.getAsJsonObject();
RestFilterCondition restFilterCondition = parseConditionObject(conditionObject, metaClass);
rootCondition.getConditions().add(restFilterCondition);
}
Map<String, Object> queryParameters = new HashMap<>();
collectQueryParameters(rootCondition, queryParameters);

return new RestFilterParseResult(rootCondition.toJpql(), queryParameters);
}
}

Map<String, Object> queryParameters = new HashMap<>();
collectQueryParameters(rootCondition, queryParameters);

return new RestFilterParseResult(rootCondition.toJpql(), queryParameters);
return new RestFilterParseResult(null, null);
}

protected void collectQueryParameters(RestFilterCondition condition, Map<String, Object> queryParameters) {
Expand Down

0 comments on commit 374d307

Please sign in to comment.