Search in sources :

Example 31 with SwaggerDeserializationResult

use of io.swagger.parser.util.SwaggerDeserializationResult in project swagger-parser by swagger-api.

the class SwaggerConverter method readContents.

@Override
public SwaggerParseResult readContents(String swaggerAsString, List<AuthorizationValue> auth, ParseOptions options) {
    SwaggerDeserializationResult result = new SwaggerParser().readWithInfo(swaggerAsString, options == null ? true : options.isResolve());
    if (options != null) {
        if (options.isResolve()) {
            Swagger resolved = new SwaggerResolver(result.getSwagger(), convert(auth)).resolve();
            result.setSwagger(resolved);
        }
    }
    return readResult(result, auth, options);
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) SwaggerResolver(io.swagger.parser.SwaggerResolver) SwaggerDeserializationResult(io.swagger.parser.util.SwaggerDeserializationResult) Swagger(io.swagger.models.Swagger)

Example 32 with SwaggerDeserializationResult

use of io.swagger.parser.util.SwaggerDeserializationResult in project swagger-parser by swagger-api.

the class SwaggerConverter method readLocation.

@Override
public SwaggerParseResult readLocation(String url, List<AuthorizationValue> auths, ParseOptions options) {
    boolean resolve = false;
    if (options != null) {
        resolve = options.isResolve();
    }
    SwaggerDeserializationResult result = new SwaggerParser().readWithInfo(url, convert(auths), resolve);
    return readResult(result, auths, options);
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) SwaggerDeserializationResult(io.swagger.parser.util.SwaggerDeserializationResult)

Example 33 with SwaggerDeserializationResult

use of io.swagger.parser.util.SwaggerDeserializationResult in project swagger-parser by swagger-api.

the class LegacyConverterTest method testIssue43.

@Test
public void testIssue43() throws Exception {
    new Expectations() {

        {
            remoteUrl.urlToString("http://gateway.marvel.com/docs", new ArrayList<AuthorizationValue>());
            result = marvel_json;
            remoteUrl.urlToString("http://gateway.marvel.com/docs/public", new ArrayList<AuthorizationValue>());
            result = public_json;
        }
    };
    SwaggerParser parser = new SwaggerParser();
    SwaggerDeserializationResult result = parser.readWithInfo("http://gateway.marvel.com/docs", null, true);
    Assert.assertNotNull(result.getSwagger());
}
Also used : Expectations(mockit.Expectations) SwaggerParser(io.swagger.parser.SwaggerParser) AuthorizationValue(io.swagger.models.auth.AuthorizationValue) SwaggerDeserializationResult(io.swagger.parser.util.SwaggerDeserializationResult) Test(org.testng.annotations.Test)

Example 34 with SwaggerDeserializationResult

use of io.swagger.parser.util.SwaggerDeserializationResult in project swagger-parser by swagger-api.

the class LegacyConverterTest method testIssueFun.

@Test
public void testIssueFun() throws Exception {
    new Expectations() {

        {
            remoteUrl.urlToString("http://localhost:8080/api-docs", new ArrayList<AuthorizationValue>());
            result = resources_json;
            remoteUrl.urlToString("http://localhost:8080/api-docs/pet", new ArrayList<AuthorizationValue>());
            result = pet_json;
            remoteUrl.urlToString("http://localhost:8080/api-docs/store", new ArrayList<AuthorizationValue>());
            result = store_json;
            remoteUrl.urlToString("http://localhost:8080/api-docs/user", new ArrayList<AuthorizationValue>());
            result = user_json;
            remoteUrl.urlToString("http://localhost:8080/api-docs", null);
            result = resources_json;
            remoteUrl.urlToString("http://localhost:8080/api-docs/pet", null);
            result = pet_json;
            remoteUrl.urlToString("http://localhost:8080/api-docs/store", null);
            result = store_json;
            remoteUrl.urlToString("http://localhost:8080/api-docs/user", null);
            result = user_json;
        }
    };
    SwaggerParser parser = new SwaggerParser();
    SwaggerDeserializationResult result = parser.readWithInfo("http://localhost:8080/api-docs", null, true);
    Swagger swagger = parser.read("http://localhost:8080/api-docs");
    Assert.assertNotNull(swagger);
}
Also used : Expectations(mockit.Expectations) SwaggerParser(io.swagger.parser.SwaggerParser) AuthorizationValue(io.swagger.models.auth.AuthorizationValue) SwaggerDeserializationResult(io.swagger.parser.util.SwaggerDeserializationResult) Swagger(io.swagger.models.Swagger) Test(org.testng.annotations.Test)

Example 35 with SwaggerDeserializationResult

use of io.swagger.parser.util.SwaggerDeserializationResult in project swagger-parser by swagger-api.

the class Swagger20Parser method readWithInfo.

@Override
public SwaggerDeserializationResult readWithInfo(String location, List<AuthorizationValue> auths) {
    String data;
    try {
        location = location.replaceAll("\\\\", "/");
        if (location.toLowerCase().startsWith("http")) {
            data = RemoteUrl.urlToString(location, auths);
        } else {
            final String fileScheme = "file:";
            Path path;
            if (location.toLowerCase().startsWith(fileScheme)) {
                path = Paths.get(URI.create(location));
            } else {
                path = Paths.get(location);
            }
            if (Files.exists(path)) {
                data = FileUtils.readFileToString(path.toFile(), "UTF-8");
            } else {
                data = ClasspathHelper.loadFileFromClasspath(location);
            }
        }
        JsonNode rootNode;
        if (data.trim().startsWith("{")) {
            ObjectMapper mapper = Json.mapper();
            rootNode = mapper.readTree(data);
        } else {
            rootNode = DeserializationUtils.readYamlTree(data);
        }
        return readWithInfo(rootNode);
    } catch (SSLHandshakeException e) {
        SwaggerDeserializationResult output = new SwaggerDeserializationResult();
        output.message("unable to read location `" + location + "` due to a SSL configuration error.  " + "It is possible that the server SSL certificate is invalid, self-signed, or has an untrusted " + "Certificate Authority.");
        return output;
    } catch (Exception e) {
        SwaggerDeserializationResult output = new SwaggerDeserializationResult();
        output.message("unable to read location `" + location + "`");
        return output;
    }
}
Also used : Path(java.nio.file.Path) SwaggerDeserializationResult(io.swagger.parser.util.SwaggerDeserializationResult) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) IOException(java.io.IOException)

Aggregations

SwaggerDeserializationResult (io.swagger.parser.util.SwaggerDeserializationResult)56 Test (org.testng.annotations.Test)44 Swagger (io.swagger.models.Swagger)21 AuthorizationValue (io.swagger.models.auth.AuthorizationValue)10 SwaggerParser (io.swagger.parser.SwaggerParser)8 Expectations (mockit.Expectations)8 Parameter (io.swagger.models.parameters.Parameter)7 PathParameter (io.swagger.models.parameters.PathParameter)7 QueryParameter (io.swagger.models.parameters.QueryParameter)7 RefProperty (io.swagger.models.properties.RefProperty)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 ArrayModel (io.swagger.models.ArrayModel)5 Model (io.swagger.models.Model)5 RefModel (io.swagger.models.RefModel)5 BodyParameter (io.swagger.models.parameters.BodyParameter)5 ComposedModel (io.swagger.models.ComposedModel)4 Path (io.swagger.models.Path)4 ArrayProperty (io.swagger.models.properties.ArrayProperty)4 Property (io.swagger.models.properties.Property)4 IOException (java.io.IOException)4