Skip to content

Commit 9852d53

Browse files
author
bnasslahsen
committed
code review
1 parent fa84aa0 commit 9852d53

File tree

5 files changed

+25
-40
lines changed

5 files changed

+25
-40
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,16 +227,17 @@ public Operation build(HandlerMethod handlerMethod, RequestMethod requestMethod,
227227
operation.setOperationId(operationId);
228228
// requests
229229
String[] pNames = this.localSpringDocParameterNameDiscoverer.getParameterNames(handlerMethod.getMethod());
230+
MethodParameter[] parameters = handlerMethod.getMethodParameters();
230231
String[] reflectionParametersNames = Arrays.stream(handlerMethod.getMethod().getParameters()).map(java.lang.reflect.Parameter::getName).toArray(String[]::new);
231232
if (pNames == null || Arrays.stream(pNames).anyMatch(Objects::isNull))
232233
pNames = reflectionParametersNames;
233-
DelegatingMethodParameter[] parameters = DelegatingMethodParameter.customize(pNames, handlerMethod.getMethodParameters(), parameterBuilder.getDelegatingMethodParameterCustomizer());
234+
parameters = DelegatingMethodParameter.customize(pNames, parameters, parameterBuilder.getDelegatingMethodParameterCustomizer());
234235
RequestBodyInfo requestBodyInfo = new RequestBodyInfo();
235236
List<Parameter> operationParameters = (operation.getParameters() != null) ? operation.getParameters() : new ArrayList<>();
236237
Map<String, io.swagger.v3.oas.annotations.Parameter> parametersDocMap = getApiParameters(handlerMethod.getMethod());
237238
Components components = openAPI.getComponents();
238239

239-
for (DelegatingMethodParameter methodParameter : parameters) {
240+
for (MethodParameter methodParameter : parameters) {
240241
// check if query param
241242
Parameter parameter = null;
242243
io.swagger.v3.oas.annotations.Parameter parameterDoc = AnnotatedElementUtils.findMergedAnnotation(
@@ -352,7 +353,7 @@ protected Parameter customiseParameter(Parameter parameter, ParameterInfo parame
352353
* @param parameter the parameter
353354
* @return the boolean
354355
*/
355-
public boolean isParamToIgnore(DelegatingMethodParameter parameter) {
356+
public boolean isParamToIgnore(MethodParameter parameter) {
356357
if (SpringDocAnnotationsUtils.isAnnotationToIgnore(parameter))
357358
return true;
358359
if (isRequiredAnnotation(parameter))

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

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,6 @@ public class DelegatingMethodParameter extends MethodParameter {
6868
*/
6969
private boolean isParameterObject;
7070

71-
/**
72-
* For simple parameters, it is null
73-
* For nested parameter is a controller method parameter
74-
*/
75-
private MethodParameter controllerMethodParameter;
76-
7771
/**
7872
* Instantiates a new Delegating method parameter.
7973
*
@@ -98,24 +92,23 @@ public class DelegatingMethodParameter extends MethodParameter {
9892
* @param optionalDelegatingMethodParameterCustomizer the optional delegating method parameter customizer
9993
* @return the method parameter [ ]
10094
*/
101-
public static DelegatingMethodParameter[] customize(String[] pNames, MethodParameter[] parameters, Optional<DelegatingMethodParameterCustomizer> optionalDelegatingMethodParameterCustomizer) {
102-
List<DelegatingMethodParameter> explodedParameters = new ArrayList<>();
95+
public static MethodParameter[] customize(String[] pNames, MethodParameter[] parameters, Optional<DelegatingMethodParameterCustomizer> optionalDelegatingMethodParameterCustomizer) {
96+
List<MethodParameter> explodedParameters = new ArrayList<>();
10397
for (int i = 0; i < parameters.length; ++i) {
10498
MethodParameter p = parameters[i];
10599
Class<?> paramClass = AdditionalModelsConverter.getReplacement(p.getParameterType());
106100
if (p.hasParameterAnnotation(ParameterObject.class) || AnnotatedElementUtils.isAnnotated(paramClass, ParameterObject.class)) {
107-
MethodParameterPojoExtractor.extractFrom(paramClass).forEach(delegatingMethodParameter -> {
108-
optionalDelegatingMethodParameterCustomizer.ifPresent(customizer -> customizer.customize(p, delegatingMethodParameter));
109-
delegatingMethodParameter.setControllerMethodParameter(p);
110-
explodedParameters.add(delegatingMethodParameter);
101+
MethodParameterPojoExtractor.extractFrom(paramClass).forEach(methodParameter -> {
102+
optionalDelegatingMethodParameterCustomizer.ifPresent(customizer -> customizer.customize(p, methodParameter));
103+
explodedParameters.add(methodParameter);
111104
});
112105
}
113106
else {
114107
String name = pNames != null ? pNames[i] : p.getParameterName();
115108
explodedParameters.add(new DelegatingMethodParameter(p, name, null, false));
116109
}
117110
}
118-
return explodedParameters.toArray(new DelegatingMethodParameter[0]);
111+
return explodedParameters.toArray(new MethodParameter[0]);
119112
}
120113

121114
@Override
@@ -220,14 +213,4 @@ public int hashCode() {
220213
public boolean isParameterObject() {
221214
return isParameterObject;
222215
}
223-
224-
public void setControllerMethodParameter(MethodParameter controllerMethodParameter) {
225-
this.controllerMethodParameter = controllerMethodParameter;
226-
}
227-
228-
public MethodParameter getControllerMethodParameter() {
229-
return controllerMethodParameter == null ? delegate : controllerMethodParameter;
230-
}
231-
232-
233-
}
216+
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ private MethodParameterPojoExtractor() {
9494
* @param clazz the clazz
9595
* @return the stream
9696
*/
97-
static Stream<DelegatingMethodParameter> extractFrom(Class<?> clazz) {
97+
static Stream<MethodParameter> extractFrom(Class<?> clazz) {
9898
return extractFrom(clazz, "");
9999
}
100100

@@ -105,7 +105,7 @@ static Stream<DelegatingMethodParameter> extractFrom(Class<?> clazz) {
105105
* @param fieldNamePrefix the field name prefix
106106
* @return the stream
107107
*/
108-
private static Stream<DelegatingMethodParameter> extractFrom(Class<?> clazz, String fieldNamePrefix) {
108+
private static Stream<MethodParameter> extractFrom(Class<?> clazz, String fieldNamePrefix) {
109109
return allFieldsOf(clazz).stream()
110110
.flatMap(f -> fromGetterOfField(clazz, f, fieldNamePrefix))
111111
.filter(Objects::nonNull);
@@ -119,7 +119,7 @@ private static Stream<DelegatingMethodParameter> extractFrom(Class<?> clazz, Str
119119
* @param fieldNamePrefix the field name prefix
120120
* @return the stream
121121
*/
122-
private static Stream<DelegatingMethodParameter> fromGetterOfField(Class<?> paramClass, Field field, String fieldNamePrefix) {
122+
private static Stream<MethodParameter> fromGetterOfField(Class<?> paramClass, Field field, String fieldNamePrefix) {
123123
if (isSimpleType(field.getType()))
124124
return fromSimpleClass(paramClass, field, fieldNamePrefix);
125125
else
@@ -134,7 +134,7 @@ private static Stream<DelegatingMethodParameter> fromGetterOfField(Class<?> para
134134
* @param fieldNamePrefix the field name prefix
135135
* @return the stream
136136
*/
137-
private static Stream<DelegatingMethodParameter> fromSimpleClass(Class<?> paramClass, Field field, String fieldNamePrefix) {
137+
private static Stream<MethodParameter> fromSimpleClass(Class<?> paramClass, Field field, String fieldNamePrefix) {
138138
Annotation[] fieldAnnotations = field.getDeclaredAnnotations();
139139
try {
140140
Nullable nullableField = NULLABLE_ANNOTATION;

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,10 @@ public static void mergeSchema(Content existingContent, Schema<?> schemaN, Strin
231231
* @return the boolean
232232
*/
233233
@SuppressWarnings("unchecked")
234-
public static boolean isAnnotationToIgnore(DelegatingMethodParameter parameter) {
235-
return ANNOTATIONS_TO_IGNORE.stream().anyMatch(
236-
annotation -> AnnotationUtils.findAnnotation(parameter.getControllerMethodParameter().getParameter(), annotation) != null
234+
public static boolean isAnnotationToIgnore(MethodParameter parameter) {
235+
return parameter.getParameterIndex() != -1
236+
&& ANNOTATIONS_TO_IGNORE.stream().anyMatch(
237+
annotation -> AnnotationUtils.findAnnotation(parameter.getParameter(), annotation) != null
237238
|| AnnotationUtils.findAnnotation(parameter.getParameterType(), annotation) != null);
238239
}
239240

@@ -246,7 +247,7 @@ public static boolean isAnnotationToIgnore(DelegatingMethodParameter parameter)
246247
public static boolean isAnnotationToIgnore(Type type) {
247248
return ANNOTATIONS_TO_IGNORE.stream().anyMatch(
248249
annotation -> (type instanceof Class
249-
&& AnnotationUtils.findAnnotation((Class<?>) type, annotation) != null));
250+
&& AnnotationUtils.findAnnotation((Class<?>) type, annotation) != null));
250251
}
251252

252253
/**

springdoc-openapi-data-rest/src/main/java/org/springdoc/data/rest/core/DataRestRequestBuilder.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,11 @@ public void buildParameters(Class<?> domainType, OpenAPI openAPI, HandlerMethod
133133
* @param methodAttributes the method attributes
134134
* @param operation the operation
135135
* @param pNames the p names
136-
* @param methodParameters the parameters
136+
* @param parameters the parameters
137137
*/
138-
public void buildCommonParameters(Class<?> domainType, OpenAPI openAPI, RequestMethod requestMethod, MethodAttributes methodAttributes, Operation operation, String[] pNames, MethodParameter[] methodParameters) {
139-
DelegatingMethodParameter[] parameters = DelegatingMethodParameter.customize(pNames, methodParameters, parameterBuilder.getDelegatingMethodParameterCustomizer());
140-
for (DelegatingMethodParameter methodParameter : parameters) {
138+
public void buildCommonParameters(Class<?> domainType, OpenAPI openAPI, RequestMethod requestMethod, MethodAttributes methodAttributes, Operation operation, String[] pNames, MethodParameter[] parameters) {
139+
parameters = DelegatingMethodParameter.customize(pNames, parameters, parameterBuilder.getDelegatingMethodParameterCustomizer());
140+
for (MethodParameter methodParameter : parameters) {
141141
final String pName = methodParameter.getParameterName();
142142
ParameterInfo parameterInfo = new ParameterInfo(pName, methodParameter);
143143
if (isParamToIgnore(methodParameter)) {
@@ -171,7 +171,7 @@ else if (methodParameter.getParameterAnnotation(BackendId.class) != null) {
171171
* @param methodParameter the method parameter
172172
* @return the boolean
173173
*/
174-
private boolean isParamToIgnore(DelegatingMethodParameter methodParameter) {
174+
private boolean isParamToIgnore(MethodParameter methodParameter) {
175175
return !requestBuilder.isParamToIgnore(methodParameter)
176176
&& !isHeaderToIgnore(methodParameter)
177177
&& !"property".equals(methodParameter.getParameterName());

0 commit comments

Comments
 (0)