24
24
package org .springdoc .data .rest .customisers ;
25
25
26
26
import java .lang .reflect .Field ;
27
+ import java .lang .reflect .Modifier ;
27
28
import java .lang .reflect .Type ;
28
29
import java .util .ArrayList ;
29
30
import java .util .Arrays ;
@@ -97,13 +98,12 @@ public Operation customize(Operation operation, HandlerMethod handlerMethod) {
97
98
MethodParameter parameter = methodParameters [i ];
98
99
QuerydslPredicate predicate = parameter .getParameterAnnotation (QuerydslPredicate .class );
99
100
100
- if (predicate == null ) {
101
+ if (predicate == null )
101
102
continue ;
102
- }
103
103
104
104
QuerydslBindings bindings = extractQdslBindings (predicate );
105
105
106
- Set <String > fieldsToAdd = Arrays .stream (predicate .root ().getDeclaredFields ()).map (Field ::getName ).collect (Collectors .toSet ());
106
+ Set <String > fieldsToAdd = Arrays .stream (predicate .root ().getDeclaredFields ()).filter ( field -> ! Modifier . isStatic ( field . getModifiers ())). map (Field ::getName ).collect (Collectors .toSet ());
107
107
108
108
Map <String , Object > pathSpecMap = getPathSpec (bindings , "pathSpecs" );
109
109
//remove blacklisted fields
@@ -115,17 +115,40 @@ public Operation customize(Operation operation, HandlerMethod handlerMethod) {
115
115
116
116
fieldsToAdd .addAll (aliases );
117
117
fieldsToAdd .addAll (whiteList );
118
- for (String fieldName : fieldsToAdd ) {
119
- Type type = getFieldType (fieldName , pathSpecMap , predicate .root ());
120
- io .swagger .v3 .oas .models .parameters .Parameter newParameter = buildParam (type , fieldName );
121
118
122
- parametersToAddToOperation .add (newParameter );
119
+ boolean excludeUnlistedProperties = getFieldValueOfBoolean (bindings , "excludeUnlistedProperties" );
120
+
121
+ for (String fieldName : fieldsToAdd ) {
122
+ Type type = getFieldType (fieldName , pathSpecMap , predicate .root (), excludeUnlistedProperties );
123
+ if (type != null ) {
124
+ Parameter newParameter = buildParam (type , fieldName );
125
+ parametersToAddToOperation .add (newParameter );
126
+ }
123
127
}
124
128
}
125
129
operation .getParameters ().addAll (parametersToAddToOperation );
126
130
return operation ;
127
131
}
128
132
133
+ /**
134
+ * Gets field value of boolean.
135
+ *
136
+ * @param instance the instance
137
+ * @param fieldName the field name
138
+ * @return the field value of boolean
139
+ */
140
+ private boolean getFieldValueOfBoolean (QuerydslBindings instance , String fieldName ) {
141
+ try {
142
+ Field field = FieldUtils .getDeclaredField (instance .getClass (), fieldName , true );
143
+ if (field != null )
144
+ return (boolean ) field .get (instance );
145
+ }
146
+ catch (IllegalAccessException e ) {
147
+ LOGGER .warn (e .getMessage ());
148
+ }
149
+ return false ;
150
+ }
151
+
129
152
/**
130
153
* Extract qdsl bindings querydsl bindings.
131
154
*
@@ -150,6 +173,7 @@ private QuerydslBindings extractQdslBindings(QuerydslPredicate predicate) {
150
173
*
151
174
* @param instance the instance
152
175
* @param fieldName the field name
176
+ * @param alternativeFieldName the alternative field name
153
177
* @return the field values
154
178
*/
155
179
private Set <String > getFieldValues (QuerydslBindings instance , String fieldName , String alternativeFieldName ) {
@@ -209,30 +233,26 @@ private Optional<Path<?>> getPathFromPathSpec(Object instance) {
209
233
* @param fieldName The name of the field used as reference to get the type
210
234
* @param pathSpecMap The Qdsl path specifications as defined in the resolved bindings
211
235
* @param root The root type where the paths are gotten
236
+ * @param excludeUnlistedProperties the exclude unlisted properties
212
237
* @return The type of the field. Returns
213
238
*/
214
- private Type getFieldType (String fieldName , Map <String , Object > pathSpecMap , Class <?> root ) {
239
+ private Type getFieldType (String fieldName , Map <String , Object > pathSpecMap , Class <?> root , boolean excludeUnlistedProperties ) {
240
+ Type genericType = null ;
215
241
try {
216
242
Object pathAndBinding = pathSpecMap .get (fieldName );
217
243
Optional <Path <?>> path = getPathFromPathSpec (pathAndBinding );
218
-
219
- Type genericType ;
220
- Field declaredField = null ;
244
+ Field declaredField ;
221
245
if (path .isPresent ()) {
222
246
genericType = path .get ().getType ();
223
- }
224
- else {
247
+ } else if (!excludeUnlistedProperties ) {
225
248
declaredField = root .getDeclaredField (fieldName );
226
249
genericType = declaredField .getGenericType ();
227
250
}
228
- if (genericType != null ) {
229
- return genericType ;
230
- }
231
251
}
232
252
catch (NoSuchFieldException e ) {
233
253
LOGGER .warn ("Field {} not found on {} : {}" , fieldName , root .getName (), e .getMessage ());
234
254
}
235
- return String . class ;
255
+ return genericType ;
236
256
}
237
257
238
258
/***
0 commit comments