Skip to content

Commit 57aaeb4

Browse files
committed
Changes report: Set containingClass at MethodParameter. fixes #1491
1 parent 70adc0d commit 57aaeb4

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
* * * * See the License for the specific language governing permissions and
1717
* * * * limitations under the License.
1818
* * *
19+
* * * https://www.apache.org/licenses/LICENSE-2.0
20+
* * *
21+
* * * Unless required by applicable law or agreed to in writing, software
22+
* * * distributed under the License is distributed on an "AS IS" BASIS,
23+
* * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24+
* * * See the License for the specific language governing permissions and
25+
* * * limitations under the License.
1926
* *
2027
*
2128
*/
@@ -25,6 +32,7 @@
2532
import java.lang.reflect.AnnotatedElement;
2633
import java.lang.reflect.Constructor;
2734
import java.lang.reflect.Executable;
35+
import java.lang.reflect.Field;
2836
import java.lang.reflect.Member;
2937
import java.lang.reflect.Method;
3038
import java.lang.reflect.Type;
@@ -35,6 +43,9 @@
3543
import java.util.Optional;
3644

3745
import org.apache.commons.lang3.ArrayUtils;
46+
import org.apache.commons.lang3.reflect.FieldUtils;
47+
import org.slf4j.Logger;
48+
import org.slf4j.LoggerFactory;
3849
import org.springdoc.core.annotations.ParameterObject;
3950
import org.springdoc.core.converters.AdditionalModelsConverter;
4051
import org.springdoc.core.customizers.DelegatingMethodParameterCustomizer;
@@ -43,6 +54,7 @@
4354
import org.springframework.core.ParameterNameDiscoverer;
4455
import org.springframework.core.annotation.AnnotatedElementUtils;
4556
import org.springframework.lang.NonNull;
57+
import org.springframework.lang.Nullable;
4658

4759
/**
4860
* The type Delegating method parameter.
@@ -75,6 +87,11 @@ public class DelegatingMethodParameter extends MethodParameter {
7587
*/
7688
private boolean isNotRequired;
7789

90+
/**
91+
* The constant LOGGER.
92+
*/
93+
private static final Logger LOGGER = LoggerFactory.getLogger(DelegatingMethodParameter.class);
94+
7895
/**
7996
* Instantiates a new Delegating method parameter.
8097
*
@@ -241,4 +258,27 @@ public int hashCode() {
241258
public boolean isParameterObject() {
242259
return isParameterObject;
243260
}
261+
262+
/**
263+
* Return a variant of this {@code MethodParameter} which refers to the
264+
* given containing class.
265+
* @param containingClass a specific containing class (potentially a
266+
* subclass of the declaring class, e.g. substituting a type variable)
267+
* A copy of spring withContainingClass, to keep compatibility with older spring versions
268+
* @see #getParameterType()
269+
*/
270+
public static MethodParameter changeContainingClass(MethodParameter methodParameter, @Nullable Class<?> containingClass) {
271+
MethodParameter result = methodParameter.clone();
272+
try {
273+
Field containingClassField = FieldUtils.getDeclaredField(result.getClass(), "containingClass", true);
274+
containingClassField.set(result, containingClass);
275+
Field parameterTypeField = FieldUtils.getDeclaredField(result.getClass(), "parameterType", true);
276+
parameterTypeField.set(result, null);
277+
}
278+
catch (IllegalAccessException e) {
279+
LOGGER.warn(e.getMessage());
280+
}
281+
return result;
282+
}
283+
244284
}

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
*
33
* *
4+
* * * Copyright 2019-2020 the original author or authors.
45
* * *
56
* * * * Copyright 2019-2022 the original author or authors.
67
* * * *
@@ -16,6 +17,13 @@
1617
* * * * See the License for the specific language governing permissions and
1718
* * * * limitations under the License.
1819
* * *
20+
* * * https://www.apache.org/licenses/LICENSE-2.0
21+
* * *
22+
* * * Unless required by applicable law or agreed to in writing, software
23+
* * * distributed under the License is distributed on an "AS IS" BASIS,
24+
* * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25+
* * * See the License for the specific language governing permissions and
26+
* * * limitations under the License.
1927
* *
2028
*
2129
*/
@@ -177,7 +185,8 @@ private static Stream<MethodParameter> fromSimpleClass(Class<?> paramClass, Fiel
177185
.filter(d -> d.getName().equals(field.getName()))
178186
.map(PropertyDescriptor::getReadMethod)
179187
.filter(Objects::nonNull)
180-
.map(method -> new MethodParameter(method, -1).withContainingClass(paramClass))
188+
.map(method -> new MethodParameter(method, -1))
189+
.map(methodParameter -> DelegatingMethodParameter.changeContainingClass(methodParameter, paramClass))
181190
.map(param -> new DelegatingMethodParameter(param, fieldNamePrefix + field.getName(), finalFieldAnnotations, true, isNotRequired));
182191
}
183192
catch (IntrospectionException e) {

0 commit comments

Comments
 (0)