Skip to content

Commit 8fd9f35

Browse files
author
bnasslahsen
committed
PR Review. fixes #505
1 parent e8b1b1c commit 8fd9f35

File tree

3 files changed

+44
-47
lines changed

3 files changed

+44
-47
lines changed

.editorconfig

Lines changed: 0 additions & 11 deletions
This file was deleted.

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

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@
5353
import io.swagger.v3.oas.models.parameters.Parameter;
5454
import io.swagger.v3.oas.models.parameters.RequestBody;
5555
import org.apache.commons.lang3.StringUtils;
56-
import org.springdoc.api.annotations.ParameterObject;
57-
import org.springdoc.core.converters.AdditionalModelsConverter;
5856
import org.springdoc.core.customizers.OperationCustomizer;
5957
import org.springdoc.core.customizers.ParameterCustomizer;
6058

@@ -162,24 +160,10 @@ public Operation build(HandlerMethod handlerMethod, RequestMethod requestMethod,
162160
// requests
163161
String[] pNames = this.localSpringDocParameterNameDiscoverer.getParameterNames(handlerMethod.getMethod());
164162
MethodParameter[] parameters = handlerMethod.getMethodParameters();
165-
166-
List<MethodParameter> explodedParameters = new ArrayList<>();
167-
for (int i = 0; i < parameters.length; ++i) {
168-
MethodParameter p = parameters[i];
169-
if (p.hasParameterAnnotation(ParameterObject.class)) {
170-
Class<?> paramClass = AdditionalModelsConverter.getReplacement(p.getParameterType());
171-
Stream.of(paramClass.getDeclaredFields())
172-
.map(f -> DelegatingMethodParameter.fromGetterOfField(paramClass, f))
173-
.filter(Objects::nonNull)
174-
.forEach(explodedParameters::add);
175-
}
176-
else {
177-
String name = pNames != null ? pNames[i] : p.getParameterName();
178-
explodedParameters.add(new DelegatingMethodParameter(p, name, null));
179-
}
180-
}
181-
parameters = explodedParameters.toArray(new MethodParameter[0]);
182-
163+
String[] reflectionParametersNames = Arrays.stream(parameters).map(MethodParameter::getParameterName).toArray(String[]::new);
164+
if (pNames == null)
165+
pNames = reflectionParametersNames;
166+
parameters = DelegatingMethodParameter.customize(pNames, parameters);
183167
RequestBodyInfo requestBodyInfo = new RequestBodyInfo();
184168
List<Parameter> operationParameters = (operation.getParameters() != null) ? operation.getParameters() : new ArrayList<>();
185169
Map<String, io.swagger.v3.oas.annotations.Parameter> parametersDocMap = getApiParameters(handlerMethod.getMethod());
@@ -188,9 +172,10 @@ public Operation build(HandlerMethod handlerMethod, RequestMethod requestMethod,
188172
for (MethodParameter methodParameter : parameters) {
189173
// check if query param
190174
Parameter parameter = null;
175+
final String pName = methodParameter.getParameterName();
191176
io.swagger.v3.oas.annotations.Parameter parameterDoc = methodParameter.getParameterAnnotation(io.swagger.v3.oas.annotations.Parameter.class);
192177
if (parameterDoc == null)
193-
parameterDoc = parametersDocMap.get(methodParameter.getParameterName());
178+
parameterDoc = parametersDocMap.get(pName);
194179
// use documentation as reference
195180
if (parameterDoc != null) {
196181
if (parameterDoc.hidden())
@@ -200,7 +185,7 @@ public Operation build(HandlerMethod handlerMethod, RequestMethod requestMethod,
200185
}
201186

202187
if (!isParamToIgnore(methodParameter)) {
203-
ParameterInfo parameterInfo = new ParameterInfo(methodParameter.getParameterName(), methodParameter, parameter);
188+
ParameterInfo parameterInfo = new ParameterInfo(pName, methodParameter, parameter);
204189
parameter = buildParams(parameterInfo, components, requestMethod,
205190
methodAttributes.getJsonViewAnnotation());
206191
// Merge with the operation parameters

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

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@
1111
import java.lang.reflect.Member;
1212
import java.lang.reflect.Method;
1313
import java.lang.reflect.Type;
14+
import java.util.ArrayList;
15+
import java.util.List;
1416
import java.util.Objects;
1517
import java.util.stream.Stream;
1618

1719
import org.apache.commons.lang3.ArrayUtils;
20+
import org.springdoc.api.annotations.ParameterObject;
21+
import org.springdoc.core.converters.AdditionalModelsConverter;
1822

1923
import org.springframework.core.MethodParameter;
2024
import org.springframework.core.ParameterNameDiscoverer;
@@ -38,21 +42,23 @@ class DelegatingMethodParameter extends MethodParameter {
3842
this.parameterName = parameterName;
3943
}
4044

41-
@Nullable
42-
static MethodParameter fromGetterOfField(Class<?> paramClass, Field field) {
43-
try {
44-
return Stream.of(Introspector.getBeanInfo(paramClass).getPropertyDescriptors())
45-
.filter(d -> d.getName().equals(field.getName()))
46-
.map(PropertyDescriptor::getReadMethod)
47-
.filter(Objects::nonNull)
48-
.findFirst()
49-
.map(method -> new MethodParameter(method, -1))
50-
.map(param -> new DelegatingMethodParameter(param, field.getName(), field.getDeclaredAnnotations()))
51-
.orElse(null);
52-
}
53-
catch (IntrospectionException e) {
54-
return null;
45+
public static MethodParameter[] customize(String[] pNames, MethodParameter[] parameters) {
46+
List<MethodParameter> explodedParameters = new ArrayList<>();
47+
for (int i = 0; i < parameters.length; ++i) {
48+
MethodParameter p = parameters[i];
49+
if (p.hasParameterAnnotation(ParameterObject.class)) {
50+
Class<?> paramClass = AdditionalModelsConverter.getReplacement(p.getParameterType());
51+
Stream.of(paramClass.getDeclaredFields())
52+
.map(f -> fromGetterOfField(paramClass, f))
53+
.filter(Objects::nonNull)
54+
.forEach(explodedParameters::add);
55+
}
56+
else {
57+
String name = pNames != null ? pNames[i] : p.getParameterName();
58+
explodedParameters.add(new DelegatingMethodParameter(p, name, null));
59+
}
5560
}
61+
return explodedParameters.toArray(new MethodParameter[0]);
5662
}
5763

5864
@Override
@@ -130,4 +136,21 @@ public Type getNestedGenericParameterType() {
130136
public void initParameterNameDiscovery(ParameterNameDiscoverer parameterNameDiscoverer) {
131137
delegate.initParameterNameDiscovery(parameterNameDiscoverer);
132138
}
139+
140+
@Nullable
141+
static MethodParameter fromGetterOfField(Class<?> paramClass, Field field) {
142+
try {
143+
return Stream.of(Introspector.getBeanInfo(paramClass).getPropertyDescriptors())
144+
.filter(d -> d.getName().equals(field.getName()))
145+
.map(PropertyDescriptor::getReadMethod)
146+
.filter(Objects::nonNull)
147+
.findFirst()
148+
.map(method -> new MethodParameter(method, -1))
149+
.map(param -> new DelegatingMethodParameter(param, field.getName(), field.getDeclaredAnnotations()))
150+
.orElse(null);
151+
}
152+
catch (IntrospectionException e) {
153+
return null;
154+
}
155+
}
133156
}

0 commit comments

Comments
 (0)