Skip to content

Commit 9124d9b

Browse files
committed
changes for issue #1382
1 parent e5dadac commit 9124d9b

File tree

4 files changed

+231
-18
lines changed

4 files changed

+231
-18
lines changed

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/InlineModelResolver.java

Lines changed: 86 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,13 @@ public void flatten(OpenAPI openAPI) {
8686
addGenerated(modelName, model);
8787
openAPI.getComponents().addSchemas(modelName, model);
8888
} else if (model instanceof ComposedSchema) {
89-
String modelName = resolveModelName(model.getTitle(), "body");
90-
mediaType.setSchema(new Schema().$ref(modelName));
91-
addGenerated(modelName, model);
92-
openAPI.getComponents().addSchemas(modelName, model);
89+
flattenComposedSchema(model, pathname);
90+
if (model.get$ref() == null) {
91+
String modelName = resolveModelName(model.getTitle(), "body");
92+
mediaType.setSchema(this.makeRefProperty(modelName, model));
93+
addGenerated(modelName, model);
94+
openAPI.getComponents().addSchemas(modelName, model);
95+
}
9396
} else if (model instanceof ArraySchema) {
9497
ArraySchema am = (ArraySchema) model;
9598
Schema inner = am.getItems();
@@ -106,6 +109,14 @@ public void flatten(OpenAPI openAPI) {
106109
addGenerated(modelName, inner);
107110
openAPI.getComponents().addSchemas(modelName, inner);
108111
}
112+
}else if (inner instanceof ComposedSchema && this.flattenComposedSchemas){
113+
flattenComposedSchema(inner,key);
114+
if (inner.get$ref() == null) {
115+
String modelName = resolveModelName(inner.getTitle(), "inline_body_items_" + key + "_" + pathname);
116+
am.setItems(this.makeRefProperty(modelName, inner));
117+
addGenerated(modelName, inner);
118+
openAPI.getComponents().addSchemas(modelName, inner);
119+
}
109120
}
110121
}
111122
}
@@ -149,6 +160,14 @@ public void flatten(OpenAPI openAPI) {
149160
addGenerated(modelName, am);
150161
openAPI.getComponents().addSchemas(modelName, am);
151162
}
163+
}else if (inner instanceof ComposedSchema && this.flattenComposedSchemas){
164+
flattenComposedSchema(inner, parameter.getName());
165+
if (inner.get$ref() == null) {
166+
String modelName = resolveModelName(inner.getTitle(), "inline_parameter_items_" + parameter.getName());
167+
am.setItems(this.makeRefProperty(modelName, inner));
168+
addGenerated(modelName, inner);
169+
openAPI.getComponents().addSchemas(modelName, inner);
170+
}
152171
}
153172
}
154173
}
@@ -177,16 +196,16 @@ public void flatten(OpenAPI openAPI) {
177196
addGenerated(modelName, mediaSchema);
178197
openAPI.getComponents().addSchemas(modelName, mediaSchema);
179198
}
180-
}
181-
} else if (mediaSchema instanceof ComposedSchema ){
182-
String modelName = resolveModelName(mediaSchema.getTitle(), "inline_response_" + key);
183-
String existing = matchGenerated(mediaSchema);
184-
if (existing != null) {
185-
media.setSchema(this.makeRefProperty(existing, mediaSchema));
186-
} else {
187-
media.setSchema(this.makeRefProperty(modelName, mediaSchema));
188-
addGenerated(modelName, mediaSchema);
189-
openAPI.getComponents().addSchemas(modelName, mediaSchema);
199+
}else if (mediaSchema instanceof ComposedSchema) {
200+
String modelName = resolveModelName(mediaSchema.getTitle(), "inline_response_" + key);
201+
String existing = matchGenerated(mediaSchema);
202+
if (existing != null) {
203+
media.setSchema(this.makeRefProperty(existing, mediaSchema));
204+
} else {
205+
media.setSchema(this.makeRefProperty(modelName, mediaSchema));
206+
addGenerated(modelName, mediaSchema);
207+
openAPI.getComponents().addSchemas(modelName, mediaSchema);
208+
}
190209
}
191210

192211
}else if (mediaSchema instanceof ArraySchema) {
@@ -206,8 +225,17 @@ public void flatten(OpenAPI openAPI) {
206225
addGenerated(modelName, inner);
207226
openAPI.getComponents().addSchemas(modelName, inner);
208227
}
228+
}else if (inner instanceof ComposedSchema && this.flattenComposedSchemas){
229+
flattenComposedSchema(inner,key);
230+
if (inner.get$ref() == null) {
231+
String modelName = resolveModelName(inner.getTitle(), "inline_response_items" + key);
232+
ap.setItems(this.makeRefProperty(modelName, inner));
233+
addGenerated(modelName, inner);
234+
openAPI.getComponents().addSchemas(modelName, inner);
235+
}
209236
}
210237
}
238+
211239
} else if (mediaSchema.getAdditionalProperties() != null && mediaSchema.getAdditionalProperties() instanceof Schema) {
212240

213241
Schema innerProperty = (Schema) mediaSchema.getAdditionalProperties();
@@ -260,6 +288,14 @@ public void flatten(OpenAPI openAPI) {
260288
} else {
261289
m.setItems(new Schema().$ref(existing));
262290
}
291+
}else if (inner instanceof ComposedSchema && this.flattenComposedSchemas){
292+
flattenComposedSchema(inner,modelName);
293+
if (inner.get$ref() == null) {
294+
modelName = resolveModelName(inner.getTitle(), "inline_array_items_" + modelName);
295+
m.setItems(this.makeRefProperty(modelName, inner));
296+
addGenerated(modelName, inner);
297+
openAPI.getComponents().addSchemas(modelName, inner);
298+
}
263299
}
264300
}
265301
} else if (model instanceof ComposedSchema) {
@@ -473,6 +509,40 @@ public void flattenProperties(Map<String, Schema> properties, String path) {
473509
}
474510
}
475511

512+
private void flattenComposedSchema(Schema inner, String key) {
513+
514+
ComposedSchema composedSchema = (ComposedSchema) inner;
515+
String inlineModelName = "";
516+
517+
List<Schema> list = null;
518+
if (composedSchema.getAllOf() != null) {
519+
list = composedSchema.getAllOf();
520+
inlineModelName = "AllOf";
521+
}else if (composedSchema.getAnyOf() != null) {
522+
list = composedSchema.getAnyOf();
523+
inlineModelName = "AnyOf";
524+
}else if (composedSchema.getOneOf() != null) {
525+
list = composedSchema.getOneOf();
526+
inlineModelName = "OneOf";
527+
}
528+
529+
for(int i= 0; i<list.size();i++){
530+
if (list.get(i).get$ref() == null){
531+
Schema inline = list.get(i);
532+
if (inline.getProperties()!= null){
533+
flattenProperties(inline.getProperties(), key);
534+
}
535+
if (this.flattenComposedSchemas) {
536+
int position = i+1;
537+
inlineModelName = resolveModelName(inline.getTitle(), key + inlineModelName + "_" + position);
538+
list.set(i,new Schema().$ref(inlineModelName));
539+
addGenerated(inlineModelName, inline);
540+
openAPI.getComponents().addSchemas(inlineModelName, inline);
541+
}
542+
}
543+
}
544+
}
545+
476546
@SuppressWarnings("static-method")
477547
public Schema modelFromProperty(ArraySchema object, @SuppressWarnings("unused") String path) {
478548
String description = object.getDescription();
@@ -593,7 +663,8 @@ public void copyVendorExtensions(Schema source, Schema target) {
593663
private boolean isObjectSchema(Schema schema) {
594664
return schema instanceof ObjectSchema
595665
|| "object".equalsIgnoreCase(schema.getType())
596-
|| (schema.getType() == null && schema.getProperties() != null && !schema.getProperties().isEmpty());
666+
|| (schema.getType() == null && schema.getProperties() != null && !schema.getProperties().isEmpty()
667+
|| schema instanceof ComposedSchema);
597668
}
598669

599670
public boolean isSkipMatches() {

modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,68 @@ public class OpenAPIV3ParserTest {
8282
protected int serverPort = getDynamicPort();
8383
protected WireMockServer wireMockServer;
8484

85+
@Test
86+
public void testIssueFlattenArraySchemaItemsInlineModelFalse() {
87+
OpenAPIV3Parser openApiParser = new OpenAPIV3Parser();
88+
ParseOptions options = new ParseOptions();
89+
options.setResolve(true);
90+
options.setFlatten(true);
91+
options.setFlattenComposedSchemas(false);
92+
options.setCamelCaseFlattenNaming(false);
93+
SwaggerParseResult parseResult = openApiParser.readLocation("flattenArrayItems.yaml", null, options);
94+
OpenAPI openAPI = parseResult.getOpenAPI();
95+
96+
//responses
97+
assertNull(openAPI.getComponents().getSchemas().get("Inline_response_items200"));
98+
assertNull(openAPI.getComponents().getSchemas().get("Inline_response_400"));
99+
100+
//parameters
101+
assertNull(openAPI.getComponents().getSchemas().get("Inline_parameter_items_bodylimit"));
102+
assertNull(openAPI.getComponents().getSchemas().get("Pagelimit"));
103+
104+
//requestBodies
105+
assertNull(openAPI.getComponents().getSchemas().get("Body"));
106+
assertNull(openAPI.getComponents().getSchemas().get("Inline_response_items200"));
107+
108+
//components
109+
assertNull(openAPI.getComponents().getSchemas().get("Inline_array_items_ArrayTest"));
110+
111+
}
112+
113+
@Test
114+
public void testIssueFlattenArraySchemaItemsInlineModelTrue() {
115+
OpenAPIV3Parser openApiParser = new OpenAPIV3Parser();
116+
ParseOptions options = new ParseOptions();
117+
options.setResolve(true);
118+
options.setFlatten(true);
119+
options.setFlattenComposedSchemas(true);
120+
options.setCamelCaseFlattenNaming(true);
121+
SwaggerParseResult parseResult = openApiParser.readLocation("flattenArrayItems.yaml", null, options);
122+
OpenAPI openAPI = parseResult.getOpenAPI();
123+
124+
//responses
125+
assertNotNull(openAPI.getComponents().getSchemas().get("Inline_response_items200"));
126+
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("Inline_response_items200")).getAnyOf().get(0).get$ref(),"#/components/schemas/Macaw");
127+
assertNotNull(openAPI.getComponents().getSchemas().get("Inline_response_400"));
128+
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("Inline_response_400")).getAnyOf().get(0).get$ref(),"#/components/schemas/Macaw3");
129+
130+
//parameters
131+
assertNotNull(openAPI.getComponents().getSchemas().get("Inline_parameter_items_bodylimit"));
132+
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("Inline_parameter_items_bodylimit")).getAnyOf().get(0).get$ref(),"#/components/schemas/Macaw1");
133+
assertNotNull(openAPI.getComponents().getSchemas().get("Pagelimit"));
134+
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("Pagelimit")).getOneOf().get(0).get$ref(),"#/components/schemas/Macaw2");
135+
136+
//requestBodies
137+
assertNotNull(openAPI.getComponents().getSchemas().get("Body"));
138+
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("Body")).getAllOf().get(1).get$ref(),"#/components/schemas/requestBodiesAllOf_2");
139+
assertNotNull(openAPI.getComponents().getSchemas().get("Inline_response_items200"));
140+
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("Inline_body_items_applicationxml_requestBodies")).getAllOf().get(1).get$ref(),"#/components/schemas/ApplicationxmlAllOf_2");
141+
142+
//components
143+
assertNotNull(openAPI.getComponents().getSchemas().get("Inline_array_items_ArrayTest"));
144+
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("Inline_array_items_ArrayTest")).getOneOf().get(1).get$ref(),"#/components/schemas/ArrayTestOneOf_2");
145+
}
146+
85147

86148
@Test
87149
public void testCamelCaseFlattenNamingFalse() {

modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/util/InlineModelResolverTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -938,13 +938,10 @@ public void testInlineMapResponseWithObjectSchema() throws Exception {
938938

939939
ApiResponses apiResponses = new ApiResponses().addApiResponse("200",apiResponse);
940940

941-
942-
943941
openAPI.path("/foo/baz", new PathItem()
944942
.get(new Operation()
945943
.responses(apiResponses)));
946944

947-
948945
new InlineModelResolver().flatten(openAPI);
949946

950947
ApiResponse response = openAPI.getPaths().get("/foo/baz").getGet().getResponses().get("200");
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
openapi: 3.0.2
2+
info:
3+
title: test - OAS3
4+
version: 1.0.0
5+
paths:
6+
"/parameters":
7+
get:
8+
description: ...
9+
parameters:
10+
- name: bodylimit
11+
in: query
12+
schema:
13+
type: array
14+
items:
15+
anyOf:
16+
- "$ref": "#/components/schemas/Macaw1"
17+
- "$ref": "#/components/schemas/Parakeet1"
18+
- name: pagelimit
19+
in: query
20+
schema:
21+
oneOf:
22+
- "$ref": "#/components/schemas/Macaw2"
23+
- "$ref": "#/components/schemas/Parakeet2"
24+
/responses:
25+
get:
26+
responses:
27+
'200':
28+
description: successful operation
29+
content:
30+
application/json:
31+
schema:
32+
type: array
33+
items:
34+
anyOf:
35+
- "$ref": "#/components/schemas/Macaw"
36+
- "$ref": "#/components/schemas/Parakeet"
37+
'400':
38+
description: successful operation
39+
content:
40+
application/json:
41+
schema:
42+
anyOf:
43+
- "$ref": "#/components/schemas/Macaw3"
44+
- "$ref": "#/components/schemas/Parakeet3"
45+
/requestBodies:
46+
post:
47+
requestBody:
48+
content:
49+
"application/json":
50+
schema:
51+
type: object
52+
allOf:
53+
- $ref: '#/components/schemas/Address'
54+
- type: object
55+
required:
56+
- gps
57+
properties:
58+
gps:
59+
type: string
60+
"application/xml":
61+
schema:
62+
type: array
63+
items:
64+
allOf:
65+
- $ref: '#/components/schemas/Address2'
66+
- type: object
67+
required:
68+
- gps2
69+
properties:
70+
gps2:
71+
type: string
72+
components:
73+
schemas:
74+
ArrayTest:
75+
type: array
76+
items:
77+
oneOf:
78+
- $ref: "#/components/schemas/Foo"
79+
- type: object
80+
properties:
81+
foo:
82+
type: string
83+

0 commit comments

Comments
 (0)