Repository: swagger-api/swagger-parser
Pull request #794 (2018-08-10 05:09:31)

Fix for issue 745 (Issue in converting server url in RFC 3986 format from OpenAPI Spec 2 to Open API Spec 3)
Hi team,

This PR is to solve the issue #745 (OpenAPI Converter: Issue in converting server url in RFC 3986 format from OpenAPI Spec 2 to Open API Spec 3 (OAS 2 to 3 conversion))

Please review the PR and merge the same.

Thanks,
Mohammed


Patch:
diff --git a/modules/swagger-parser-v2-converter/src/main/java/io/swagger/v3/parser/converter/SwaggerConverter.java b/modules/swagger-parser-v2-converter/src/main/java/io/swagger/v3/parser/converter/SwaggerConverter.java
index 26c0b5b4..2449470e 100644
--- a/modules/swagger-parser-v2-converter/src/main/java/io/swagger/v3/parser/converter/SwaggerConverter.java
+++ b/modules/swagger-parser-v2-converter/src/main/java/io/swagger/v3/parser/converter/SwaggerConverter.java
@@ -414,6 +414,9 @@ public class SwaggerConverter implements SwaggerParserExtension {
                 servers.add(server);
             }
         } else {
+            if (!"/".equals(baseUrl)) {
+                baseUrl = "//" + baseUrl;
+            }
             Server server = new Server();
             server.setUrl(baseUrl);
             servers.add(server);


Test patch:
diff --git a/modules/swagger-parser-v2-converter/src/test/java/io/swagger/parser/test/V2ConverterTest.java b/modules/swagger-parser-v2-converter/src/test/java/io/swagger/parser/test/V2ConverterTest.java
index f6cfc58f..c0e779fb 100644
--- a/modules/swagger-parser-v2-converter/src/test/java/io/swagger/parser/test/V2ConverterTest.java
+++ b/modules/swagger-parser-v2-converter/src/test/java/io/swagger/parser/test/V2ConverterTest.java
@@ -79,6 +79,7 @@ public class V2ConverterTest {
     private static final String ISSUE_673_YAML = "issue-673.yaml";
     private static final String ISSUE_676_JSON = "issue-676.json";
     private static final String ISSUE_708_YAML = "issue-708.yaml";
+    private static final String ISSUE_745_YAML = "issue-745.yaml";
     private static final String ISSUE_755_YAML = "issue-755.yaml";
     private static final String ISSUE_740_YAML = "issue-740.yaml";
     private static final String ISSUE_756_JSON = "issue-756.json";
@@ -87,7 +88,6 @@ public class V2ConverterTest {
     private static final String ISSUE_765_YAML = "issue-765.yaml";
     private static final String ISSUE_768_JSON = "issue-786.json";
 
-
     private static final String API_BATCH_PATH = "/api/batch/";
     private static final String PETS_PATH = "/pets";
     private static final String PET_FIND_BY_STATUS_PATH = "/pet/findByStatus";
@@ -626,6 +626,12 @@ public class V2ConverterTest {
         assertEquals(schema.getPattern(), "^[0-9]+$");
     }
 
+    @Test(description = "Issue in converting server url in RFC 3986 format from OpenAPI Spec 2 to Open API Spec 3")
+    public void testIssue745() throws Exception {
+        OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_745_YAML);
+        assertTrue(oas.getServers().get(0).getUrl().startsWith("//"));
+    }
+  
     @Test(description = "OpenAPIParser.readLocation fails when fetching valid Swagger 2.0 resource with AuthorizationValues provided")
     public void testIssue785() {
         AuthorizationValue apiKey = new AuthorizationValue("api_key", "special-key", "header");
diff --git a/modules/swagger-parser-v2-converter/src/test/resources/issue-745.yaml b/modules/swagger-parser-v2-converter/src/test/resources/issue-745.yaml
new file mode 100644
index 00000000..a9d8d8b1
--- /dev/null
+++ b/modules/swagger-parser-v2-converter/src/test/resources/issue-745.yaml
@@ -0,0 +1,10 @@
+swagger: '2.0'
+info:
+  description: 'Test'
+  version: 1.0.0
+  title: OpenAPI Test
+  license:
+    name: Apache-2.0
+    url: 'http://www.apache.org/licenses/LICENSE-2.0.html'
+host: petstore.swagger.io
+basePath: /v2
\ No newline at end of file