Skip to content

Commit 585773c

Browse files
authored
Merge pull request #1230 from swagger-api/codegen-ticket-8601
refs swagger-api/swagger-codegen#8601 - flatten composed models in body schema
2 parents 3420db7 + 65b00a9 commit 585773c

File tree

4 files changed

+144
-30
lines changed

4 files changed

+144
-30
lines changed

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

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import io.swagger.v3.oas.models.media.MediaType;
1010
import io.swagger.v3.oas.models.media.ObjectSchema;
1111
import io.swagger.v3.oas.models.media.Schema;
12+
import io.swagger.v3.oas.models.media.StringSchema;
1213
import io.swagger.v3.oas.models.media.XML;
1314
import io.swagger.v3.oas.models.parameters.Parameter;
1415
import io.swagger.v3.oas.models.parameters.RequestBody;
@@ -69,7 +70,11 @@ public void flatten(OpenAPI openAPI) {
6970
mediaType.setSchema(new Schema().$ref(modelName));
7071
addGenerated(modelName, model);
7172
openAPI.getComponents().addSchemas(modelName, model);
72-
73+
} else if (model instanceof ComposedSchema) {
74+
String modelName = resolveModelName(model.getTitle(), "body");
75+
mediaType.setSchema(new Schema().$ref(modelName));
76+
addGenerated(modelName, model);
77+
openAPI.getComponents().addSchemas(modelName, model);
7378
} else if (model instanceof ArraySchema) {
7479
ArraySchema am = (ArraySchema) model;
7580
Schema inner = am.getItems();
@@ -78,14 +83,13 @@ public void flatten(OpenAPI openAPI) {
7883
if (inner.getProperties() != null && inner.getProperties().size() > 0) {
7984
flattenProperties(inner.getProperties(), pathname);
8085
String modelName = resolveModelName(inner.getTitle(), "body");
81-
Schema innerModel = createModelFromProperty(inner, modelName);
82-
String existing = matchGenerated(innerModel);
86+
String existing = matchGenerated(inner);
8387
if (existing != null) {
8488
am.setItems(new Schema().$ref(existing));
8589
} else {
8690
am.setItems(new Schema().$ref(modelName));
87-
addGenerated(modelName, innerModel);
88-
openAPI.getComponents().addSchemas(modelName, innerModel);
91+
addGenerated(modelName, inner);
92+
openAPI.getComponents().addSchemas(modelName, inner);
8993
}
9094
}
9195
}
@@ -110,22 +114,25 @@ public void flatten(OpenAPI openAPI) {
110114
openAPI.getComponents().addSchemas(modelName, model);
111115
}
112116
}
113-
} else if (model instanceof ArraySchema) {
117+
}else if (model instanceof ComposedSchema) {
118+
String modelName = resolveModelName(model.getTitle(), parameter.getName());
119+
parameter.setSchema(new Schema().$ref(modelName));
120+
addGenerated(modelName, model);
121+
openAPI.getComponents().addSchemas(modelName, model);
122+
}else if (model instanceof ArraySchema) {
114123
ArraySchema am = (ArraySchema) model;
115124
Schema inner = am.getItems();
116-
117125
if (isObjectSchema(inner)) {
118126
if (inner.getProperties() != null && inner.getProperties().size() > 0) {
119127
flattenProperties(inner.getProperties(), pathname);
120128
String modelName = resolveModelName(inner.getTitle(), parameter.getName());
121-
Schema innerModel = createModelFromProperty(inner, modelName);
122-
String existing = matchGenerated(innerModel);
129+
String existing = matchGenerated(inner);
123130
if (existing != null) {
124131
am.setItems(new Schema().$ref(existing));
125132
} else {
126133
am.setItems(new Schema().$ref(modelName));
127-
addGenerated(modelName, innerModel);
128-
openAPI.getComponents().addSchemas(modelName, innerModel);
134+
addGenerated(modelName, am);
135+
openAPI.getComponents().addSchemas(modelName, am);
129136
}
130137
}
131138
}
@@ -147,17 +154,27 @@ public void flatten(OpenAPI openAPI) {
147154
if (isObjectSchema(mediaSchema)) {
148155
if (mediaSchema.getProperties() != null && mediaSchema.getProperties().size() > 0) {
149156
String modelName = resolveModelName(mediaSchema.getTitle(), "inline_response_" + key);
150-
Schema model = createModelFromProperty(mediaSchema, modelName);
151-
String existing = matchGenerated(model);
157+
String existing = matchGenerated(mediaSchema);
152158
if (existing != null) {
153159
media.setSchema(this.makeRefProperty(existing, mediaSchema));
154160
} else {
155161
media.setSchema(this.makeRefProperty(modelName, mediaSchema));
156-
addGenerated(modelName, model);
157-
openAPI.getComponents().addSchemas(modelName, model);
162+
addGenerated(modelName, mediaSchema);
163+
openAPI.getComponents().addSchemas(modelName, mediaSchema);
158164
}
159165
}
160-
} else if (mediaSchema instanceof ArraySchema) {
166+
} else if (mediaSchema instanceof ComposedSchema ){
167+
String modelName = resolveModelName(mediaSchema.getTitle(), "inline_response_" + key);
168+
String existing = matchGenerated(mediaSchema);
169+
if (existing != null) {
170+
media.setSchema(this.makeRefProperty(existing, mediaSchema));
171+
} else {
172+
media.setSchema(this.makeRefProperty(modelName, mediaSchema));
173+
addGenerated(modelName, mediaSchema);
174+
openAPI.getComponents().addSchemas(modelName, mediaSchema);
175+
}
176+
177+
}else if (mediaSchema instanceof ArraySchema) {
161178
ArraySchema ap = (ArraySchema) mediaSchema;
162179
Schema inner = ap.getItems();
163180

@@ -166,14 +183,13 @@ public void flatten(OpenAPI openAPI) {
166183
flattenProperties(inner.getProperties(), pathname);
167184
String modelName = resolveModelName(inner.getTitle(),
168185
"inline_response_" + key);
169-
Schema innerModel = createModelFromProperty(inner, modelName);
170-
String existing = matchGenerated(innerModel);
186+
String existing = matchGenerated(inner);
171187
if (existing != null) {
172188
ap.setItems(this.makeRefProperty(existing, inner));
173189
} else {
174190
ap.setItems(this.makeRefProperty(modelName, inner));
175-
addGenerated(modelName, innerModel);
176-
openAPI.getComponents().addSchemas(modelName, innerModel);
191+
addGenerated(modelName, inner);
192+
openAPI.getComponents().addSchemas(modelName, inner);
177193
}
178194
}
179195
}
@@ -185,14 +201,13 @@ public void flatten(OpenAPI openAPI) {
185201
flattenProperties(innerProperty.getProperties(), pathname);
186202
String modelName = resolveModelName(innerProperty.getTitle(),
187203
"inline_response_" + key);
188-
Schema innerModel = createModelFromProperty(innerProperty, modelName);
189-
String existing = matchGenerated(innerModel);
204+
String existing = matchGenerated(innerProperty);
190205
if (existing != null) {
191206
mediaSchema.setAdditionalProperties(new Schema().$ref(existing));
192207
} else {
193208
mediaSchema.setAdditionalProperties(new Schema().$ref(modelName));
194-
addGenerated(modelName, innerModel);
195-
openAPI.getComponents().addSchemas(modelName, innerModel);
209+
addGenerated(modelName, innerProperty);
210+
openAPI.getComponents().addSchemas(modelName, innerProperty);
196211
}
197212
}
198213
}
@@ -222,11 +237,10 @@ public void flatten(OpenAPI openAPI) {
222237
if (isObjectSchema(inner)) {
223238
if (inner.getProperties() != null && inner.getProperties().size() > 0) {
224239
String innerModelName = resolveModelName(inner.getTitle(), modelName + "_inner");
225-
Schema innerModel = createModelFromProperty(inner, innerModelName);
226-
String existing = matchGenerated(innerModel);
240+
String existing = matchGenerated(inner);
227241
if (existing == null) {
228-
openAPI.getComponents().addSchemas(innerModelName, innerModel);
229-
addGenerated(innerModelName, innerModel);
242+
openAPI.getComponents().addSchemas(innerModelName, inner);
243+
addGenerated(innerModelName, inner);
230244
m.setItems(new Schema().$ref(innerModelName));
231245
} else {
232246
m.setItems(new Schema().$ref(existing));

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,23 @@ public class OpenAPIV3ParserTest {
6666
protected int serverPort = getDynamicPort();
6767
protected WireMockServer wireMockServer;
6868

69+
@Test
70+
public void testCodegenIssue8601() {
71+
OpenAPIV3Parser openApiParser = new OpenAPIV3Parser();
72+
ParseOptions options = new ParseOptions();
73+
options.setResolve(true);
74+
options.setFlatten(true);
75+
SwaggerParseResult parseResult = openApiParser.readLocation("codegen-issue-8601.yaml", null, options);
76+
77+
OpenAPI openAPI = parseResult.getOpenAPI();
78+
assertNotNull(openAPI.getComponents().getSchemas().get("status"));
79+
assertNotNull(openAPI.getComponents().getSchemas().get("body"));
80+
assertNotNull(openAPI.getComponents().getSchemas().get("inline_response_200"));
81+
assertNotNull(openAPI.getComponents().getSchemas().get("body_1"));
82+
assertNotNull(openAPI.getComponents().getSchemas().get("Test1"));
83+
assertNotNull(openAPI.getComponents().getSchemas().get("Test2"));
84+
}
85+
6986
@Test
7087
public void testCodegenIssue9773() {
7188
OpenAPIV3Parser openApiParser = new OpenAPIV3Parser();

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,8 +1345,6 @@ public void testInlineItemsSchema() throws Exception {
13451345
options.setFlatten(true);
13461346
OpenAPI openAPI = new OpenAPIV3Parser().read("flatten.json",null, options);
13471347

1348-
System.out.println(openAPI);
1349-
13501348
assertNotNull(openAPI);
13511349
assertNotNull(openAPI.getComponents().getSchemas().get("inline_response_200"));
13521350
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
openapi: "3.0.0"
2+
info:
3+
version: 1.0.0
4+
title: Test
5+
paths:
6+
/test:
7+
post:
8+
operationId: test
9+
parameters:
10+
- name: status
11+
in: query
12+
description: Status values that need to be considered for filter
13+
required: false
14+
style: pipeDelimited
15+
schema:
16+
anyOf:
17+
- type: string
18+
- type: integer
19+
requestBody:
20+
required: true
21+
content:
22+
application/json:
23+
schema:
24+
allOf:
25+
- $ref: '#/components/schemas/Test1'
26+
- type: object
27+
responses:
28+
default:
29+
description: response...
30+
get:
31+
requestBody:
32+
required: true
33+
content:
34+
application/json:
35+
schema:
36+
oneOf:
37+
- type: object
38+
properties:
39+
title:
40+
type: string
41+
authors:
42+
type: array
43+
items:
44+
type: string
45+
isbn:
46+
type: string
47+
required:
48+
- title
49+
example:
50+
title: The Hitchhiker's Guide to the Galaxy
51+
authors:
52+
- Douglas Adams
53+
isbn: 0-330-25864-8
54+
- type: object
55+
properties:
56+
title:
57+
type: string
58+
directors:
59+
type: array
60+
items:
61+
type: string
62+
year:
63+
type: integer
64+
required:
65+
- year
66+
example:
67+
title: Blade Runner
68+
directors:
69+
- Ridley Scott
70+
year: 1982
71+
responses:
72+
'200':
73+
description: An array containing strings and/or integers
74+
content:
75+
application/json:
76+
schema:
77+
anyOf:
78+
- type: string
79+
- type: integer
80+
components:
81+
schemas:
82+
Test1:
83+
type: object
84+
Test2:
85+
type: object

0 commit comments

Comments
 (0)