Skip to content

Commit a1f40e2

Browse files
author
bnasslahsen
committed
Improve support of Generics inheritance on complex return types.Fixes #537.
1 parent 2514d9b commit a1f40e2

File tree

7 files changed

+145
-1
lines changed

7 files changed

+145
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@
2121
import java.lang.reflect.ParameterizedType;
2222
import java.lang.reflect.Type;
2323

24+
import org.springframework.core.GenericTypeResolver;
2425
import org.springframework.core.MethodParameter;
2526

2627
public interface ReturnTypeParser {
2728

2829
default Type getReturnType(MethodParameter methodParameter) {
2930
if (methodParameter.getGenericParameterType() instanceof ParameterizedType)
30-
return methodParameter.getGenericParameterType();
31+
return GenericTypeResolver.resolveType(methodParameter.getGenericParameterType(),methodParameter.getContainingClass());
3132
return methodParameter.getParameterType();
3233
}
3334
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package test.org.springdoc.api.app105;
2+
3+
import java.util.List;
4+
5+
import io.swagger.v3.oas.annotations.Operation;
6+
import io.swagger.v3.oas.annotations.Parameter;
7+
8+
import org.springframework.stereotype.Controller;
9+
import org.springframework.web.bind.annotation.GetMapping;
10+
import org.springframework.web.bind.annotation.PathVariable;
11+
import org.springframework.web.bind.annotation.ResponseBody;
12+
13+
@Controller
14+
@SuppressWarnings("rawtypes")
15+
public abstract class CrudController<T extends HavingPK> {
16+
17+
@GetMapping(path = "{id}")
18+
@ResponseBody
19+
@Operation(description = "Get single object")
20+
public T get( //
21+
@Parameter(description = "The id to get.", required = true) @PathVariable("id") int id) {
22+
return null;
23+
}
24+
25+
@GetMapping(path = "")
26+
@ResponseBody
27+
@Operation(description = "Receive a list of objects")
28+
public List<T> list() {
29+
return null;
30+
}
31+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package test.org.springdoc.api.app105;
2+
3+
public class Design extends HavingPK {
4+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package test.org.springdoc.api.app105;
2+
3+
import io.swagger.v3.oas.annotations.tags.Tag;
4+
5+
import org.springframework.stereotype.Controller;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
8+
@Tag(name = "design")
9+
@Controller
10+
@RequestMapping("/design")
11+
public class DesignController extends CrudController<Design> {
12+
13+
14+
15+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package test.org.springdoc.api.app105;
2+
3+
public class HavingPK {
4+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package test.org.springdoc.api.app105;
2+
3+
import test.org.springdoc.api.AbstractSpringDocTest;
4+
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
7+
public class SpringDocApp105Test extends AbstractSpringDocTest {
8+
@SpringBootApplication
9+
static class SpringDocTestApp {}
10+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
{
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "OpenAPI definition",
5+
"version": "v0"
6+
},
7+
"servers": [
8+
{
9+
"url": "http://localhost",
10+
"description": "Generated server url"
11+
}
12+
],
13+
"paths": {
14+
"/design/{id}": {
15+
"get": {
16+
"tags": [
17+
"design"
18+
],
19+
"description": "Get single object",
20+
"operationId": "get",
21+
"parameters": [
22+
{
23+
"name": "id",
24+
"in": "path",
25+
"description": "The id to get.",
26+
"required": true,
27+
"schema": {
28+
"type": "integer",
29+
"format": "int32"
30+
}
31+
}
32+
],
33+
"responses": {
34+
"200": {
35+
"description": "default response",
36+
"content": {
37+
"*/*": {
38+
"schema": {
39+
"$ref": "#/components/schemas/Design"
40+
}
41+
}
42+
}
43+
}
44+
}
45+
}
46+
},
47+
"/design": {
48+
"get": {
49+
"tags": [
50+
"design"
51+
],
52+
"description": "Receive a list of objects",
53+
"operationId": "list",
54+
"responses": {
55+
"200": {
56+
"description": "default response",
57+
"content": {
58+
"*/*": {
59+
"schema": {
60+
"type": "array",
61+
"items": {
62+
"$ref": "#/components/schemas/Design"
63+
}
64+
}
65+
}
66+
}
67+
}
68+
}
69+
}
70+
}
71+
},
72+
"components": {
73+
"schemas": {
74+
"Design": {
75+
"type": "object"
76+
}
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)