Skip to content

Commit

Permalink
Set containingClass at MethodParameter. fixes #1491
Browse files Browse the repository at this point in the history
  • Loading branch information
bnasslahsen committed Feb 5, 2022
1 parent 540c939 commit 3061086
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Constructor;
import java.lang.reflect.Executable;
import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
Expand All @@ -33,6 +34,9 @@
import java.util.Optional;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springdoc.api.annotations.ParameterObject;
import org.springdoc.core.converters.AdditionalModelsConverter;
import org.springdoc.core.customizers.DelegatingMethodParameterCustomizer;
Expand All @@ -41,6 +45,7 @@
import org.springframework.core.ParameterNameDiscoverer;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;

/**
* The type Delegating method parameter.
Expand Down Expand Up @@ -73,6 +78,11 @@ public class DelegatingMethodParameter extends MethodParameter {
*/
private boolean isNotRequired;

/**
* The constant LOGGER.
*/
private static final Logger LOGGER = LoggerFactory.getLogger(DelegatingMethodParameter.class);

/**
* Instantiates a new Delegating method parameter.
*
Expand Down Expand Up @@ -239,4 +249,27 @@ public int hashCode() {
public boolean isParameterObject() {
return isParameterObject;
}

/**
* Return a variant of this {@code MethodParameter} which refers to the
* given containing class.
* @param containingClass a specific containing class (potentially a
* subclass of the declaring class, e.g. substituting a type variable)
* A copy of spring withContainingClass, to keep compatibility with older spring versions
* @see #getParameterType()
*/
public static MethodParameter changeContainingClass(MethodParameter methodParameter, @Nullable Class<?> containingClass) {
MethodParameter result = methodParameter.clone();
try {
Field containingClassField = FieldUtils.getDeclaredField(result.getClass(), "containingClass", true);
containingClassField.set(result, containingClass);
Field parameterTypeField = FieldUtils.getDeclaredField(result.getClass(), "parameterType", true);
parameterTypeField.set(result, null);
}
catch (IllegalAccessException e) {
LOGGER.warn(e.getMessage());
}
return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ private static Stream<MethodParameter> fromSimpleClass(Class<?> paramClass, Fiel
.filter(d -> d.getName().equals(field.getName()))
.map(PropertyDescriptor::getReadMethod)
.filter(Objects::nonNull)
.map(method -> new MethodParameter(method, -1).withContainingClass(paramClass))
.map(method -> new MethodParameter(method, -1))
.map(methodParameter -> DelegatingMethodParameter.changeContainingClass(methodParameter, paramClass))
.map(param -> new DelegatingMethodParameter(param, fieldNamePrefix + field.getName(), finalFieldAnnotations, true, isNotRequired));
}
catch (IntrospectionException e) {
Expand Down

0 comments on commit 3061086

Please sign in to comment.