@@ -74,12 +74,11 @@ public void flatten(OpenAPI openAPI) {
74
74
ArraySchema am = (ArraySchema ) model ;
75
75
Schema inner = am .getItems ();
76
76
77
- if (inner instanceof ObjectSchema ) {
78
- ObjectSchema op = (ObjectSchema ) inner ;
79
- if (op .getProperties () != null && op .getProperties ().size () > 0 ) {
80
- flattenProperties (op .getProperties (), pathname );
81
- String modelName = resolveModelName (op .getTitle (), "body" );
82
- Schema innerModel = modelFromProperty (op , modelName );
77
+ if (isObjectSchema (inner )) {
78
+ if (inner .getProperties () != null && inner .getProperties ().size () > 0 ) {
79
+ flattenProperties (inner .getProperties (), pathname );
80
+ String modelName = resolveModelName (inner .getTitle (), "body" );
81
+ Schema innerModel = createModelFromProperty (inner , modelName );
83
82
String existing = matchGenerated (innerModel );
84
83
if (existing != null ) {
85
84
am .setItems (new Schema ().$ref (existing ));
@@ -115,12 +114,11 @@ public void flatten(OpenAPI openAPI) {
115
114
ArraySchema am = (ArraySchema ) model ;
116
115
Schema inner = am .getItems ();
117
116
118
- if (inner instanceof ObjectSchema ) {
119
- ObjectSchema op = (ObjectSchema ) inner ;
120
- if (op .getProperties () != null && op .getProperties ().size () > 0 ) {
121
- flattenProperties (op .getProperties (), pathname );
122
- String modelName = resolveModelName (op .getTitle (), parameter .getName ());
123
- Schema innerModel = modelFromProperty (op , modelName );
117
+ if (isObjectSchema (inner )) {
118
+ if (inner .getProperties () != null && inner .getProperties ().size () > 0 ) {
119
+ flattenProperties (inner .getProperties (), pathname );
120
+ String modelName = resolveModelName (inner .getTitle (), parameter .getName ());
121
+ Schema innerModel = createModelFromProperty (inner , modelName );
124
122
String existing = matchGenerated (innerModel );
125
123
if (existing != null ) {
126
124
am .setItems (new Schema ().$ref (existing ));
@@ -145,57 +143,54 @@ public void flatten(OpenAPI openAPI) {
145
143
if (content .get (name ) != null ) {
146
144
MediaType media = content .get (name );
147
145
if (media .getSchema () != null ) {
148
- Schema property = media .getSchema ();
149
- if (property instanceof ObjectSchema ) {
150
- ObjectSchema op = (ObjectSchema ) property ;
151
- if (op .getProperties () != null && op .getProperties ().size () > 0 ) {
152
- String modelName = resolveModelName (op .getTitle (), "inline_response_" + key );
153
- Schema model = modelFromProperty (op , modelName );
146
+ Schema mediaSchema = media .getSchema ();
147
+ if (isObjectSchema (mediaSchema )) {
148
+ if (mediaSchema .getProperties () != null && mediaSchema .getProperties ().size () > 0 ) {
149
+ String modelName = resolveModelName (mediaSchema .getTitle (), "inline_response_" + key );
150
+ Schema model = createModelFromProperty (mediaSchema , modelName );
154
151
String existing = matchGenerated (model );
155
152
if (existing != null ) {
156
- media .setSchema (this .makeRefProperty (existing , property ));
153
+ media .setSchema (this .makeRefProperty (existing , mediaSchema ));
157
154
} else {
158
- media .setSchema (this .makeRefProperty (modelName , property ));
155
+ media .setSchema (this .makeRefProperty (modelName , mediaSchema ));
159
156
addGenerated (modelName , model );
160
157
openAPI .getComponents ().addSchemas (modelName , model );
161
158
}
162
159
}
163
- } else if (property instanceof ArraySchema ) {
164
- ArraySchema ap = (ArraySchema ) property ;
160
+ } else if (mediaSchema instanceof ArraySchema ) {
161
+ ArraySchema ap = (ArraySchema ) mediaSchema ;
165
162
Schema inner = ap .getItems ();
166
163
167
- if (inner instanceof ObjectSchema ) {
168
- ObjectSchema op = (ObjectSchema ) inner ;
169
- if (op .getProperties () != null && op .getProperties ().size () > 0 ) {
170
- flattenProperties (op .getProperties (), pathname );
171
- String modelName = resolveModelName (op .getTitle (),
164
+ if (isObjectSchema (inner )) {
165
+ if (inner .getProperties () != null && inner .getProperties ().size () > 0 ) {
166
+ flattenProperties (inner .getProperties (), pathname );
167
+ String modelName = resolveModelName (inner .getTitle (),
172
168
"inline_response_" + key );
173
- Schema innerModel = modelFromProperty ( op , modelName );
169
+ Schema innerModel = createModelFromProperty ( inner , modelName );
174
170
String existing = matchGenerated (innerModel );
175
171
if (existing != null ) {
176
- ap .setItems (this .makeRefProperty (existing , op ));
172
+ ap .setItems (this .makeRefProperty (existing , inner ));
177
173
} else {
178
- ap .setItems (this .makeRefProperty (modelName , op ));
174
+ ap .setItems (this .makeRefProperty (modelName , inner ));
179
175
addGenerated (modelName , innerModel );
180
176
openAPI .getComponents ().addSchemas (modelName , innerModel );
181
177
}
182
178
}
183
179
}
184
- } else if (property .getAdditionalProperties () != null && property .getAdditionalProperties () instanceof Schema ) {
185
-
186
- Schema innerProperty = (Schema ) property .getAdditionalProperties ();
187
- if (innerProperty instanceof ObjectSchema ) {
188
- ObjectSchema op = (ObjectSchema ) innerProperty ;
189
- if (op .getProperties () != null && op .getProperties ().size () > 0 ) {
190
- flattenProperties (op .getProperties (), pathname );
191
- String modelName = resolveModelName (op .getTitle (),
180
+ } else if (mediaSchema .getAdditionalProperties () != null && mediaSchema .getAdditionalProperties () instanceof Schema ) {
181
+
182
+ Schema innerProperty = (Schema ) mediaSchema .getAdditionalProperties ();
183
+ if (isObjectSchema (innerProperty )) {
184
+ if (innerProperty .getProperties () != null && innerProperty .getProperties ().size () > 0 ) {
185
+ flattenProperties (innerProperty .getProperties (), pathname );
186
+ String modelName = resolveModelName (innerProperty .getTitle (),
192
187
"inline_response_" + key );
193
- Schema innerModel = modelFromProperty ( op , modelName );
188
+ Schema innerModel = createModelFromProperty ( innerProperty , modelName );
194
189
String existing = matchGenerated (innerModel );
195
190
if (existing != null ) {
196
- property .setAdditionalProperties (new Schema ().$ref (existing ));
191
+ mediaSchema .setAdditionalProperties (new Schema ().$ref (existing ));
197
192
} else {
198
- property .setAdditionalProperties (new Schema ().$ref (modelName ));
193
+ mediaSchema .setAdditionalProperties (new Schema ().$ref (modelName ));
199
194
addGenerated (modelName , innerModel );
200
195
openAPI .getComponents ().addSchemas (modelName , innerModel );
201
196
}
@@ -224,11 +219,10 @@ public void flatten(OpenAPI openAPI) {
224
219
} else if (model instanceof ArraySchema ) {
225
220
ArraySchema m = (ArraySchema ) model ;
226
221
Schema inner = m .getItems ();
227
- if (inner instanceof ObjectSchema ) {
228
- ObjectSchema op = (ObjectSchema ) inner ;
229
- if (op .getProperties () != null && op .getProperties ().size () > 0 ) {
230
- String innerModelName = resolveModelName (op .getTitle (), modelName + "_inner" );
231
- Schema innerModel = modelFromProperty (op , innerModelName );
222
+ if (isObjectSchema (inner )) {
223
+ if (inner .getProperties () != null && inner .getProperties ().size () > 0 ) {
224
+ String innerModelName = resolveModelName (inner .getTitle (), modelName + "_inner" );
225
+ Schema innerModel = createModelFromProperty (inner , innerModelName );
232
226
String existing = matchGenerated (innerModel );
233
227
if (existing == null ) {
234
228
openAPI .getComponents ().addSchemas (innerModelName , innerModel );
@@ -328,13 +322,10 @@ public void flattenProperties(Map<String, Schema> properties, String path) {
328
322
Map <String , Schema > modelsToAdd = new HashMap <>();
329
323
for (String key : properties .keySet ()) {
330
324
Schema property = properties .get (key );
331
- if (property instanceof ObjectSchema && ((ObjectSchema ) property ).getProperties () != null
332
- && ((ObjectSchema ) property ).getProperties ().size () > 0 ) {
333
-
334
- ObjectSchema op = (ObjectSchema ) property ;
325
+ if (isObjectSchema (property ) && property .getProperties () != null && property .getProperties ().size () > 0 ) {
335
326
336
- String modelName = resolveModelName (op .getTitle (), path + "_" + key );
337
- Schema model = modelFromProperty ( op , modelName );
327
+ String modelName = resolveModelName (property .getTitle (), path + "_" + key );
328
+ Schema model = createModelFromProperty ( property , modelName );
338
329
339
330
String existing = matchGenerated (model );
340
331
@@ -350,12 +341,11 @@ public void flattenProperties(Map<String, Schema> properties, String path) {
350
341
ArraySchema ap = (ArraySchema ) property ;
351
342
Schema inner = ap .getItems ();
352
343
353
- if (inner instanceof ObjectSchema ) {
354
- ObjectSchema op = (ObjectSchema ) inner ;
355
- if (op .getProperties () != null && op .getProperties ().size () > 0 ) {
356
- flattenProperties (op .getProperties (), path );
357
- String modelName = resolveModelName (op .getTitle (), path + "_" + key );
358
- Schema innerModel = modelFromProperty (op , modelName );
344
+ if (isObjectSchema (inner )) {
345
+ if (inner .getProperties () != null && inner .getProperties ().size () > 0 ) {
346
+ flattenProperties (inner .getProperties (), path );
347
+ String modelName = resolveModelName (inner .getTitle (), path + "_" + key );
348
+ Schema innerModel = createModelFromProperty (inner , modelName );
359
349
String existing = matchGenerated (innerModel );
360
350
if (existing != null ) {
361
351
ap .setItems (new Schema ().$ref (existing ));
@@ -369,12 +359,11 @@ public void flattenProperties(Map<String, Schema> properties, String path) {
369
359
} else if (property .getAdditionalProperties () != null && property .getAdditionalProperties () instanceof Schema ) {
370
360
Schema inner = (Schema ) property .getAdditionalProperties ();
371
361
372
- if (inner instanceof ObjectSchema ) {
373
- ObjectSchema op = (ObjectSchema ) inner ;
374
- if (op .getProperties () != null && op .getProperties ().size () > 0 ) {
375
- flattenProperties (op .getProperties (), path );
376
- String modelName = resolveModelName (op .getTitle (), path + "_" + key );
377
- Schema innerModel = modelFromProperty (op , modelName );
362
+ if (isObjectSchema (inner )) {
363
+ if (inner .getProperties () != null && inner .getProperties ().size () > 0 ) {
364
+ flattenProperties (inner .getProperties (), path );
365
+ String modelName = resolveModelName (inner .getTitle (), path + "_" + key );
366
+ Schema innerModel = createModelFromProperty (inner , modelName );
378
367
String existing = matchGenerated (innerModel );
379
368
if (existing != null ) {
380
369
property .setAdditionalProperties (new Schema ().$ref (existing ));
@@ -420,26 +409,26 @@ public Schema modelFromProperty(ArraySchema object, @SuppressWarnings("unused")
420
409
return null ;
421
410
}
422
411
423
- public Schema modelFromProperty ( ObjectSchema object , String path ) {
424
- String description = object .getDescription ();
412
+ public Schema createModelFromProperty ( Schema schema , String path ) {
413
+ String description = schema .getDescription ();
425
414
String example = null ;
426
- List <String > requiredList = object .getRequired ();
415
+ List <String > requiredList = schema .getRequired ();
427
416
428
417
429
- Object obj = object .getExample ();
418
+ Object obj = schema .getExample ();
430
419
if (obj != null ) {
431
420
example = obj .toString ();
432
421
}
433
- String name = object .getName ();
434
- XML xml = object .getXml ();
435
- Map <String , Schema > properties = object .getProperties ();
422
+ String name = schema .getName ();
423
+ XML xml = schema .getXml ();
424
+ Map <String , Schema > properties = schema .getProperties ();
436
425
437
426
Schema model = new Schema ();//TODO Verify this!
438
427
model .setDescription (description );
439
428
model .setExample (example );
440
429
model .setName (name );
441
430
model .setXml (xml );
442
- model .setType (object .getType ());
431
+ model .setType (schema .getType ());
443
432
model .setRequired (requiredList );
444
433
445
434
if (properties != null ) {
@@ -507,4 +496,10 @@ public void setSkipMatches(boolean skipMatches) {
507
496
this .skipMatches = skipMatches ;
508
497
}
509
498
510
- }
499
+ private boolean isObjectSchema (Schema schema ) {
500
+ return schema instanceof ObjectSchema
501
+ || "object" .equalsIgnoreCase (schema .getType ())
502
+ || (schema .getType () == null && schema .getProperties () != null && !schema .getProperties ().isEmpty ());
503
+
504
+ }
505
+ }
0 commit comments