Skip to content

Commit ad8fcb9

Browse files
authored
Merge pull request #1344 from swagger-api/flattenComposedSchemaAtComponents
flatten inline composedSchema
2 parents 2292e45 + 5e81eff commit ad8fcb9

File tree

5 files changed

+170
-52
lines changed

5 files changed

+170
-52
lines changed

modules/swagger-parser-core/src/main/java/io/swagger/v3/parser/core/models/ParseOptions.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ public class ParseOptions {
66
private boolean resolveFully;
77
private boolean flatten;
88
private boolean flattenComposedSchemas;
9+
private boolean camelCaseFlattenNaming;
910
private boolean skipMatches;
1011

1112
public boolean isResolve() {
@@ -51,4 +52,11 @@ public boolean isFlattenComposedSchemas() {
5152
public void setFlattenComposedSchemas(boolean flattenComposedSchemas) {
5253
this.flattenComposedSchemas = flattenComposedSchemas;
5354
}
55+
public boolean isCamelCaseFlattenNaming() {
56+
return camelCaseFlattenNaming;
57+
}
58+
59+
public void setCamelCaseFlattenNaming(boolean camelCaseFlattenNaming) {
60+
this.camelCaseFlattenNaming = camelCaseFlattenNaming;
61+
}
5462
}

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/OpenAPIV3Parser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public SwaggerParseResult readLocation(String url, List<AuthorizationValue> auth
7070
result.setOpenAPI(resolver.resolve());
7171
new ResolverFully(options.isResolveCombinators()).resolveFully(result.getOpenAPI());
7272
} else if (options.isFlatten()) {
73-
InlineModelResolver inlineModelResolver = new InlineModelResolver(options.isFlattenComposedSchemas());
73+
InlineModelResolver inlineModelResolver = new InlineModelResolver(options.isFlattenComposedSchemas(), options.isCamelCaseFlattenNaming());
7474
inlineModelResolver.setSkipMatches(options.isSkipMatches());
7575
inlineModelResolver.flatten(result.getOpenAPI());
7676
}
@@ -207,7 +207,7 @@ public SwaggerParseResult readContents(String swaggerAsString, List<Authorizatio
207207
result.setOpenAPI(new OpenAPIResolver(result.getOpenAPI(), auth, null).resolve());
208208
new ResolverFully(options.isResolveCombinators()).resolveFully(result.getOpenAPI());
209209
} else if (options.isFlatten()) {
210-
InlineModelResolver inlineModelResolver = new InlineModelResolver(options.isFlattenComposedSchemas());
210+
InlineModelResolver inlineModelResolver = new InlineModelResolver(options.isFlattenComposedSchemas(), options.isCamelCaseFlattenNaming());
211211
inlineModelResolver.setSkipMatches(options.isSkipMatches());
212212
inlineModelResolver.flatten(result.getOpenAPI());
213213
}

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

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,13 @@ public class InlineModelResolver {
3333
Map<String, String> generatedSignature = new HashMap<>();
3434

3535
private boolean flattenComposedSchemas;
36+
private boolean camelCaseFlattenNaming;
3637

38+
public InlineModelResolver(){this(false,false);}
3739

38-
public InlineModelResolver(){this(false);}
39-
40-
public InlineModelResolver(boolean flattenComposedSchemas) {
40+
public InlineModelResolver(boolean flattenComposedSchemas, boolean camelCaseFlattenNaming) {
4141
this.flattenComposedSchemas = flattenComposedSchemas;
42+
this.camelCaseFlattenNaming = camelCaseFlattenNaming;
4243
}
4344

4445
public void flatten(OpenAPI openAPI) {
@@ -53,7 +54,7 @@ public void flatten(OpenAPI openAPI) {
5354

5455
// operations
5556
Map<String, PathItem> paths = openAPI.getPaths();
56-
if(openAPI.getComponents()== null){
57+
if (openAPI.getComponents()== null){
5758
openAPI.setComponents(new Components());
5859
}
5960
Map<String, Schema> models = openAPI.getComponents().getSchemas();
@@ -71,7 +72,7 @@ public void flatten(OpenAPI openAPI) {
7172
for(String key: content.keySet()) {
7273
if (content.get(key) != null) {
7374
MediaType mediaType = content.get(key);
74-
if(mediaType.getSchema() != null) {
75+
if (mediaType.getSchema() != null) {
7576
Schema model = mediaType.getSchema();
7677
if (model.getProperties() != null && model.getProperties().size() > 0) {
7778
flattenProperties(model.getProperties(), pathname);
@@ -258,24 +259,42 @@ public void flatten(OpenAPI openAPI) {
258259
}
259260
} else if (model instanceof ComposedSchema) {
260261
ComposedSchema composedSchema = (ComposedSchema) model;
262+
String inlineModelName = "";
263+
261264
List<Schema> list = null;
262265
if (composedSchema.getAllOf() != null) {
263-
list = composedSchema.getAllOf();
266+
list = composedSchema.getAllOf();
267+
inlineModelName = "AllOf";
264268
}else if (composedSchema.getAnyOf() != null) {
265269
list = composedSchema.getAnyOf();
270+
inlineModelName = "AnyOf";
266271
}else if (composedSchema.getOneOf() != null) {
267272
list = composedSchema.getOneOf();
273+
inlineModelName = "OneOf";
268274
}
275+
269276
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+
}
272289
}
273290
}
274291
}
275292
}
276293
}
277294
}
278295

296+
297+
279298
/**
280299
* This function fix models that are string (mostly enum). Before this fix, the example
281300
* would look something like that in the doc: "\"example from def\""
@@ -317,7 +336,18 @@ public void addGenerated(String name, Schema model) {
317336
public String uniqueName(String key) {
318337
int count = 0;
319338
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+
}
321351
// should not be
322352
// assigned. Also declare
323353
// the methods parameters
@@ -337,6 +367,7 @@ public String uniqueName(String key) {
337367
return key;
338368
}
339369

370+
340371
public void flattenProperties(Map<String, Schema> properties, String path) {
341372
if (properties == null) {
342373
return;
@@ -377,7 +408,7 @@ public void flattenProperties(Map<String, Schema> properties, String path) {
377408
addGenerated(modelName, innerModel);
378409
openAPI.getComponents().addSchemas(modelName, innerModel);
379410
}
380-
}else if(inner instanceof ComposedSchema && this.flattenComposedSchemas){
411+
}else if (inner instanceof ComposedSchema && this.flattenComposedSchemas){
381412
ComposedSchema composedSchema = (ComposedSchema) inner;
382413
String modelName = resolveModelName(inner.getTitle(), path + "_" + key);
383414
List<Schema> list = null;
@@ -389,7 +420,7 @@ public void flattenProperties(Map<String, Schema> properties, String path) {
389420
list = composedSchema.getOneOf();
390421
}
391422
for(int i= 0; i<list.size();i++){
392-
if(list.get(i).getProperties()!= null){
423+
if (list.get(i).getProperties()!= null){
393424
flattenProperties(list.get(i).getProperties(), modelName);
394425
}
395426
}
@@ -546,7 +577,7 @@ public Schema makeRefProperty(String ref, Schema property) {
546577
* @param target target property
547578
*/
548579
public void copyVendorExtensions(Schema source, Schema target) {
549-
if(source.getExtensions() != null) {
580+
if (source.getExtensions() != null) {
550581
Map<String, Object> vendorExtensions = source.getExtensions();
551582
for (String extName : vendorExtensions.keySet()) {
552583
target.addExtension(extName, vendorExtensions.get(extName));
@@ -566,6 +597,5 @@ private boolean isObjectSchema(Schema schema) {
566597
return schema instanceof ObjectSchema
567598
|| "object".equalsIgnoreCase(schema.getType())
568599
|| (schema.getType() == null && schema.getProperties() != null && !schema.getProperties().isEmpty());
569-
570600
}
571601
}

0 commit comments

Comments
 (0)