From 23c4b0ba45c8c79f6b9b78dd5199d837fa2db721 Mon Sep 17 00:00:00 2001 From: cdqpuk Date: Thu, 5 Nov 2020 09:17:09 +0100 Subject: [PATCH] #925: Correct behavior for QuerydslPredicateOperationCustomizer#excludeUnlistedProperties --- .../QuerydslPredicateOperationCustomizer.java | 14 ++++++++------ .../springdoc/api/app19/ApplicationPredicate.java | 1 + .../src/test/resources/results/app19.json | 8 ++++++++ 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/springdoc-openapi-data-rest/src/main/java/org/springdoc/data/rest/customisers/QuerydslPredicateOperationCustomizer.java b/springdoc-openapi-data-rest/src/main/java/org/springdoc/data/rest/customisers/QuerydslPredicateOperationCustomizer.java index e7a8a7548..373e64d8b 100644 --- a/springdoc-openapi-data-rest/src/main/java/org/springdoc/data/rest/customisers/QuerydslPredicateOperationCustomizer.java +++ b/springdoc-openapi-data-rest/src/main/java/org/springdoc/data/rest/customisers/QuerydslPredicateOperationCustomizer.java @@ -116,10 +116,13 @@ public Operation customize(Operation operation, HandlerMethod handlerMethod) { fieldsToAdd.addAll(aliases); fieldsToAdd.addAll(whiteList); - boolean excludeUnlistedProperties = getFieldValueOfBoolean(bindings, "excludeUnlistedProperties"); + // if only listed properties should be included, remove all other fields from fieldsToAdd + if (getFieldValueOfBoolean(bindings, "excludeUnlistedProperties")) { + fieldsToAdd.removeIf(s -> !whiteList.contains(s)); + } for (String fieldName : fieldsToAdd) { - Type type = getFieldType(fieldName, pathSpecMap, predicate.root(), excludeUnlistedProperties); + Type type = getFieldType(fieldName, pathSpecMap, predicate.root()); if (type != null) { Parameter newParameter = buildParam(type, fieldName); parametersToAddToOperation.add(newParameter); @@ -232,11 +235,10 @@ private Optional> getPathFromPathSpec(Object instance) { * Tries to figure out the Type of the field. It first checks the Qdsl pathSpecMap before checking the root class. Defaults to String.class * @param fieldName The name of the field used as reference to get the type * @param pathSpecMap The Qdsl path specifications as defined in the resolved bindings - * @param root The root type where the paths are gotten - * @param excludeUnlistedProperties the exclude unlisted properties + * @param root The root type where the paths are gotten * @return The type of the field. Returns */ - private Type getFieldType(String fieldName, Map pathSpecMap, Class root, boolean excludeUnlistedProperties) { + private Type getFieldType(String fieldName, Map pathSpecMap, Class root) { Type genericType = null; try { Object pathAndBinding = pathSpecMap.get(fieldName); @@ -244,7 +246,7 @@ private Type getFieldType(String fieldName, Map pathSpecMap, Cla Field declaredField; if (path.isPresent()) { genericType = path.get().getType(); - } else if (!excludeUnlistedProperties) { + } else { declaredField = root.getDeclaredField(fieldName); genericType = declaredField.getGenericType(); } diff --git a/springdoc-openapi-data-rest/src/test/java/test/org/springdoc/api/app19/ApplicationPredicate.java b/springdoc-openapi-data-rest/src/test/java/test/org/springdoc/api/app19/ApplicationPredicate.java index 43a7336fa..54ce71c5c 100644 --- a/springdoc-openapi-data-rest/src/test/java/test/org/springdoc/api/app19/ApplicationPredicate.java +++ b/springdoc-openapi-data-rest/src/test/java/test/org/springdoc/api/app19/ApplicationPredicate.java @@ -11,6 +11,7 @@ public class ApplicationPredicate implements QuerydslBinderCustomizer