Skip to content

Commit 6c45df6

Browse files
authored
Merge branch '2.0' into branch_fix855
2 parents 0eac158 + f54f20c commit 6c45df6

File tree

41 files changed

+1169
-133
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1169
-133
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ You can include this library from Sonatype OSS for SNAPSHOTS, or Maven central f
9797
<dependency>
9898
<groupId>io.swagger.parser.v3</groupId>
9999
<artifactId>swagger-parser</artifactId>
100-
<version>2.0.4</version>
100+
<version>2.0.5-SNAPSHOT</version>
101101
</dependency>
102102

103103
```
@@ -108,7 +108,7 @@ or
108108
<dependency>
109109
<groupId>io.swagger.parser.v3</groupId>
110110
<artifactId>swagger-parser</artifactId>
111-
<version>2.0.4</version>
111+
<version>2.0.5-SNAPSHOT</version>
112112
</dependency>
113113

114114
```

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.4</version>
6+
<version>2.0.5-SNAPSHOT</version>
77
<relativePath>../..</relativePath>
88
</parent>
99
<modelVersion>4.0.0</modelVersion>

modules/swagger-parser-v2-converter/pom.xml

Lines changed: 3 additions & 3 deletions
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.4</version>
6+
<version>2.0.5-SNAPSHOT</version>
77
<relativePath>../..</relativePath>
88
</parent>
99
<modelVersion>4.0.0</modelVersion>
@@ -30,12 +30,12 @@
3030
<dependency>
3131
<groupId>io.swagger.parser.v3</groupId>
3232
<artifactId>swagger-parser-core</artifactId>
33-
<version>${parent.version}</version>
33+
<version>${project.parent.version}</version>
3434
</dependency>
3535
<dependency>
3636
<groupId>io.swagger.parser.v3</groupId>
3737
<artifactId>swagger-parser-v3</artifactId>
38-
<version>${parent.version}</version>
38+
<version>${project.parent.version}</version>
3939
</dependency>
4040
<dependency>
4141
<groupId>org.apache.commons</groupId>

modules/swagger-parser-v2-converter/src/main/java/io/swagger/v3/parser/converter/SwaggerConverter.java

Lines changed: 94 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ public SwaggerParseResult convert(SwaggerDeserializationResult parse) {
194194
if ("body".equals(v.getIn())) {
195195
components.addRequestBodies(k, convertParameterToRequestBody(v));
196196
} else if ("formData".equals(v.getIn())) {
197-
components.addRequestBodies(k, convertFormDataToRequestBody(v));
197+
// formData_ is added not to overwrite existing schemas
198+
components.addSchemas("formData_" + k, convertFormDataToSchema(v));
198199
} else {
199200
components.addParameters(k, convert(v));
200201
}
@@ -529,6 +530,16 @@ private boolean isRefABodyParam(io.swagger.models.parameters.Parameter param) {
529530
return false;
530531
}
531532

533+
private boolean isRefAFormParam(io.swagger.models.parameters.Parameter param) {
534+
if (param instanceof RefParameter) {
535+
RefParameter refParameter = (RefParameter) param;
536+
String simpleRef = refParameter.getSimpleRef();
537+
io.swagger.models.parameters.Parameter parameter = globalV2Parameters.get(simpleRef);
538+
return "formData".equals(parameter.getIn());
539+
}
540+
return false;
541+
}
542+
532543
public Operation convert(io.swagger.models.Operation v2Operation) {
533544
Operation operation = new Operation();
534545
if (StringUtils.isNotBlank(v2Operation.getDescription())) {
@@ -555,10 +566,11 @@ public Operation convert(io.swagger.models.Operation v2Operation) {
555566
String $ref = convert.get$ref();
556567
if ($ref != null && $ref.startsWith("#/components/requestBodies/") && isRefABodyParam(param)) {
557568
operation.setRequestBody(new RequestBody().$ref($ref));
569+
} else if ($ref != null && $ref.startsWith("#/components/schemas/") && isRefAFormParam(param)) {
570+
formParams.add(param);
558571
} else {
559572
operation.addParametersItem(convert);
560573
}
561-
//operation.addParametersItem(convert(param));
562574
}
563575
}
564576

@@ -611,8 +623,9 @@ private Map<String, Object> convert(Map<String, Object> vendorExtensions) {
611623
return vendorExtensions;
612624
}
613625

614-
private RequestBody convertFormDataToRequestBody(io.swagger.models.parameters.Parameter formParam) {
615-
return convertFormDataToRequestBody(Arrays.asList(formParam), null);
626+
private Schema convertFormDataToSchema(io.swagger.models.parameters.Parameter formParam) {
627+
SerializableParameter sp = (SerializableParameter) formParam;
628+
return convert(sp);
616629
}
617630

618631
private RequestBody convertFormDataToRequestBody(List<io.swagger.models.parameters.Parameter> formParams, List<String> consumes) {
@@ -621,73 +634,27 @@ private RequestBody convertFormDataToRequestBody(List<io.swagger.models.paramete
621634
Schema formSchema = new Schema();
622635

623636
for (io.swagger.models.parameters.Parameter param : formParams) {
624-
SerializableParameter sp = (SerializableParameter) param;
637+
SerializableParameter sp;
625638

626639
Schema schema;
627-
if ("file".equals(sp.getType())) {
628-
schema = new FileSchema();
629-
} else if ("array".equals(sp.getType())) {
630-
ArraySchema as = new ArraySchema();
631-
if (sp.getItems() != null) {
632-
as.setItems(convert(sp.getItems()));
633-
}
634-
schema = as;
640+
String name;
641+
if (param instanceof RefParameter) {
642+
RefParameter refParameter = (RefParameter) param;
643+
String simpleRef = refParameter.getSimpleRef();
644+
sp = (SerializableParameter) globalV2Parameters.get(simpleRef);
645+
name = components.getSchemas().get("formData_" + simpleRef).getName();
646+
schema = new Schema().$ref("#/components/schemas/formData_" + simpleRef);
635647
} else {
636-
schema = new Schema();
637-
schema.setType(sp.getType());
638-
schema.setFormat(sp.getFormat());
639-
}
640-
641-
schema.setDescription(sp.getDescription());
642-
schema.setReadOnly(sp.isReadOnly());
643-
schema.setEnum(sp.getEnum());
644-
645-
if (sp.getMaxItems() != null) {
646-
schema.setMaxItems(sp.getMaxItems());
647-
}
648-
if (sp.getMinItems() != null) {
649-
schema.setMinItems(sp.getMinItems());
650-
}
651-
if (sp.isUniqueItems() != null) {
652-
schema.setUniqueItems(sp.isUniqueItems());
653-
}
654-
655-
schema.setMaximum(sp.getMaximum());
656-
schema.setExclusiveMaximum(sp.isExclusiveMaximum());
657-
schema.setMinimum(sp.getMinimum());
658-
schema.setExclusiveMinimum(sp.isExclusiveMinimum());
659-
schema.setMinLength(sp.getMinLength());
660-
schema.setMaxLength(sp.getMaxLength());
661-
662-
if (sp.getVendorExtensions() != null) {
663-
Object exampleExtension = sp.getVendorExtensions().get("x-example");
664-
if (exampleExtension != null) {
665-
schema.setExample(exampleExtension);
666-
}
667-
Object nullableExtension = sp.getVendorExtensions().get("x-nullable");
668-
if (nullableExtension != null) {
669-
schema.setNullable((Boolean) nullableExtension);
670-
}
671-
schema.setExtensions(convert(sp.getVendorExtensions()));
672-
}
673-
674-
if (sp.getMultipleOf() != null) {
675-
schema.setMultipleOf(new BigDecimal(sp.getMultipleOf().toString()));
676-
}
677-
678-
schema.setPattern(sp.getPattern());
679-
schema.setExtensions(convert(sp.getVendorExtensions()));
680-
681-
if (sp instanceof AbstractSerializableParameter) {
682-
AbstractSerializableParameter ap = (AbstractSerializableParameter) sp;
683-
schema.setDefault(ap.getDefault());
648+
sp = (SerializableParameter) param;
649+
schema = convert(sp);
650+
name = schema.getName();
684651
}
685652

686653
if (sp.getRequired()) {
687654
formSchema.addRequiredItem(sp.getName());
688655
}
689656

690-
formSchema.addProperties(param.getName(), schema);
657+
formSchema.addProperties(name, schema);
691658
}
692659

693660
List<String> mediaTypes = new ArrayList<>(globalConsumes);
@@ -709,6 +676,69 @@ private RequestBody convertFormDataToRequestBody(List<io.swagger.models.paramete
709676
return body;
710677
}
711678

679+
private Schema convert(SerializableParameter sp) {
680+
Schema schema;
681+
if ("file".equals(sp.getType())) {
682+
schema = new FileSchema();
683+
} else if ("array".equals(sp.getType())) {
684+
ArraySchema as = new ArraySchema();
685+
if (sp.getItems() != null) {
686+
as.setItems(convert(sp.getItems()));
687+
}
688+
schema = as;
689+
} else {
690+
schema = new Schema();
691+
schema.setType(sp.getType());
692+
schema.setFormat(sp.getFormat());
693+
}
694+
695+
schema.setDescription(sp.getDescription());
696+
schema.setReadOnly(sp.isReadOnly());
697+
schema.setEnum(sp.getEnum());
698+
699+
if (sp.getMaxItems() != null) {
700+
schema.setMaxItems(sp.getMaxItems());
701+
}
702+
if (sp.getMinItems() != null) {
703+
schema.setMinItems(sp.getMinItems());
704+
}
705+
if (sp.isUniqueItems() != null) {
706+
schema.setUniqueItems(sp.isUniqueItems());
707+
}
708+
709+
schema.setMaximum(sp.getMaximum());
710+
schema.setExclusiveMaximum(sp.isExclusiveMaximum());
711+
schema.setMinimum(sp.getMinimum());
712+
schema.setExclusiveMinimum(sp.isExclusiveMinimum());
713+
schema.setMinLength(sp.getMinLength());
714+
schema.setMaxLength(sp.getMaxLength());
715+
schema.setName(sp.getName());
716+
717+
if (sp.getVendorExtensions() != null) {
718+
Object exampleExtension = sp.getVendorExtensions().get("x-example");
719+
if (exampleExtension != null) {
720+
schema.setExample(exampleExtension);
721+
}
722+
Object nullableExtension = sp.getVendorExtensions().get("x-nullable");
723+
if (nullableExtension != null) {
724+
schema.setNullable((Boolean) nullableExtension);
725+
}
726+
schema.setExtensions(convert(sp.getVendorExtensions()));
727+
}
728+
729+
if (sp.getMultipleOf() != null) {
730+
schema.setMultipleOf(new BigDecimal(sp.getMultipleOf().toString()));
731+
}
732+
733+
schema.setPattern(sp.getPattern());
734+
735+
if (sp instanceof AbstractSerializableParameter) {
736+
AbstractSerializableParameter ap = (AbstractSerializableParameter) sp;
737+
schema.setDefault(ap.getDefault());
738+
}
739+
return schema;
740+
}
741+
712742
private RequestBody convertParameterToRequestBody(io.swagger.models.parameters.Parameter param) {
713743
return convertParameterToRequestBody(param, null);
714744
}
@@ -957,6 +987,8 @@ public Parameter convert(io.swagger.models.parameters.Parameter v2Parameter) {
957987
if (components.getRequestBodies() != null &&
958988
components.getRequestBodies().get(ref.getSimpleRef()) != null) {
959989
updatedRef += "requestBodies";
990+
} else if (components.getSchemas() != null && components.getSchemas().get("formData_" + ref.getSimpleRef()) != null) {
991+
updatedRef += "schemas";
960992
} else {
961993
updatedRef += "parameters";
962994
}

modules/swagger-parser-v2-converter/src/test/java/io/swagger/parser/test/V2ConverterTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,11 +325,12 @@ public void testIssue16() throws Exception {
325325
public void testIssue17() throws Exception {
326326
OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_17_JSON);
327327
Map<String, RequestBody> requestBodies = oas.getComponents().getRequestBodies();
328-
assertNotNull(requestBodies.get("formEmail").getContent().get("multipart/form-data"));
329-
assertNotNull(requestBodies.get("formPassword").getContent().get("multipart/form-data"));
328+
Map<String, Schema> schemas = oas.getComponents().getSchemas();
329+
assertNotNull(schemas.get("formData_formEmail"));
330+
assertNotNull(schemas.get("formData_formPassword"));
330331
assertNotNull(requestBodies.get("bodyParam").getContent().get("*/*"));
331-
assertEquals(oas.getPaths().get("/formPost").getPost().getParameters().get(0).get$ref(),
332-
REQUEST_BODY_FORMEMAIL);
332+
assertNull(oas.getPaths().get("/formPost").getPost().getParameters());
333+
assertNotNull(oas.getPaths().get("/formPost").getPost().getRequestBody());
333334
assertNotNull(oas.getPaths().get("/report/{userId}").getGet().getRequestBody().
334335
getContent().get("multipart/form-data").getSchema().getProperties().get("limitForm"));
335336
}

modules/swagger-parser-v3/pom.xml

Lines changed: 3 additions & 3 deletions
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.4</version>
6+
<version>2.0.5-SNAPSHOT</version>
77
<relativePath>../..</relativePath>
88
</parent>
99
<modelVersion>4.0.0</modelVersion>
@@ -26,7 +26,7 @@
2626
<dependency>
2727
<groupId>io.swagger.parser.v3</groupId>
2828
<artifactId>swagger-parser-core</artifactId>
29-
<version>${parent.version}</version>
29+
<version>${project.parent.version}</version>
3030
</dependency>
3131
<dependency>
3232
<groupId>org.jmockit</groupId>
@@ -80,6 +80,6 @@
8080
</dependency>
8181
</dependencies>
8282
<properties>
83-
<swagger-core-module-version>2.0.4</swagger-core-module-version>
83+
<swagger-core-module-version>2.0.5-SNAPSHOT</swagger-core-module-version>
8484
</properties>
8585
</project>

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,10 @@ else if(rootPath != null) {
159159
protected <T> void updateLocalRefs(String file, T result) {
160160
if(result instanceof ApiResponse) {
161161
ApiResponse response = (ApiResponse) result;
162-
for(String mediaType : response.getContent().keySet()) {
163-
updateLocalRefs(file, response.getContent().get(mediaType).getSchema());
162+
if (response.getContent() != null) {
163+
for (String mediaType : response.getContent().keySet()) {
164+
updateLocalRefs(file, response.getContent().get(mediaType).getSchema());
165+
}
164166
}
165167
}
166168
if(result instanceof Schema && ((Schema)(result)).get$ref() != null) {

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ComponentsProcessor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public void processComponents() {
5454
if (openApi.getComponents() == null){
5555
return;
5656
}
57+
5758
final Map<String, Schema> schemas = openApi.getComponents().getSchemas();
5859
final Map<String, ApiResponse> responses = openApi.getComponents().getResponses();
5960
final Map<String, RequestBody> requestBodies = openApi.getComponents().getRequestBodies();

0 commit comments

Comments
 (0)