Skip to content

Commit c7c31fc

Browse files
committed
Merge branch 'fix/respect_composed_schemas' of https://github.com/slw546/springdoc-openapi into slw546-fix/respect_composed_schemas
2 parents 7c0703c + dd16cd0 commit c7c31fc

File tree

7 files changed

+210
-2
lines changed

7 files changed

+210
-2
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,16 @@ public static Schema extractSchema(Components components, Type returnType, JsonV
129129
componentSchemas.putAll(schemaMap);
130130
}
131131
else
132-
for (Map.Entry<String, Schema> entry : schemaMap.entrySet())
133-
if (!componentSchemas.containsKey(entry.getKey()))
132+
for (Map.Entry<String, Schema> entry : schemaMap.entrySet()) {
133+
if (!componentSchemas.containsKey(entry.getKey())) {
134134
componentSchemas.put(entry.getKey(), entry.getValue());
135+
// If we've seen this schema before but find later it should be polymorphic,
136+
// replace the existing schema with this richer version.
137+
} else if (entry.getValue() instanceof ComposedSchema &&
138+
!(componentSchemas.get(entry.getKey()) instanceof ComposedSchema)) {
139+
componentSchemas.put(entry.getKey(), entry.getValue());
140+
}
141+
}
135142
components.setSchemas(componentSchemas);
136143
}
137144
if (resolvedSchema.schema != null) {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package test.org.springdoc.api.app31;
2+
3+
import io.swagger.v3.oas.annotations.media.Schema;
4+
5+
@Schema
6+
public class Cat extends Pet {
7+
8+
private final boolean meows;
9+
10+
public Cat() {
11+
super();
12+
this.meows = false;
13+
}
14+
15+
public Cat(boolean meows, String name) {
16+
super(name);
17+
this.meows = meows;
18+
}
19+
20+
public boolean getMeows() {
21+
return meows;
22+
}
23+
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package test.org.springdoc.api.app31;
2+
3+
import io.swagger.v3.oas.annotations.media.Schema;
4+
5+
@Schema
6+
public class Dog extends Pet {
7+
8+
private final boolean barks;
9+
10+
public Dog() {
11+
super();
12+
this.barks = false;
13+
}
14+
15+
public Dog(boolean barks, String name) {
16+
super(name);
17+
this.barks = barks;
18+
}
19+
20+
public boolean getBarks() {
21+
return barks;
22+
}
23+
24+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package test.org.springdoc.api.app31;
2+
3+
import com.fasterxml.jackson.annotation.JsonSubTypes;
4+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
5+
6+
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
7+
@JsonSubTypes({
8+
@JsonSubTypes.Type(Dog.class),
9+
@JsonSubTypes.Type(Cat.class)
10+
})
11+
public class Pet {
12+
13+
public final String name;
14+
15+
public Pet() {
16+
this.name = null;
17+
}
18+
19+
public Pet(String name) {
20+
this.name = name;
21+
}
22+
23+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package test.org.springdoc.api.app31;
2+
3+
import org.springframework.web.bind.annotation.GetMapping;
4+
import org.springframework.web.bind.annotation.RestController;
5+
6+
@RestController
7+
public class PetController {
8+
9+
@GetMapping("/any")
10+
public Pet getAnyPet() {
11+
return new Cat(true, "cat");
12+
}
13+
14+
@GetMapping("/dog")
15+
public Dog getDog() {
16+
return new Dog(true, "dog");
17+
}
18+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package test.org.springdoc.api.app31;
2+
3+
import test.org.springdoc.api.AbstractSpringDocTest;
4+
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
7+
public class SpringDocApp31Test extends AbstractSpringDocTest {
8+
9+
@SpringBootApplication
10+
static class SpringDocTestApp {}
11+
12+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
{
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "OpenAPI definition",
5+
"version": "v0"
6+
},
7+
"servers": [{
8+
"url": "http://localhost",
9+
"description": "Generated server url"
10+
}],
11+
"paths": {
12+
"/dog": {
13+
"get": {
14+
"tags": ["pet-controller"],
15+
"operationId": "getDog",
16+
"responses": {
17+
"200": {
18+
"description": "OK",
19+
"content": {
20+
"application/hal+json": {
21+
"schema": {
22+
"$ref": "#/components/schemas/Dog"
23+
}
24+
}
25+
}
26+
}
27+
}
28+
}
29+
},
30+
"/any": {
31+
"get": {
32+
"tags": ["pet-controller"],
33+
"operationId": "getAnyPet",
34+
"responses": {
35+
"200": {
36+
"description": "OK",
37+
"content": {
38+
"application/hal+json": {
39+
"schema": {
40+
"oneOf": [{
41+
"$ref": "#/components/schemas/Pet"
42+
}, {
43+
"$ref": "#/components/schemas/Cat"
44+
}, {
45+
"$ref": "#/components/schemas/Dog"
46+
}]
47+
}
48+
}
49+
}
50+
}
51+
}
52+
}
53+
}
54+
},
55+
"components": {
56+
"schemas": {
57+
"Dog": {
58+
"type": "object",
59+
"allOf": [{
60+
"$ref": "#/components/schemas/Pet"
61+
}, {
62+
"type": "object",
63+
"properties": {
64+
"barks": {
65+
"type": "boolean"
66+
}
67+
}
68+
}]
69+
},
70+
"Cat": {
71+
"type": "object",
72+
"allOf": [{
73+
"$ref": "#/components/schemas/Pet"
74+
}, {
75+
"type": "object",
76+
"properties": {
77+
"meows": {
78+
"type": "boolean"
79+
}
80+
}
81+
}]
82+
},
83+
"Pet": {
84+
"required": ["type"],
85+
"type": "object",
86+
"properties": {
87+
"name": {
88+
"type": "string"
89+
},
90+
"type": {
91+
"type": "string"
92+
}
93+
},
94+
"discriminator": {
95+
"propertyName": "type"
96+
}
97+
}
98+
}
99+
}
100+
}

0 commit comments

Comments
 (0)