@@ -33,12 +33,13 @@ public class InlineModelResolver {
33
33
Map <String , String > generatedSignature = new HashMap <>();
34
34
35
35
private boolean flattenComposedSchemas ;
36
+ private boolean camelCaseFlattenNaming ;
36
37
38
+ public InlineModelResolver (){this (false ,false );}
37
39
38
- public InlineModelResolver (){this (false );}
39
-
40
- public InlineModelResolver (boolean flattenComposedSchemas ) {
40
+ public InlineModelResolver (boolean flattenComposedSchemas , boolean camelCaseFlattenNaming ) {
41
41
this .flattenComposedSchemas = flattenComposedSchemas ;
42
+ this .camelCaseFlattenNaming = camelCaseFlattenNaming ;
42
43
}
43
44
44
45
public void flatten (OpenAPI openAPI ) {
@@ -53,7 +54,7 @@ public void flatten(OpenAPI openAPI) {
53
54
54
55
// operations
55
56
Map <String , PathItem > paths = openAPI .getPaths ();
56
- if (openAPI .getComponents ()== null ){
57
+ if (openAPI .getComponents ()== null ){
57
58
openAPI .setComponents (new Components ());
58
59
}
59
60
Map <String , Schema > models = openAPI .getComponents ().getSchemas ();
@@ -71,7 +72,7 @@ public void flatten(OpenAPI openAPI) {
71
72
for (String key : content .keySet ()) {
72
73
if (content .get (key ) != null ) {
73
74
MediaType mediaType = content .get (key );
74
- if (mediaType .getSchema () != null ) {
75
+ if (mediaType .getSchema () != null ) {
75
76
Schema model = mediaType .getSchema ();
76
77
if (model .getProperties () != null && model .getProperties ().size () > 0 ) {
77
78
flattenProperties (model .getProperties (), pathname );
@@ -258,24 +259,42 @@ public void flatten(OpenAPI openAPI) {
258
259
}
259
260
} else if (model instanceof ComposedSchema ) {
260
261
ComposedSchema composedSchema = (ComposedSchema ) model ;
262
+ String inlineModelName = "" ;
263
+
261
264
List <Schema > list = null ;
262
265
if (composedSchema .getAllOf () != null ) {
263
- list = composedSchema .getAllOf ();
266
+ list = composedSchema .getAllOf ();
267
+ inlineModelName = "AllOf" ;
264
268
}else if (composedSchema .getAnyOf () != null ) {
265
269
list = composedSchema .getAnyOf ();
270
+ inlineModelName = "AnyOf" ;
266
271
}else if (composedSchema .getOneOf () != null ) {
267
272
list = composedSchema .getOneOf ();
273
+ inlineModelName = "OneOf" ;
268
274
}
275
+
269
276
for (int i = 0 ; i <list .size ();i ++){
270
- if (list .get (i ).getProperties ()!= null ){
271
- flattenProperties (list .get (i ).getProperties (), modelName );
277
+ if (list .get (i ).get$ref () == null ){
278
+ Schema inline = list .get (i );
279
+ if (inline .getProperties ()!= null ){
280
+ flattenProperties (inline .getProperties (), modelName );
281
+ }
282
+ if (this .flattenComposedSchemas ) {
283
+ int position = i +1 ;
284
+ inlineModelName = resolveModelName (inline .getTitle (), modelName + inlineModelName + "_" + position );
285
+ list .set (i ,new Schema ().$ref (inlineModelName ));
286
+ addGenerated (inlineModelName , inline );
287
+ openAPI .getComponents ().addSchemas (inlineModelName , inline );
288
+ }
272
289
}
273
290
}
274
291
}
275
292
}
276
293
}
277
294
}
278
295
296
+
297
+
279
298
/**
280
299
* This function fix models that are string (mostly enum). Before this fix, the example
281
300
* would look something like that in the doc: "\"example from def\""
@@ -317,7 +336,18 @@ public void addGenerated(String name, Schema model) {
317
336
public String uniqueName (String key ) {
318
337
int count = 0 ;
319
338
boolean done = false ;
320
- key = key .replaceAll ("[^a-z_\\ .A-Z0-9 ]" , "" ); // FIXME: a parameter
339
+ if (camelCaseFlattenNaming ) {
340
+ String uniqueKey ;
341
+ String concatenated = "" ;
342
+ for (int i = 0 ; i < key .split ("-" ).length ; i ++) {
343
+ uniqueKey = key .split ("-" )[i ];
344
+ uniqueKey = uniqueKey .substring (0 , 1 ).toUpperCase () + uniqueKey .substring (1 );
345
+ concatenated = concatenated .concat (uniqueKey );
346
+ }
347
+ key = concatenated .replaceAll ("[^a-z_\\ .A-Z0-9 ]" , "" ); // FIXME: a parameter
348
+ }else {
349
+ key = key .replaceAll ("[^a-z_\\ .A-Z0-9 ]" , "" ); // FIXME: a parameter
350
+ }
321
351
// should not be
322
352
// assigned. Also declare
323
353
// the methods parameters
@@ -337,6 +367,7 @@ public String uniqueName(String key) {
337
367
return key ;
338
368
}
339
369
370
+
340
371
public void flattenProperties (Map <String , Schema > properties , String path ) {
341
372
if (properties == null ) {
342
373
return ;
@@ -377,7 +408,7 @@ public void flattenProperties(Map<String, Schema> properties, String path) {
377
408
addGenerated (modelName , innerModel );
378
409
openAPI .getComponents ().addSchemas (modelName , innerModel );
379
410
}
380
- }else if (inner instanceof ComposedSchema && this .flattenComposedSchemas ){
411
+ }else if (inner instanceof ComposedSchema && this .flattenComposedSchemas ){
381
412
ComposedSchema composedSchema = (ComposedSchema ) inner ;
382
413
String modelName = resolveModelName (inner .getTitle (), path + "_" + key );
383
414
List <Schema > list = null ;
@@ -389,7 +420,7 @@ public void flattenProperties(Map<String, Schema> properties, String path) {
389
420
list = composedSchema .getOneOf ();
390
421
}
391
422
for (int i = 0 ; i <list .size ();i ++){
392
- if (list .get (i ).getProperties ()!= null ){
423
+ if (list .get (i ).getProperties ()!= null ){
393
424
flattenProperties (list .get (i ).getProperties (), modelName );
394
425
}
395
426
}
@@ -546,7 +577,7 @@ public Schema makeRefProperty(String ref, Schema property) {
546
577
* @param target target property
547
578
*/
548
579
public void copyVendorExtensions (Schema source , Schema target ) {
549
- if (source .getExtensions () != null ) {
580
+ if (source .getExtensions () != null ) {
550
581
Map <String , Object > vendorExtensions = source .getExtensions ();
551
582
for (String extName : vendorExtensions .keySet ()) {
552
583
target .addExtension (extName , vendorExtensions .get (extName ));
@@ -566,6 +597,5 @@ private boolean isObjectSchema(Schema schema) {
566
597
return schema instanceof ObjectSchema
567
598
|| "object" .equalsIgnoreCase (schema .getType ())
568
599
|| (schema .getType () == null && schema .getProperties () != null && !schema .getProperties ().isEmpty ());
569
-
570
600
}
571
601
}
0 commit comments