Skip to content

Commit a98d3e6

Browse files
committed
@Schema oneOf config is ignored when generate the api-docs. Fixes #2705
1 parent 3dba5e6 commit a98d3e6

File tree

4 files changed

+206
-1
lines changed

4 files changed

+206
-1
lines changed

Diff for: springdoc-openapi-starter-common/src/main/java/org/springdoc/core/converters/PolymorphicModelConverter.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import io.swagger.v3.core.converter.ModelConverter;
3939
import io.swagger.v3.core.converter.ModelConverterContext;
4040
import io.swagger.v3.core.util.AnnotationsUtils;
41+
import io.swagger.v3.oas.models.Components;
4142
import io.swagger.v3.oas.models.media.ComposedSchema;
4243
import io.swagger.v3.oas.models.media.ObjectSchema;
4344
import io.swagger.v3.oas.models.media.Schema;
@@ -112,8 +113,16 @@ public Schema resolve(AnnotatedType type, ModelConverterContext context, Iterato
112113
type.resolveAsRef(true);
113114
Schema<?> resolvedSchema = chain.next().resolve(type, context, chain);
114115
resolvedSchema = getResolvedSchema(javaType, resolvedSchema);
115-
if (resolvedSchema == null || resolvedSchema.get$ref() == null)
116+
if (resolvedSchema == null || resolvedSchema.get$ref() == null) {
116117
return resolvedSchema;
118+
}
119+
if(resolvedSchema.get$ref().contains(Components.COMPONENTS_SCHEMAS_REF)) {
120+
String schemaName = resolvedSchema.get$ref().substring(Components.COMPONENTS_SCHEMAS_REF.length());
121+
Schema existingSchema = context.getDefinedModels().get(schemaName);
122+
if (existingSchema != null && existingSchema.getOneOf() != null) {
123+
return resolvedSchema;
124+
}
125+
}
117126
return composePolymorphicSchema(type, resolvedSchema, context.getDefinedModels().values());
118127
}
119128
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package test.org.springdoc.api.v30.app228;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import com.fasterxml.jackson.annotation.JsonSubTypes;
5+
import io.swagger.v3.oas.annotations.media.Schema;
6+
7+
import org.springframework.web.bind.annotation.PostMapping;
8+
import org.springframework.web.bind.annotation.RequestBody;
9+
import org.springframework.web.bind.annotation.RequestMapping;
10+
import org.springframework.web.bind.annotation.RestController;
11+
12+
/**
13+
* @author bnasslahsen
14+
*/
15+
@RestController
16+
@RequestMapping
17+
public class HelloController {
18+
19+
20+
@PostMapping("swaggerTest")
21+
public String swaggerTest(@RequestBody MyRequest myRequest) {
22+
return null;
23+
}
24+
25+
public class MyRequest {
26+
@Schema(oneOf = {Child1.class, Child2.class})
27+
@JsonProperty
28+
private Parent parent;
29+
}
30+
31+
@JsonSubTypes({
32+
@JsonSubTypes.Type(value = Child1.class),
33+
@JsonSubTypes.Type(value = Child2.class),
34+
@JsonSubTypes.Type(value = Child3.class),
35+
})
36+
public abstract class Parent {
37+
@JsonProperty
38+
private String parentProperty;
39+
}
40+
41+
public class Child1 extends Parent {
42+
@JsonProperty
43+
private String childProperty1;
44+
}
45+
46+
public class Child2 extends Parent {
47+
@JsonProperty
48+
private String childProperty2;
49+
}
50+
51+
public class Child3 extends Parent {
52+
@JsonProperty
53+
private String childProperty3;
54+
}
55+
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * *
6+
* * * * * Copyright 2019-2022 the original author or authors.
7+
* * * * *
8+
* * * * * Licensed under the Apache License, Version 2.0 (the "License");
9+
* * * * * you may not use this file except in compliance with the License.
10+
* * * * * You may obtain a copy of the License at
11+
* * * * *
12+
* * * * * https://www.apache.org/licenses/LICENSE-2.0
13+
* * * * *
14+
* * * * * Unless required by applicable law or agreed to in writing, software
15+
* * * * * distributed under the License is distributed on an "AS IS" BASIS,
16+
* * * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* * * * * See the License for the specific language governing permissions and
18+
* * * * * limitations under the License.
19+
* * * *
20+
* * *
21+
* *
22+
*
23+
*/
24+
25+
package test.org.springdoc.api.v30.app228;
26+
27+
import test.org.springdoc.api.v30.AbstractSpringDocV30Test;
28+
29+
import org.springframework.boot.autoconfigure.SpringBootApplication;
30+
31+
public class SpringDocApp228Test extends AbstractSpringDocV30Test {
32+
33+
@SpringBootApplication
34+
static class SpringDocTestApp {}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
"/swaggerTest": {
15+
"post": {
16+
"tags": [
17+
"hello-controller"
18+
],
19+
"operationId": "swaggerTest",
20+
"requestBody": {
21+
"content": {
22+
"application/json": {
23+
"schema": {
24+
"$ref": "#/components/schemas/MyRequest"
25+
}
26+
}
27+
},
28+
"required": true
29+
},
30+
"responses": {
31+
"200": {
32+
"description": "OK",
33+
"content": {
34+
"*/*": {
35+
"schema": {
36+
"type": "string"
37+
}
38+
}
39+
}
40+
}
41+
}
42+
}
43+
}
44+
},
45+
"components": {
46+
"schemas": {
47+
"Child1": {
48+
"type": "object",
49+
"allOf": [
50+
{
51+
"$ref": "#/components/schemas/Parent"
52+
},
53+
{
54+
"type": "object",
55+
"properties": {
56+
"childProperty1": {
57+
"type": "string"
58+
}
59+
}
60+
}
61+
]
62+
},
63+
"Child2": {
64+
"type": "object",
65+
"allOf": [
66+
{
67+
"$ref": "#/components/schemas/Parent"
68+
},
69+
{
70+
"type": "object",
71+
"properties": {
72+
"childProperty2": {
73+
"type": "string"
74+
}
75+
}
76+
}
77+
]
78+
},
79+
"MyRequest": {
80+
"type": "object",
81+
"properties": {
82+
"parent": {
83+
"$ref": "#/components/schemas/Parent"
84+
}
85+
}
86+
},
87+
"Parent": {
88+
"type": "object",
89+
"properties": {
90+
"parentProperty": {
91+
"type": "string"
92+
}
93+
},
94+
"oneOf": [
95+
{
96+
"$ref": "#/components/schemas/Child1"
97+
},
98+
{
99+
"$ref": "#/components/schemas/Child2"
100+
}
101+
]
102+
}
103+
}
104+
}
105+
}

0 commit comments

Comments
 (0)