Search in sources :

Example 1 with AuthorizationValue

use of io.swagger.models.auth.AuthorizationValue in project swagger-parser by swagger-api.

the class SwaggerParser method readWithInfo.

public SwaggerDeserializationResult readWithInfo(String swaggerAsString, boolean resolve) {
    if (swaggerAsString == null) {
        return new SwaggerDeserializationResult().message("empty or null swagger supplied");
    }
    try {
        JsonNode node;
        if (swaggerAsString.trim().startsWith("{")) {
            ObjectMapper mapper = Json.mapper();
            node = mapper.readTree(swaggerAsString);
        } else {
            node = DeserializationUtils.readYamlTree(swaggerAsString);
        }
        SwaggerDeserializationResult result = new Swagger20Parser().readWithInfo(node);
        if (result != null) {
            if (resolve) {
                result.setSwagger(new SwaggerResolver(result.getSwagger(), new ArrayList<AuthorizationValue>(), null).resolve());
            }
        } else {
            result = new SwaggerDeserializationResult().message("Definition does not appear to be a valid Swagger format");
        }
        return result;
    } catch (Exception e) {
        return new SwaggerDeserializationResult().message("malformed or unreadable swagger supplied");
    }
}
Also used : AuthorizationValue(io.swagger.models.auth.AuthorizationValue) SwaggerDeserializationResult(io.swagger.parser.util.SwaggerDeserializationResult) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException)

Example 2 with AuthorizationValue

use of io.swagger.models.auth.AuthorizationValue in project swagger-parser by swagger-api.

the class NetworkReferenceTests method testIssue335.

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

        {
            remoteUrl.urlToString("http://server1/resources/swagger.json", new ArrayList<AuthorizationValue>());
            result = issue_335_json;
            remoteUrl.urlToString("http://server1/resources/Bar.json", new ArrayList<AuthorizationValue>());
            result = issue_335_bar_json;
        }
    };
    SwaggerDeserializationResult result = new SwaggerParser().readWithInfo("http://server1/resources/swagger.json", null, true);
    Swagger swagger = result.getSwagger();
    assertNotNull(swagger);
    assertNotNull(swagger.getDefinitions());
    assertNotNull(swagger.getDefinitions().get("BarData"));
    assertNotNull(swagger.getDefinitions().get("BarSettingsRequest"));
}
Also used : Expectations(mockit.Expectations) AuthorizationValue(io.swagger.models.auth.AuthorizationValue) SwaggerDeserializationResult(io.swagger.parser.util.SwaggerDeserializationResult) Swagger(io.swagger.models.Swagger) Test(org.testng.annotations.Test)

Example 3 with AuthorizationValue

use of io.swagger.models.auth.AuthorizationValue in project swagger-parser by swagger-api.

the class NetworkReferenceTests method testPathReference.

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

        {
            remoteUrl.urlToString("http://petstore.swagger.io/v2/swagger.json", new ArrayList<AuthorizationValue>());
            result = issue_407_json;
        }
    };
    SwaggerParser parser = new SwaggerParser();
    String yaml = "swagger: '2.0'\n" + "info:\n" + "  description: |\n" + "  version: 1.0.0\n" + "  title: testing\n" + "paths:\n" + "   /foo:\n" + "     $ref: 'http://petstore.swagger.io/v2/swagger.json#/paths/~1pet'\n" + "   /bar:\n" + "     $ref: 'http://petstore.swagger.io/v2/swagger.json#/paths/~1pet'\n" + "schemes:\n" + " - https\n" + " - http";
    final SwaggerDeserializationResult result = parser.readWithInfo(yaml);
    Assert.assertNotNull(result.getSwagger());
    assertTrue(result.getMessages().size() == 0);
    assertTrue(result.getSwagger().getDefinitions().size() == 3);
}
Also used : Expectations(mockit.Expectations) AuthorizationValue(io.swagger.models.auth.AuthorizationValue) SwaggerDeserializationResult(io.swagger.parser.util.SwaggerDeserializationResult) Test(org.testng.annotations.Test)

Example 4 with AuthorizationValue

use of io.swagger.models.auth.AuthorizationValue in project swagger-parser by swagger-api.

the class NetworkReferenceTests method testIssue411.

@Test
public void testIssue411() throws Exception {
    final List<AuthorizationValue> auths = new ArrayList<>();
    AuthorizationValue auth = new AuthorizationValue("Authorization", "OMG_SO_SEKR3T", "header");
    auths.add(auth);
    new Expectations() {

        {
            remoteUrl.urlToString("http://remote1/resources/swagger.json", auths);
            result = issue_411_server;
            remoteUrl.urlToString("http://remote2/resources/foo", auths);
            result = issue_411_components;
        }
    };
    SwaggerParser parser = new SwaggerParser();
    SwaggerDeserializationResult result = parser.readWithInfo("http://remote1/resources/swagger.json", auths, true);
    Json.prettyPrint(result);
    Swagger swagger = result.getSwagger();
    assertNotNull(swagger.getPath("/health"));
    Path health = swagger.getPath("/health");
    assertTrue(health.getGet().getParameters().size() == 0);
    Object responseRef = health.getGet().getResponses().get("200").getSchema();
    assertTrue(responseRef instanceof RefProperty);
    RefProperty refProperty = (RefProperty) responseRef;
    assertEquals(refProperty.get$ref(), "#/definitions/Success");
    assertNotNull(swagger.getDefinitions().get("Success"));
    Parameter param = swagger.getPath("/stuff").getGet().getParameters().get(0);
    assertEquals(param.getIn(), "query");
    assertEquals(param.getName(), "skip");
    Response response = swagger.getPath("/stuff").getGet().getResponses().get("200");
    assertNotNull(response);
    assertTrue(response.getSchema() instanceof StringProperty);
    Response error = swagger.getPath("/stuff").getGet().getResponses().get("400");
    assertNotNull(error);
    Property errorProp = error.getSchema();
    assertNotNull(errorProp);
    assertTrue(errorProp instanceof RefProperty);
    RefProperty errorProperty = (RefProperty) errorProp;
    assertEquals(errorProperty.get$ref(), "#/definitions/Error");
    assertTrue(swagger.getDefinitions().get("Error") instanceof ModelImpl);
}
Also used : Expectations(mockit.Expectations) Path(io.swagger.models.Path) SwaggerDeserializationResult(io.swagger.parser.util.SwaggerDeserializationResult) ArrayList(java.util.ArrayList) StringProperty(io.swagger.models.properties.StringProperty) RefProperty(io.swagger.models.properties.RefProperty) Response(io.swagger.models.Response) AuthorizationValue(io.swagger.models.auth.AuthorizationValue) Swagger(io.swagger.models.Swagger) Parameter(io.swagger.models.parameters.Parameter) ModelImpl(io.swagger.models.ModelImpl) StringProperty(io.swagger.models.properties.StringProperty) RefProperty(io.swagger.models.properties.RefProperty) Property(io.swagger.models.properties.Property) Test(org.testng.annotations.Test)

Example 5 with AuthorizationValue

use of io.swagger.models.auth.AuthorizationValue in project swagger-parser by swagger-api.

the class NetworkReferenceTests method testIssue323.

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

        {
            remoteUrl.urlToString("http://localhost:8080/nested-file-references/issue-323.yaml", new ArrayList<AuthorizationValue>());
            result = issue_323_yaml;
            remoteUrl.urlToString("http://localhost:8080/nested-file-references/eventsCase9.yaml", new ArrayList<AuthorizationValue>());
            result = issue_323_events_yaml;
            remoteUrl.urlToString("http://localhost:8080/nested-file-references/common/pagingWithFolderRef.yaml", new ArrayList<AuthorizationValue>());
            result = issue_323_paging_yaml;
            remoteUrl.urlToString("http://localhost:8080/nested-file-references/common/common2/bar.yaml", new ArrayList<AuthorizationValue>());
            result = issue_323_bar_yaml;
        }
    };
    SwaggerDeserializationResult result = new SwaggerParser().readWithInfo("http://localhost:8080/nested-file-references/issue-323.yaml", null, true);
    assertNotNull(result.getSwagger());
    Swagger swagger = result.getSwagger();
    assertNotNull(swagger.getPath("/events"));
    assertNotNull(swagger.getDefinitions().get("StatusResponse"));
    assertNotNull(swagger.getDefinitions().get("Paging"));
    assertNotNull(swagger.getDefinitions().get("Foobar"));
}
Also used : Expectations(mockit.Expectations) AuthorizationValue(io.swagger.models.auth.AuthorizationValue) SwaggerDeserializationResult(io.swagger.parser.util.SwaggerDeserializationResult) Swagger(io.swagger.models.Swagger) Test(org.testng.annotations.Test)

Aggregations

AuthorizationValue (io.swagger.models.auth.AuthorizationValue)18 Test (org.testng.annotations.Test)15 SwaggerDeserializationResult (io.swagger.parser.util.SwaggerDeserializationResult)10 Expectations (mockit.Expectations)8 Swagger (io.swagger.models.Swagger)6 SwaggerParser (io.swagger.parser.SwaggerParser)2 ArrayList (java.util.ArrayList)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ModelImpl (io.swagger.models.ModelImpl)1 Path (io.swagger.models.Path)1 Response (io.swagger.models.Response)1 Parameter (io.swagger.models.parameters.Parameter)1 Property (io.swagger.models.properties.Property)1 RefProperty (io.swagger.models.properties.RefProperty)1 StringProperty (io.swagger.models.properties.StringProperty)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1