Skip to content

Commit 3659275

Browse files
committed
fix for flatten inline composedSchema issue
1 parent b4c52dd commit 3659275

File tree

14 files changed

+284
-58
lines changed

14 files changed

+284
-58
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ You can include this library from Sonatype OSS for SNAPSHOTS, or Maven central f
103103
<dependency>
104104
<groupId>io.swagger.parser.v3</groupId>
105105
<artifactId>swagger-parser</artifactId>
106-
<version>2.0.18</version>
106+
<version>2.0.19</version>
107107
</dependency>
108108
```
109109

modules/swagger-parser-core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<parent>
44
<groupId>io.swagger.parser.v3</groupId>
55
<artifactId>swagger-parser-project</artifactId>
6-
<version>2.0.19-SNAPSHOT</version>
6+
<version>2.0.20-SNAPSHOT</version>
77
<relativePath>../..</relativePath>
88
</parent>
99
<modelVersion>4.0.0</modelVersion>

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-v2-converter/pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<parent>
44
<groupId>io.swagger.parser.v3</groupId>
55
<artifactId>swagger-parser-project</artifactId>
6-
<version>2.0.19-SNAPSHOT</version>
6+
<version>2.0.20-SNAPSHOT</version>
77
<relativePath>../..</relativePath>
88
</parent>
99
<modelVersion>4.0.0</modelVersion>
@@ -12,6 +12,11 @@
1212
<name>swagger-parser-v2-converter</name>
1313

1414
<dependencies>
15+
<dependency>
16+
<groupId>io.swagger</groupId>
17+
<artifactId>swagger-core</artifactId>
18+
<version>${swagger-core-v2-version}</version>
19+
</dependency>
1520
<dependency>
1621
<groupId>io.swagger</groupId>
1722
<artifactId>swagger-parser</artifactId>

modules/swagger-parser-v3/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<parent>
44
<groupId>io.swagger.parser.v3</groupId>
55
<artifactId>swagger-parser-project</artifactId>
6-
<version>2.0.19-SNAPSHOT</version>
6+
<version>2.0.20-SNAPSHOT</version>
77
<relativePath>../..</relativePath>
88
</parent>
99
<modelVersion>4.0.0</modelVersion>

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
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,13 @@ private void resolveHeaders(Map<String, Header> headers) {
217217
Map<String,Example> resolved = resolveExample(examples);
218218
resolvedValue.setExamples(resolved);
219219
}
220+
Schema schema = resolvedValue.getSchema();
221+
if(schema != null) {
222+
Schema resolvedSchema = resolveSchema( schema);
223+
if(resolvedSchema != null) {
224+
resolvedValue.setSchema( resolvedSchema);
225+
}
226+
}
220227
header.setValue(resolvedValue);
221228
}
222229
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,17 @@ public void testIssue85(@Injectable final List<AuthorizationValue> auths) {
492492
assertTrue(prop instanceof Schema);
493493
}
494494

495+
@Test
496+
public void testIssue1352(@Injectable final List<AuthorizationValue> auths) {
497+
ParseOptions options = new ParseOptions();
498+
options.setResolve(true);
499+
options.setResolveFully(true);
500+
501+
OpenAPI openAPI= new OpenAPIV3Parser().readLocation("issue-1352.json", auths, options).getOpenAPI();
502+
assertNull(openAPI.getPaths().get("/responses").getPatch().getResponses().get("200").getHeaders().get("x-my-secret-header").getSchema().get$ref());
503+
504+
}
505+
495506
@Test
496507
public void testIssue1157(@Injectable final List<AuthorizationValue> auths) {
497508
ParseOptions options = new ParseOptions();

0 commit comments

Comments
 (0)