Skip to content

Commit e070b44

Browse files
author
bnasslahsen
committed
Add support for @PageableDefault. Fixes #831.
1 parent 53e017f commit e070b44

27 files changed

+810
-64
lines changed

springdoc-openapi-common/src/main/java/org/springdoc/api/ErrorMessage.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,14 @@
2525
*/
2626
public class ErrorMessage {
2727

28+
/**
29+
* The Id.
30+
*/
2831
private UUID id;
2932

33+
/**
34+
* The Message.
35+
*/
3036
private String message;
3137

3238
/**

springdoc-openapi-common/src/main/java/org/springdoc/core/AbstractRequestBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ public Operation build(HandlerMethod handlerMethod, RequestMethod requestMethod,
231231
String[] reflectionParametersNames = Arrays.stream(handlerMethod.getMethod().getParameters()).map(java.lang.reflect.Parameter::getName).toArray(String[]::new);
232232
if (pNames == null || Arrays.stream(pNames).anyMatch(Objects::isNull))
233233
pNames = reflectionParametersNames;
234-
parameters = DelegatingMethodParameter.customize(pNames, parameters);
234+
parameters = DelegatingMethodParameter.customize(pNames, parameters,parameterBuilder.getDelegatingMethodParameterCustomizer());
235235
RequestBodyInfo requestBodyInfo = new RequestBodyInfo();
236236
List<Parameter> operationParameters = (operation.getParameters() != null) ? operation.getParameters() : new ArrayList<>();
237237
Map<String, io.swagger.v3.oas.annotations.Parameter> parametersDocMap = getApiParameters(handlerMethod.getMethod());

springdoc-openapi-common/src/main/java/org/springdoc/core/ActuatorProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ default Tag getTag() {
6363
* Is rest controller boolean.
6464
*
6565
* @param operationPath the operation path
66+
* @param controllerClass the controller class
6667
* @return the boolean
6768
*/
6869
default boolean isRestController(String operationPath, Class<?> controllerClass) {

springdoc-openapi-common/src/main/java/org/springdoc/core/DelegatingMethodParameter.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,13 @@
3030
import java.util.Arrays;
3131
import java.util.List;
3232
import java.util.Objects;
33+
import java.util.Optional;
34+
import java.util.stream.Stream;
3335

3436
import org.apache.commons.lang3.ArrayUtils;
3537
import org.springdoc.api.annotations.ParameterObject;
3638
import org.springdoc.core.converters.AdditionalModelsConverter;
39+
import org.springdoc.core.customizers.DelegatingMethodParameterCustomizer;
3740

3841
import org.springframework.core.MethodParameter;
3942
import org.springframework.core.ParameterNameDiscoverer;
@@ -79,27 +82,37 @@ public class DelegatingMethodParameter extends MethodParameter {
7982
this.delegate = delegate;
8083
this.additionalParameterAnnotations = additionalParameterAnnotations;
8184
this.parameterName = parameterName;
82-
this.isParameterObject=isParameterObject;
85+
this.isParameterObject = isParameterObject;
8386
}
8487

8588
/**
8689
* Customize method parameter [ ].
8790
*
8891
* @param pNames the p names
8992
* @param parameters the parameters
93+
* @param optionalDelegatingMethodParameterCustomizer the optional delegating method parameter customizer
9094
* @return the method parameter [ ]
9195
*/
92-
public static MethodParameter[] customize(String[] pNames, MethodParameter[] parameters) {
96+
public static MethodParameter[] customize(String[] pNames, MethodParameter[] parameters, Optional<DelegatingMethodParameterCustomizer> optionalDelegatingMethodParameterCustomizer) {
9397
List<MethodParameter> explodedParameters = new ArrayList<>();
9498
for (int i = 0; i < parameters.length; ++i) {
9599
MethodParameter p = parameters[i];
96100
Class<?> paramClass = AdditionalModelsConverter.getReplacement(p.getParameterType());
97101
if (p.hasParameterAnnotation(ParameterObject.class) || AnnotatedElementUtils.isAnnotated(paramClass, ParameterObject.class)) {
98-
MethodParameterPojoExtractor.extractFrom(paramClass).forEach(explodedParameters::add);
102+
Stream<MethodParameter> methodParameterStream = MethodParameterPojoExtractor.extractFrom(paramClass);
103+
if (!optionalDelegatingMethodParameterCustomizer.isPresent())
104+
MethodParameterPojoExtractor.extractFrom(paramClass).forEach(explodedParameters::add);
105+
else {
106+
DelegatingMethodParameterCustomizer delegatingMethodParameterCustomizer = optionalDelegatingMethodParameterCustomizer.get();
107+
methodParameterStream.forEach(methodParameter -> {
108+
delegatingMethodParameterCustomizer.customize(p, methodParameter);
109+
explodedParameters.add(methodParameter);
110+
});
111+
}
99112
}
100113
else {
101114
String name = pNames != null ? pNames[i] : p.getParameterName();
102-
explodedParameters.add(new DelegatingMethodParameter(p, name, null,false));
115+
explodedParameters.add(new DelegatingMethodParameter(p, name, null, false));
103116
}
104117
}
105118
return explodedParameters.toArray(new MethodParameter[0]);

springdoc-openapi-common/src/main/java/org/springdoc/core/GenericParameterBuilder.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.apache.commons.lang3.StringUtils;
4848
import org.slf4j.Logger;
4949
import org.slf4j.LoggerFactory;
50+
import org.springdoc.core.customizers.DelegatingMethodParameterCustomizer;
5051

5152
import org.springframework.core.MethodParameter;
5253
import org.springframework.core.ResolvableType;
@@ -65,6 +66,11 @@ public class GenericParameterBuilder {
6566
*/
6667
private static final List<Class<?>> FILE_TYPES = new ArrayList<>();
6768

69+
/**
70+
* The Optional delegating method parameter customizer.
71+
*/
72+
private final Optional<DelegatingMethodParameterCustomizer> optionalDelegatingMethodParameterCustomizer;
73+
6874
/**
6975
* The constant LOGGER.
7076
*/
@@ -84,9 +90,11 @@ public class GenericParameterBuilder {
8490
* Instantiates a new Generic parameter builder.
8591
*
8692
* @param propertyResolverUtils the property resolver utils
93+
* @param optionalDelegatingMethodParameterCustomizer the optional delegating method parameter customizer
8794
*/
88-
public GenericParameterBuilder(PropertyResolverUtils propertyResolverUtils) {
95+
public GenericParameterBuilder(PropertyResolverUtils propertyResolverUtils, Optional<DelegatingMethodParameterCustomizer> optionalDelegatingMethodParameterCustomizer) {
8996
this.propertyResolverUtils = propertyResolverUtils;
97+
this.optionalDelegatingMethodParameterCustomizer=optionalDelegatingMethodParameterCustomizer;
9098
}
9199

92100
/**
@@ -413,6 +421,15 @@ public boolean isFile(MethodParameter methodParameter) {
413421
}
414422
}
415423

424+
/**
425+
* Gets delegating method parameter customizer.
426+
*
427+
* @return the delegating method parameter customizer
428+
*/
429+
public Optional<DelegatingMethodParameterCustomizer> getDelegatingMethodParameterCustomizer() {
430+
return optionalDelegatingMethodParameterCustomizer;
431+
}
432+
416433
/**
417434
* Is file boolean.
418435
*

springdoc-openapi-common/src/main/java/org/springdoc/core/GenericResponseBuilder.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@
6767
* The type Generic response builder.
6868
* @author bnasslahsen
6969
*/
70-
@SuppressWarnings("rawtypes")
7170
public class GenericResponseBuilder {
7271

7372
/**
@@ -539,6 +538,13 @@ private Map<String, ApiResponse> getGenericMapResponse(Class<?> beanType) {
539538
.collect(LinkedHashMap::new, Map::putAll, Map::putAll);
540539
}
541540

541+
/**
542+
* Is valid http code boolean.
543+
*
544+
* @param httpCode the http code
545+
* @param methodParameter the method parameter
546+
* @return the boolean
547+
*/
542548
private boolean isValidHttpCode(String httpCode, MethodParameter methodParameter) {
543549
Set<io.swagger.v3.oas.annotations.responses.ApiResponse> responseSet = getApiResponses(methodParameter.getMethod());
544550
if (isHttpCodePresent(httpCode, responseSet))
@@ -556,6 +562,13 @@ private boolean isValidHttpCode(String httpCode, MethodParameter methodParameter
556562
return false;
557563
}
558564

565+
/**
566+
* Is http code present boolean.
567+
*
568+
* @param httpCode the http code
569+
* @param responseSet the response set
570+
* @return the boolean
571+
*/
559572
private boolean isHttpCodePresent(String httpCode, Set<io.swagger.v3.oas.annotations.responses.ApiResponse> responseSet) {
560573
return !responseSet.isEmpty() && responseSet.stream().anyMatch(apiResponseAnnotations -> httpCode.equals(apiResponseAnnotations.responseCode()));
561574
}

springdoc-openapi-common/src/main/java/org/springdoc/core/ReturnTypeParser.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ else if (resolvedType.hasGenerics()) {
7373
return genericType;
7474
}
7575

76+
/**
77+
* Resolve type.
78+
*
79+
* @param resolvableTypes the resolvable types
80+
* @param contextClass the context class
81+
*/
7682
static void resolveType(ResolvableType[] resolvableTypes, Class<?> contextClass) {
7783
for (int i = 0; i < resolvableTypes.length; i++) {
7884
if (resolvableTypes[i].hasUnresolvableGenerics() && resolvableTypes[i].getType() instanceof ParameterizedType) {
@@ -88,6 +94,13 @@ else if (resolvableTypes[i].hasGenerics()) {
8894
}
8995
}
9096

97+
/**
98+
* Gets resolved type.
99+
*
100+
* @param resolvableType the resolvable type
101+
* @param contextClass the context class
102+
* @return the resolved type
103+
*/
91104
static ResolvableType getResolvedType(ResolvableType resolvableType, Class<?> contextClass) {
92105
ParameterizedType parameterizedType = (ParameterizedType) resolvableType.getType();
93106
Class<?>[] generics = new Class<?>[parameterizedType.getActualTypeArguments().length];

springdoc-openapi-common/src/main/java/org/springdoc/core/SpringDocConfiguration.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.springdoc.core.converters.PropertyCustomizingConverter;
4242
import org.springdoc.core.converters.ResponseSupportConverter;
4343
import org.springdoc.core.converters.SchemaPropertyDeprecatingConverter;
44+
import org.springdoc.core.customizers.DelegatingMethodParameterCustomizer;
4445
import org.springdoc.core.customizers.OpenApiBuilderCustomiser;
4546
import org.springdoc.core.customizers.OpenApiCustomiser;
4647
import org.springdoc.core.customizers.PropertyCustomizer;
@@ -271,12 +272,13 @@ ReturnTypeParser genericReturnTypeParser() {
271272
* Parameter builder generic parameter builder.
272273
*
273274
* @param propertyResolverUtils the property resolver utils
275+
* @param optionalDelegatingMethodParameterCustomizer the optional delegating method parameter customizer
274276
* @return the generic parameter builder
275277
*/
276278
@Bean
277279
@ConditionalOnMissingBean
278-
GenericParameterBuilder parameterBuilder(PropertyResolverUtils propertyResolverUtils) {
279-
return new GenericParameterBuilder(propertyResolverUtils);
280+
GenericParameterBuilder parameterBuilder(PropertyResolverUtils propertyResolverUtils, Optional<DelegatingMethodParameterCustomizer> optionalDelegatingMethodParameterCustomizer) {
281+
return new GenericParameterBuilder(propertyResolverUtils,optionalDelegatingMethodParameterCustomizer);
280282
}
281283

282284
/**

springdoc-openapi-common/src/main/java/org/springdoc/core/converters/ConverterUtils.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public static void addResponseTypeToIgnore(Class<?> cls) {
8484
/**
8585
* Is response type wrapper boolean.
8686
*
87-
* @param rawClass the raw class
87+
* @param rawClass the raw class
8888
* @return the boolean
8989
*/
9090
public static boolean isResponseTypeWrapper(Class<?> rawClass) {
@@ -94,7 +94,7 @@ public static boolean isResponseTypeWrapper(Class<?> rawClass) {
9494
/**
9595
* Is response type to ignore boolean.
9696
*
97-
* @param rawClass the raw class
97+
* @param rawClass the raw class
9898
* @return the boolean
9999
*/
100100
public static boolean isResponseTypeToIgnore(Class<?> rawClass) {
@@ -126,7 +126,7 @@ public static void removeResponseTypeToIgnore(Class<?> classes) {
126126
/**
127127
* Is flux type wrapper boolean.
128128
*
129-
* @param rawClass the raw class
129+
* @param rawClass the raw class
130130
* @return the boolean
131131
*/
132132
public static boolean isFluxTypeWrapper(Class<?> rawClass) {

springdoc-openapi-common/src/main/java/org/springdoc/core/converters/models/DefaultPageable.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public class DefaultPageable extends Pageable {
3434
/**
3535
* Instantiates a new Default pageable.
3636
*
37-
* @param page the page
38-
* @param size the size
37+
* @param page the page
38+
* @param size the size
3939
* @param sort the sort
4040
*/
4141
public DefaultPageable(int page, int size, List<String> sort) {

springdoc-openapi-common/src/main/java/org/springdoc/core/converters/models/Pageable.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ public class Pageable {
6161
/**
6262
* Instantiates a new Pageable.
6363
*
64-
* @param page the page
65-
* @param size the size
64+
* @param page the page
65+
* @param size the size
6666
* @param sort the sort
6767
*/
6868
public Pageable(int page, int size, List<String> sort) {

springdoc-openapi-common/src/main/java/org/springdoc/core/customizers/ActuatorOpenApiCustomiser.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
*/
2020
public class ActuatorOpenApiCustomiser implements OpenApiCustomiser {
2121

22+
/**
23+
* The Path pathern.
24+
*/
2225
private final Pattern pathPathern = Pattern.compile("\\{(.*?)}");
2326

2427
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.springdoc.core.customizers;
2+
3+
import org.springframework.core.MethodParameter;
4+
5+
/**
6+
* The interface Delegating method parameter customizer.
7+
*/
8+
@FunctionalInterface
9+
public interface DelegatingMethodParameterCustomizer {
10+
11+
/**
12+
* Customize.
13+
*
14+
* @param originalParameter the original parameter
15+
* @param methodParameter the method parameter
16+
*/
17+
void customize(MethodParameter originalParameter, MethodParameter methodParameter);
18+
19+
}

springdoc-openapi-common/src/main/java/org/springdoc/core/customizers/ParameterCustomizer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public interface ParameterCustomizer {
3434
/**
3535
* Customize parameter.
3636
*
37-
* @param parameterModel to be customized
38-
* @param methodParameter original parameter from handler method
37+
* @param parameterModel to be customized
38+
* @param methodParameter original parameter from handler method
3939
* @return customized parameter
4040
*/
4141
Parameter customize(Parameter parameterModel, MethodParameter methodParameter);

springdoc-openapi-common/src/main/java/org/springdoc/core/fn/AbstractRouterFunctionVisitor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void path(String pattern) {
6464
/**
6565
* Header.
6666
*
67-
* @param name the name
67+
* @param name the name
6868
* @param value the value
6969
*/
7070
public void header(String name, String value) {
@@ -88,7 +88,7 @@ public List<RouterFunctionData> getRouterFunctionDatas() {
8888
/**
8989
* Query param.
9090
*
91-
* @param name the name
91+
* @param name the name
9292
* @param value the value
9393
*/
9494
public void queryParam(String name, String value) {
@@ -107,7 +107,7 @@ public void pathExtension(String extension) {
107107
/**
108108
* Param.
109109
*
110-
* @param name the name
110+
* @param name the name
111111
* @param value the value
112112
*/
113113
public void param(String name, String value) {

springdoc-openapi-common/src/main/java/org/springdoc/core/fn/RouterFunctionData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public Map<String, String> getQueryParams() {
9999
/**
100100
* Add query params.
101101
*
102-
* @param name the name
102+
* @param name the name
103103
* @param value the value
104104
*/
105105
public void addQueryParams(String name, String value) {

springdoc-openapi-common/src/main/java/org/springdoc/core/fn/RouterOperation.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public RouterOperation(org.springdoc.core.annotations.RouterOperation routerOper
112112
/**
113113
* Instantiates a new Router operation.
114114
*
115-
* @param routerOperationAnnotation the router operation annotation
115+
* @param routerOperationAnnotation the router operation annotation
116116
* @param routerFunctionData the router function data
117117
*/
118118
public RouterOperation(org.springdoc.core.annotations.RouterOperation routerOperationAnnotation, RouterFunctionData routerFunctionData) {
@@ -131,7 +131,7 @@ public RouterOperation(org.springdoc.core.annotations.RouterOperation routerOper
131131
/**
132132
* Instantiates a new Router operation.
133133
*
134-
* @param path the path
134+
* @param path the path
135135
* @param methods the methods
136136
*/
137137
public RouterOperation(String path, RequestMethod[] methods) {
@@ -344,7 +344,7 @@ public int compareTo(RouterOperation routerOperation) {
344344
result = methods[0].compareTo(routerOperation.getMethods()[0]);
345345
if (result == 0 && operationModel != null && routerOperation.getOperationModel() != null)
346346
result = operationModel.getOperationId().compareTo(routerOperation.getOperationModel().getOperationId());
347-
if (result == 0 && operation != null && operation.operationId() != null && routerOperation.getOperation().operationId() != null)
347+
if (result == 0 && operation != null)
348348
result = operation.operationId().compareTo(routerOperation.getOperation().operationId());
349349
return result;
350350
}

0 commit comments

Comments
 (0)