Skip to content

Commit 89a785f

Browse files
committed
refs - swagger-api/swagger-codegen/issues/8601 - flatten composedSchema
1 parent a46315b commit 89a785f

File tree

4 files changed

+139
-29
lines changed

4 files changed

+139
-29
lines changed

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

Lines changed: 37 additions & 27 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;
@@ -82,14 +83,13 @@ public void flatten(OpenAPI openAPI) {
8283
if (inner.getProperties() != null && inner.getProperties().size() > 0) {
8384
flattenProperties(inner.getProperties(), pathname);
8485
String modelName = resolveModelName(inner.getTitle(), "body");
85-
Schema innerModel = createModelFromProperty(inner, modelName);
86-
String existing = matchGenerated(innerModel);
86+
String existing = matchGenerated(inner);
8787
if (existing != null) {
8888
am.setItems(new Schema().$ref(existing));
8989
} else {
9090
am.setItems(new Schema().$ref(modelName));
91-
addGenerated(modelName, innerModel);
92-
openAPI.getComponents().addSchemas(modelName, innerModel);
91+
addGenerated(modelName, inner);
92+
openAPI.getComponents().addSchemas(modelName, inner);
9393
}
9494
}
9595
}
@@ -114,22 +114,25 @@ public void flatten(OpenAPI openAPI) {
114114
openAPI.getComponents().addSchemas(modelName, model);
115115
}
116116
}
117-
} 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) {
118123
ArraySchema am = (ArraySchema) model;
119124
Schema inner = am.getItems();
120-
121125
if (isObjectSchema(inner)) {
122126
if (inner.getProperties() != null && inner.getProperties().size() > 0) {
123127
flattenProperties(inner.getProperties(), pathname);
124128
String modelName = resolveModelName(inner.getTitle(), parameter.getName());
125-
Schema innerModel = createModelFromProperty(inner, modelName);
126-
String existing = matchGenerated(innerModel);
129+
String existing = matchGenerated(inner);
127130
if (existing != null) {
128131
am.setItems(new Schema().$ref(existing));
129132
} else {
130133
am.setItems(new Schema().$ref(modelName));
131-
addGenerated(modelName, innerModel);
132-
openAPI.getComponents().addSchemas(modelName, innerModel);
134+
addGenerated(modelName, am);
135+
openAPI.getComponents().addSchemas(modelName, am);
133136
}
134137
}
135138
}
@@ -151,17 +154,27 @@ public void flatten(OpenAPI openAPI) {
151154
if (isObjectSchema(mediaSchema)) {
152155
if (mediaSchema.getProperties() != null && mediaSchema.getProperties().size() > 0) {
153156
String modelName = resolveModelName(mediaSchema.getTitle(), "inline_response_" + key);
154-
Schema model = createModelFromProperty(mediaSchema, modelName);
155-
String existing = matchGenerated(model);
157+
String existing = matchGenerated(mediaSchema);
156158
if (existing != null) {
157159
media.setSchema(this.makeRefProperty(existing, mediaSchema));
158160
} else {
159161
media.setSchema(this.makeRefProperty(modelName, mediaSchema));
160-
addGenerated(modelName, model);
161-
openAPI.getComponents().addSchemas(modelName, model);
162+
addGenerated(modelName, mediaSchema);
163+
openAPI.getComponents().addSchemas(modelName, mediaSchema);
162164
}
163165
}
164-
} 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) {
165178
ArraySchema ap = (ArraySchema) mediaSchema;
166179
Schema inner = ap.getItems();
167180

@@ -170,14 +183,13 @@ public void flatten(OpenAPI openAPI) {
170183
flattenProperties(inner.getProperties(), pathname);
171184
String modelName = resolveModelName(inner.getTitle(),
172185
"inline_response_" + key);
173-
Schema innerModel = createModelFromProperty(inner, modelName);
174-
String existing = matchGenerated(innerModel);
186+
String existing = matchGenerated(inner);
175187
if (existing != null) {
176188
ap.setItems(this.makeRefProperty(existing, inner));
177189
} else {
178190
ap.setItems(this.makeRefProperty(modelName, inner));
179-
addGenerated(modelName, innerModel);
180-
openAPI.getComponents().addSchemas(modelName, innerModel);
191+
addGenerated(modelName, inner);
192+
openAPI.getComponents().addSchemas(modelName, inner);
181193
}
182194
}
183195
}
@@ -189,14 +201,13 @@ public void flatten(OpenAPI openAPI) {
189201
flattenProperties(innerProperty.getProperties(), pathname);
190202
String modelName = resolveModelName(innerProperty.getTitle(),
191203
"inline_response_" + key);
192-
Schema innerModel = createModelFromProperty(innerProperty, modelName);
193-
String existing = matchGenerated(innerModel);
204+
String existing = matchGenerated(innerProperty);
194205
if (existing != null) {
195206
mediaSchema.setAdditionalProperties(new Schema().$ref(existing));
196207
} else {
197208
mediaSchema.setAdditionalProperties(new Schema().$ref(modelName));
198-
addGenerated(modelName, innerModel);
199-
openAPI.getComponents().addSchemas(modelName, innerModel);
209+
addGenerated(modelName, innerProperty);
210+
openAPI.getComponents().addSchemas(modelName, innerProperty);
200211
}
201212
}
202213
}
@@ -226,11 +237,10 @@ public void flatten(OpenAPI openAPI) {
226237
if (isObjectSchema(inner)) {
227238
if (inner.getProperties() != null && inner.getProperties().size() > 0) {
228239
String innerModelName = resolveModelName(inner.getTitle(), modelName + "_inner");
229-
Schema innerModel = createModelFromProperty(inner, innerModelName);
230-
String existing = matchGenerated(innerModel);
240+
String existing = matchGenerated(inner);
231241
if (existing == null) {
232-
openAPI.getComponents().addSchemas(innerModelName, innerModel);
233-
addGenerated(innerModelName, innerModel);
242+
openAPI.getComponents().addSchemas(innerModelName, inner);
243+
addGenerated(innerModelName, inner);
234244
m.setItems(new Schema().$ref(innerModelName));
235245
} else {
236246
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)