Skip to content

Commit 14bb1bf

Browse files
committed
Polymorphism - Support JsonTypeInfo.Id.CLASS. fixes #1799
1 parent 2478516 commit 14bb1bf

File tree

7 files changed

+146
-7
lines changed

7 files changed

+146
-7
lines changed

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@ public Schema resolve(AnnotatedType type, ModelConverterContext context, Iterato
6565
if (javaType != null) {
6666
if (chain.hasNext()) {
6767
Schema<?> resolvedSchema = chain.next().resolve(type, context, chain);
68-
if (resolvedSchema instanceof ObjectSchema && resolvedSchema.getProperties() != null
69-
&& resolvedSchema.getProperties().containsKey(javaType.getRawClass().getSimpleName()))
70-
resolvedSchema = resolvedSchema.getProperties().get(javaType.getRawClass().getSimpleName());
68+
resolvedSchema = getResolvedSchema(javaType, resolvedSchema);
7169
if (resolvedSchema == null || resolvedSchema.get$ref() == null)
7270
return resolvedSchema;
7371
return composePolymorphicSchema(type, resolvedSchema, context.getDefinedModels().values());
@@ -76,6 +74,16 @@ public Schema resolve(AnnotatedType type, ModelConverterContext context, Iterato
7674
return null;
7775
}
7876

77+
private static Schema<?> getResolvedSchema(JavaType javaType, Schema<?> resolvedSchema) {
78+
if (resolvedSchema instanceof ObjectSchema && resolvedSchema.getProperties() != null){
79+
if (resolvedSchema.getProperties().containsKey(javaType.getRawClass().getName()))
80+
resolvedSchema = resolvedSchema.getProperties().get(javaType.getRawClass().getName());
81+
else if (resolvedSchema.getProperties().containsKey(javaType.getRawClass().getSimpleName()))
82+
resolvedSchema = resolvedSchema.getProperties().get(javaType.getRawClass().getSimpleName());
83+
}
84+
return resolvedSchema;
85+
}
86+
7987
/**
8088
* Compose polymorphic schema schema.
8189
*

springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/v30/app193/BasicController.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,16 @@
1212
@RequestMapping(path = "/")
1313
public class BasicController {
1414

15-
@GetMapping("/test")
16-
@Operation(summary = "get", description = "Provides an animal.")
17-
public Animal get() {
15+
@GetMapping("/test")
16+
@ResponseStatus(HttpStatus.OK)
17+
@Operation(summary = "get", description = "Provides a list of books.")
18+
public Knowledge get() {
19+
return new Books(new Book("Introduction to algorithms"));
20+
}
21+
22+
@GetMapping("/test1")
23+
@Operation(summary = "get1", description = "Provides an animal.")
24+
public Animal get1() {
1825

1926
return new Dog("Foo", 12);
2027
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package test.org.springdoc.api.v30.app193;
2+
3+
4+
import io.swagger.v3.oas.annotations.media.Schema;
5+
6+
@Schema(description = "Represents a Book.")
7+
public class Book {
8+
9+
private String title;
10+
11+
public Book(String title) {
12+
this.title = title;
13+
}
14+
15+
public String getTitle() {
16+
return title;
17+
}
18+
19+
public void setTitle(String title) {
20+
this.title = title;
21+
}
22+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package test.org.springdoc.api.v30.app193;
2+
3+
4+
import java.util.ArrayList;
5+
import java.util.Arrays;
6+
7+
import io.swagger.v3.oas.annotations.media.Schema;
8+
9+
@Schema(description = "Represents a list of Books.")
10+
public class Books extends ArrayList<Book> implements Knowledge {
11+
12+
public Books() {
13+
super();
14+
}
15+
16+
public Books(Book... books) {
17+
this();
18+
19+
if (books != null) {
20+
Arrays.stream(books).forEach(this::add);
21+
}
22+
}
23+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package test.org.springdoc.api.v30.app193;
2+
3+
import com.fasterxml.jackson.annotation.JsonSubTypes;
4+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
5+
import io.swagger.v3.oas.annotations.media.Schema;
6+
7+
/**
8+
* Interface of the Knowledge.
9+
*/
10+
@Schema(description = "Represents the knowledge.")
11+
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.WRAPPER_OBJECT)
12+
@JsonSubTypes({@JsonSubTypes.Type(value = Books.class)})
13+
public interface Knowledge {
14+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package test.org.springdoc.api.v30.app193;
2+
3+
public class Response {
4+
5+
private Knowledge books;
6+
7+
public Response(Knowledge books) {
8+
this.books = books;
9+
}
10+
11+
public Knowledge getBooks() {
12+
return books;
13+
}
14+
15+
public void setBooks(Knowledge books) {
16+
this.books = books;
17+
}
18+
}

springdoc-openapi-webmvc-core/src/test/resources/results/3.0.1/app193.json

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,34 @@
1717
"basic-controller"
1818
],
1919
"summary": "get",
20-
"description": "Provides an animal.",
20+
"description": "Provides a list of books.",
2121
"operationId": "get",
22+
"responses": {
23+
"200": {
24+
"description": "OK",
25+
"content": {
26+
"*/*": {
27+
"schema": {
28+
"oneOf": [
29+
{
30+
"$ref": "#/components/schemas/Books"
31+
}
32+
]
33+
}
34+
}
35+
}
36+
}
37+
}
38+
}
39+
},
40+
"/test1": {
41+
"get": {
42+
"tags": [
43+
"basic-controller"
44+
],
45+
"summary": "get1",
46+
"description": "Provides an animal.",
47+
"operationId": "get1",
2248
"responses": {
2349
"200": {
2450
"description": "OK",
@@ -43,6 +69,27 @@
4369
},
4470
"components": {
4571
"schemas": {
72+
"Books": {
73+
"type": "array",
74+
"description": "Represents a list of Books.",
75+
"allOf": [
76+
{
77+
"$ref": "#/components/schemas/Knowledge"
78+
},
79+
{
80+
"type": "object",
81+
"properties": {
82+
"empty": {
83+
"type": "boolean"
84+
}
85+
}
86+
}
87+
]
88+
},
89+
"Knowledge": {
90+
"type": "object",
91+
"description": "Represents the knowledge."
92+
},
4693
"Animal": {
4794
"type": "object",
4895
"description": "Represents an Animal class."

0 commit comments

Comments
 (0)