Skip to content

Commit 3061086

Browse files
committed
Set containingClass at MethodParameter. fixes #1491
1 parent 540c939 commit 3061086

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.lang.reflect.AnnotatedElement;
2424
import java.lang.reflect.Constructor;
2525
import java.lang.reflect.Executable;
26+
import java.lang.reflect.Field;
2627
import java.lang.reflect.Member;
2728
import java.lang.reflect.Method;
2829
import java.lang.reflect.Type;
@@ -33,6 +34,9 @@
3334
import java.util.Optional;
3435

3536
import org.apache.commons.lang3.ArrayUtils;
37+
import org.apache.commons.lang3.reflect.FieldUtils;
38+
import org.slf4j.Logger;
39+
import org.slf4j.LoggerFactory;
3640
import org.springdoc.api.annotations.ParameterObject;
3741
import org.springdoc.core.converters.AdditionalModelsConverter;
3842
import org.springdoc.core.customizers.DelegatingMethodParameterCustomizer;
@@ -41,6 +45,7 @@
4145
import org.springframework.core.ParameterNameDiscoverer;
4246
import org.springframework.core.annotation.AnnotatedElementUtils;
4347
import org.springframework.lang.NonNull;
48+
import org.springframework.lang.Nullable;
4449

4550
/**
4651
* The type Delegating method parameter.
@@ -73,6 +78,11 @@ public class DelegatingMethodParameter extends MethodParameter {
7378
*/
7479
private boolean isNotRequired;
7580

81+
/**
82+
* The constant LOGGER.
83+
*/
84+
private static final Logger LOGGER = LoggerFactory.getLogger(DelegatingMethodParameter.class);
85+
7686
/**
7787
* Instantiates a new Delegating method parameter.
7888
*
@@ -239,4 +249,27 @@ public int hashCode() {
239249
public boolean isParameterObject() {
240250
return isParameterObject;
241251
}
252+
253+
/**
254+
* Return a variant of this {@code MethodParameter} which refers to the
255+
* given containing class.
256+
* @param containingClass a specific containing class (potentially a
257+
* subclass of the declaring class, e.g. substituting a type variable)
258+
* A copy of spring withContainingClass, to keep compatibility with older spring versions
259+
* @see #getParameterType()
260+
*/
261+
public static MethodParameter changeContainingClass(MethodParameter methodParameter, @Nullable Class<?> containingClass) {
262+
MethodParameter result = methodParameter.clone();
263+
try {
264+
Field containingClassField = FieldUtils.getDeclaredField(result.getClass(), "containingClass", true);
265+
containingClassField.set(result, containingClass);
266+
Field parameterTypeField = FieldUtils.getDeclaredField(result.getClass(), "parameterType", true);
267+
parameterTypeField.set(result, null);
268+
}
269+
catch (IllegalAccessException e) {
270+
LOGGER.warn(e.getMessage());
271+
}
272+
return result;
273+
}
274+
242275
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ private static Stream<MethodParameter> fromSimpleClass(Class<?> paramClass, Fiel
174174
.filter(d -> d.getName().equals(field.getName()))
175175
.map(PropertyDescriptor::getReadMethod)
176176
.filter(Objects::nonNull)
177-
.map(method -> new MethodParameter(method, -1).withContainingClass(paramClass))
177+
.map(method -> new MethodParameter(method, -1))
178+
.map(methodParameter -> DelegatingMethodParameter.changeContainingClass(methodParameter, paramClass))
178179
.map(param -> new DelegatingMethodParameter(param, fieldNamePrefix + field.getName(), finalFieldAnnotations, true, isNotRequired));
179180
}
180181
catch (IntrospectionException e) {

0 commit comments

Comments
 (0)