Fixed static factory method fromUri so that it also excepts URIs without a scheme.

This commit is contained in:
Robert Winkler
2016-04-05 08:30:59 +02:00
parent ae8961d43c
commit 65522e4569
2 changed files with 28 additions and 15 deletions

View File

@@ -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));
}
}

View File

@@ -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);