diff --git a/src/main/java/io/github/swagger2markup/Swagger2MarkupConverter.java b/src/main/java/io/github/swagger2markup/Swagger2MarkupConverter.java index 9b503a5f..9eef967a 100644 --- a/src/main/java/io/github/swagger2markup/Swagger2MarkupConverter.java +++ b/src/main/java/io/github/swagger2markup/Swagger2MarkupConverter.java @@ -22,6 +22,7 @@ import io.github.swagger2markup.internal.document.builder.OverviewDocumentBuilde import io.github.swagger2markup.internal.document.builder.PathsDocumentBuilder; import io.github.swagger2markup.internal.document.builder.SecurityDocumentBuilder; import io.github.swagger2markup.spi.*; +import io.github.swagger2markup.utils.URIUtils; import io.swagger.models.Swagger; import io.swagger.parser.SwaggerParser; import org.apache.commons.io.IOUtils; @@ -71,23 +72,20 @@ public class Swagger2MarkupConverter { * @return a Swagger2MarkupConverter */ public static Builder from(URI swaggerUri) { + Validate.notNull(swaggerUri, "swaggerUri must not be null"); String scheme = swaggerUri.getScheme(); - if(scheme != null){ - if(swaggerUri.getScheme().startsWith("http")){ - try { - return from(swaggerUri.normalize().toURL()); - } catch (MalformedURLException e) { - throw new RuntimeException("Failed to convert URI into URL", e); - } - }else if(swaggerUri.getScheme().startsWith("file")){ - return from(Paths.get(swaggerUri).normalize()); + if(scheme != null && swaggerUri.getScheme().startsWith("http")){ + try { + return from(swaggerUri.toURL()); } - else{ - return from(Paths.get(swaggerUri.getPath()).normalize().toAbsolutePath()); + catch (MalformedURLException e) { + throw new RuntimeException("Failed to convert URI to URL", e); } + } else if(scheme != null && swaggerUri.getScheme().startsWith("file")){ + return from(Paths.get(swaggerUri)); } - else{ - return from(Paths.get(swaggerUri.getPath()).normalize().toAbsolutePath()); + else { + return from(URIUtils.convertUriWithoutSchemeToFileScheme(swaggerUri)); } } diff --git a/src/test/java/io/github/swagger2markup/GeneralConverterTest.java b/src/test/java/io/github/swagger2markup/GeneralConverterTest.java index 0b14d31e..ca16d1df 100644 --- a/src/test/java/io/github/swagger2markup/GeneralConverterTest.java +++ b/src/test/java/io/github/swagger2markup/GeneralConverterTest.java @@ -45,7 +45,7 @@ public class GeneralConverterTest { } @Test - public void testOutputFile() throws IOException, URISyntaxException { + public void testToFileWithoutExtension() throws IOException, URISyntaxException { //Given String swaggerJsonString = IOUtils.toString(getClass().getResourceAsStream("/yaml/swagger_petstore.yaml")); Path outputFile = Paths.get("build/test/asciidoc/outputFile.adoc"); @@ -81,7 +81,7 @@ public class GeneralConverterTest { } @Test - public void testFromFileURI() throws IOException, URISyntaxException { + public void testFromResourceURI() throws IOException, URISyntaxException { //Given Path outputDirectory = Paths.get("build/test/asciidoc/fileUri"); FileUtils.deleteQuietly(outputDirectory.toFile()); @@ -101,6 +101,21 @@ public class GeneralConverterTest { Path outputDirectory = Paths.get("build/test/asciidoc/pathUri"); FileUtils.deleteQuietly(outputDirectory.toFile()); + //When + Swagger2MarkupConverter.from(Paths.get("src/test/resources/yaml/swagger_petstore.yaml").toUri()).build() + .toFolder(outputDirectory); + + //Then + String[] files = outputDirectory.toFile().list(); + assertThat(files).hasSize(4).containsAll(expectedFiles); + } + + @Test + public void testFromStringURIWithoutScheme() throws IOException, URISyntaxException { + //Given + Path outputDirectory = Paths.get("build/test/asciidoc/pathUri"); + FileUtils.deleteQuietly(outputDirectory.toFile()); + //When Swagger2MarkupConverter.from(URI.create("src/test/resources/yaml/swagger_petstore.yaml")).build() .toFolder(outputDirectory);