Skip to content

Commit 534b7c5

Browse files
committed
Bad schema return type when created a generic wrapper class for response entity. Fixes #2733
1 parent 65f5b3f commit 534b7c5

File tree

8 files changed

+49
-78
lines changed

8 files changed

+49
-78
lines changed

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/converters/ConverterUtils.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,6 @@ public static boolean isResponseTypeWrapper(Class<?> rawClass) {
102102
return RESULT_WRAPPERS_TO_IGNORE.stream().anyMatch(clazz -> clazz.isAssignableFrom(rawClass));
103103
}
104104

105-
/**
106-
* Is exact class
107-
* @param rawClass the raw class
108-
* true if the class is in the list of classes to ignore
109-
*/
110-
public static boolean isExactClass(Class<?> rawClass) {
111-
return RESULT_WRAPPERS_TO_IGNORE.stream().anyMatch(clazz -> clazz == rawClass);
112-
}
113-
114105
/**
115106
* Is response type to ignore boolean.
116107
*

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/converters/ResponseSupportConverter.java

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@
3535
import io.swagger.v3.oas.models.media.StringSchema;
3636
import org.springdoc.core.providers.ObjectMapperProvider;
3737

38-
import static org.springdoc.core.converters.ConverterUtils.*;
38+
import static org.springdoc.core.converters.ConverterUtils.isFluxTypeWrapper;
39+
import static org.springdoc.core.converters.ConverterUtils.isResponseTypeToIgnore;
40+
import static org.springdoc.core.converters.ConverterUtils.isResponseTypeWrapper;
3941

4042
/**
4143
* The type Response support converter.
@@ -63,7 +65,12 @@ public Schema resolve(AnnotatedType type, ModelConverterContext context, Iterato
6365
if (javaType != null) {
6466
Class<?> cls = javaType.getRawClass();
6567
if (isResponseTypeWrapper(cls) && !isFluxTypeWrapper(cls)) {
66-
JavaType innerType = resolveInnerType(javaType);
68+
JavaType innerType = javaType.getBindings().getBoundType(0);
69+
if(javaType.getSuperClass() !=null
70+
&& javaType.getSuperClass().hasGenericTypes()
71+
&& isResponseTypeWrapper(javaType.getSuperClass().getRawClass())
72+
&& !Object.class.equals(javaType.getSuperClass().getBindings().getBoundType(0).getRawClass()))
73+
innerType = javaType.getSuperClass().getBindings().getBoundType(0);
6774
if (innerType == null)
6875
return new StringSchema();
6976
return context.resolve(new AnnotatedType(innerType)
@@ -76,18 +83,4 @@ else if (isResponseTypeToIgnore(cls))
7683
}
7784
return (chain.hasNext()) ? chain.next().resolve(type, context, chain) : null;
7885
}
79-
80-
/**
81-
* Resolve inner type java type.
82-
*
83-
* @param javaType the java type
84-
* @return the java type
85-
*/
86-
private JavaType resolveInnerType(JavaType javaType) {
87-
if(!isExactClass(javaType.getRawClass())) {
88-
javaType = javaType.getSuperClass();
89-
}
90-
return javaType.getBindings().getBoundType(0);
91-
}
92-
9386
}
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616
*
1717
*/
1818

19-
package test.org.springdoc.api.app227;
19+
package test.org.springdoc.api.v30.app227;
20+
21+
22+
import test.org.springdoc.api.v30.app227.model.Item;
23+
import test.org.springdoc.api.v30.app227.wrapper.ResponseEntityWrapper;
2024

2125
import org.springframework.http.HttpStatus;
2226
import org.springframework.web.bind.annotation.GetMapping;
2327
import org.springframework.web.bind.annotation.RestController;
24-
import test.org.springdoc.api.app227.model.Item;
25-
import test.org.springdoc.api.app227.wrapper.ResponseEntityWrapper;
2628

2729
@RestController
2830
public class HelloController {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
*
3+
* * Copyright 2019-2024 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package test.org.springdoc.api.v30.app227;
20+
21+
import test.org.springdoc.api.v30.AbstractSpringDocV30Test;
22+
23+
import org.springframework.boot.autoconfigure.SpringBootApplication;
24+
25+
public class SpringDocApp227Test extends AbstractSpringDocV30Test {
26+
27+
28+
@SpringBootApplication
29+
static class SpringDocTestApp {}
30+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package test.org.springdoc.api.app227.model;
1+
package test.org.springdoc.api.v30.app227.model;
22

33
/**
44
* Base item
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
package test.org.springdoc.api.app227.wrapper;
1+
package test.org.springdoc.api.v30.app227.wrapper;
2+
3+
4+
import test.org.springdoc.api.v30.app227.model.Item;
25

36
import org.springframework.http.HttpStatusCode;
47
import org.springframework.http.ResponseEntity;
5-
import test.org.springdoc.api.app227.model.Item;
68

79
public class ResponseEntityWrapper<T> extends ResponseEntity<Item<T>> {
810
public ResponseEntityWrapper(Item<T> body, HttpStatusCode status) {

springdoc-openapi-tests/springdoc-openapi-actuator-webmvc-tests/src/test/java/test/org/springdoc/api/app227/SpringDocApp227Test.java

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

0 commit comments

Comments
 (0)