Search in sources :

Example 16 with OpenAPIDeserializer

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

the class OpenAPIDeserializerTest method readMissingServerObject.

@Test
public void readMissingServerObject() throws Exception {
    final ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
    final JsonNode rootNode = mapper.readTree(Files.readAllBytes(java.nio.file.Paths.get(getClass().getResource("/oas.yaml").toURI())));
    final OpenAPIDeserializer deserializer = new OpenAPIDeserializer();
    final SwaggerParseResult result = deserializer.deserialize(rootNode);
    Assert.assertNotNull(result);
    final OpenAPI openAPI = result.getOpenAPI();
    Assert.assertNotNull(openAPI);
    assertEquals(openAPI.getServers().get(0).getUrl(), "/");
}
Also used : YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) JsonNode(com.fasterxml.jackson.databind.JsonNode) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPI(io.swagger.v3.oas.models.OpenAPI) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.testng.annotations.Test)

Example 17 with OpenAPIDeserializer

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

the class OpenAPIDeserializerTest method readContentObject.

@Test(dataProvider = "data")
public void readContentObject(JsonNode rootNode) throws Exception {
    final OpenAPIDeserializer deserializer = new OpenAPIDeserializer();
    final SwaggerParseResult result = deserializer.deserialize(rootNode);
    Assert.assertNotNull(result);
    final OpenAPI openAPI = result.getOpenAPI();
    Assert.assertNotNull(openAPI);
    final Paths paths = openAPI.getPaths();
    Assert.assertNotNull(paths);
    PathItem petByStatusEndpoint = paths.get("/pet/findByStatusContent");
    Assert.assertNotNull(petByStatusEndpoint.getGet());
    Assert.assertNotNull(petByStatusEndpoint.getGet().getParameters());
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().size(), 3);
    Assert.assertNotNull(petByStatusEndpoint.getGet().getParameters().get(0).getContent());
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getContent().size(), 1);
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getContent().get("application/json").getSchema().getType(), "array");
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getContent().get("application/json").getExample(), null);
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getContent().get("application/json").getExamples().get("list").getSummary(), "List of Names");
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getContent().get("application/json").getSchema().getType(), "array");
    Assert.assertNotNull(petByStatusEndpoint.getGet().getParameters().get(1).getContent());
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(1).getContent().size(), 1);
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(1).getContent().get("application/xml").getExamples().get("list").getSummary(), "List of names");
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(1).getContent().get("application/xml").getExamples().get("list").getValue(), "<Users><User name='Bob'/><User name='Diane'/><User name='Mary'/><User name='Bill'/></Users>");
    Assert.assertNotNull(petByStatusEndpoint.getGet().getParameters().get(1).getContent().get("application/xml").getExamples().get("empty").getSummary());
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(1).getContent().get("application/xml").getExamples().get("empty").getSummary(), "Empty list");
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(1).getContent().get("application/xml").getExamples().get("empty").getValue(), "<Users/>");
    Assert.assertNotNull(petByStatusEndpoint.getGet().getParameters().get(2).getContent());
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(2).getContent().size(), 1);
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(2).getContent().get("text/plain").getExamples().get("list").getSummary(), "List of names");
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(2).getContent().get("text/plain").getExamples().get("list").getValue(), "Bob,Diane,Mary,Bill");
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(2).getContent().get("text/plain").getExamples().get("empty").getSummary(), "Empty");
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(2).getContent().get("text/plain").getExamples().get("empty").getValue(), "");
    PathItem petEndpoint = paths.get("/pet");
    Assert.assertNotNull(petEndpoint.getPut());
    Assert.assertNotNull(petEndpoint.getPut().getResponses().get("400").getContent().get("application/json"));
    Assert.assertEquals(petEndpoint.getPut().getResponses().get("400").getContent().size(), 1);
    Assert.assertEquals(petEndpoint.getPut().getResponses().get("400").getContent().get("application/json").getSchema().getType(), "array");
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) Paths(io.swagger.v3.oas.models.Paths) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 18 with OpenAPIDeserializer

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

the class OpenAPIDeserializerTest method readServerObject.

@Test(dataProvider = "data")
public void readServerObject(JsonNode rootNode) throws Exception {
    final OpenAPIDeserializer deserializer = new OpenAPIDeserializer();
    final SwaggerParseResult result = deserializer.deserialize(rootNode);
    Assert.assertNotNull(result);
    final OpenAPI openAPI = result.getOpenAPI();
    Assert.assertNotNull(openAPI);
    final List<Server> server = openAPI.getServers();
    Assert.assertNotNull(server);
    Assert.assertNotNull(server.get(0));
    Assert.assertNotNull(server.get(0).getUrl());
    Assert.assertEquals(server.get(0).getUrl(), "http://petstore.swagger.io/v2");
    Assert.assertNotNull(server.get(1));
    Assert.assertNotNull(server.get(1).getUrl());
    Assert.assertNotNull(server.get(1).getDescription());
    Assert.assertEquals(server.get(1).getUrl(), "https://development.gigantic-server.com/v1");
    Assert.assertEquals(server.get(1).getDescription(), "Development server");
    Assert.assertNotNull(server.get(2));
    Assert.assertNotNull(server.get(2).getVariables());
    Assert.assertNotNull(server.get(2).getVariables().values());
    Assert.assertNotNull(server.get(2).getVariables().get("username"));
    Assert.assertEquals(server.get(2).getVariables().get("username").getDefault(), "demo");
    Assert.assertEquals(server.get(2).getVariables().get("username").getDescription(), "this value is assigned by the service provider, in this example `gigantic-server.com`");
    Assert.assertNotNull(server.get(2).getVariables().get("port").getEnum());
    Assert.assertEquals(server.get(2).getVariables().get("port").getEnum().get(0), "8443");
    Assert.assertEquals(server.get(2).getVariables().get("port").getEnum().get(1), "443");
    Assert.assertEquals(server.get(2).getVariables().get("port").getDefault(), "8443");
    Assert.assertNotNull(server.get(2).getVariables().get("port"));
    Assert.assertNotNull(server.get(2).getVariables().get("basePath"));
    Assert.assertNotNull(server.get(2).getExtensions().get("x-server"));
    Assert.assertEquals(server.get(2).getExtensions().get("x-server").toString(), "server extension");
    Assert.assertEquals(server.get(2).getVariables().get("basePath").getDescription(), "testing overwriting");
    Assert.assertEquals(server.get(2).getVariables().get("basePath").getDefault(), "v2");
}
Also used : Server(io.swagger.v3.oas.models.servers.Server) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 19 with OpenAPIDeserializer

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

the class OpenAPIDeserializerTest method readOAS.

@Test
public void readOAS() throws /*JsonNode rootNode*/
Exception {
    final ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
    final JsonNode rootNode = mapper.readTree(Files.readAllBytes(java.nio.file.Paths.get(getClass().getResource("/oas4.yaml").toURI())));
    final OpenAPIDeserializer deserializer = new OpenAPIDeserializer();
    final SwaggerParseResult result = deserializer.deserialize(rootNode);
    Assert.assertNotNull(result);
    final OpenAPI openAPI = result.getOpenAPI();
    Assert.assertNotNull(openAPI);
    final Paths paths = openAPI.getPaths();
    Assert.assertNotNull(paths);
    Assert.assertNotNull(paths);
    Assert.assertEquals(paths.size(), 114);
    PathItem stripe = paths.get("/v1/3d_secure");
    Assert.assertNotNull(stripe);
    Assert.assertNotNull(stripe.getPost());
    Assert.assertEquals(stripe.getPost().getDescription(), "");
    Assert.assertEquals(stripe.getPost().getOperationId(), "Create3DSecure");
    Assert.assertNotNull(stripe.getPost().getParameters());
    ApiResponses responses = stripe.getPost().getResponses();
    Assert.assertNotNull(responses);
    assertTrue(responses.containsKey("200"));
    ApiResponse response = responses.get("200");
    Assert.assertEquals(response.getDescription(), "Successful response.");
    Assert.assertEquals(response.getContent().get("application/json").getSchema().get$ref(), "#/components/schemas/three_d_secure");
    PathItem stripeGet = paths.get("/v1/account/external_accounts");
    Assert.assertNotNull(stripeGet);
    Assert.assertNotNull(stripeGet.getGet());
    Assert.assertEquals(stripeGet.getGet().getDescription(), "");
    Assert.assertEquals(stripeGet.getGet().getOperationId(), "AllAccountExternalAccounts");
    Assert.assertNotNull(stripeGet.getGet().getParameters());
    ApiResponses responsesGet = stripeGet.getGet().getResponses();
    Assert.assertNotNull(responsesGet);
    assertTrue(responsesGet.containsKey("200"));
    ApiResponse responseGet = responsesGet.get("200");
    Assert.assertEquals(responseGet.getDescription(), "Successful response.");
    Map<String, Schema> properties = (Map<String, Schema>) responseGet.getContent().get("application/json").getSchema().getProperties();
    Assert.assertNotNull(properties);
    Assert.assertNull(properties.get("data").getType());
    Assert.assertEquals(properties.get("has_more").getDescription(), "True if this list has another page of items after this one that can be fetched.");
    assertTrue(properties.get("data") instanceof ComposedSchema);
    ComposedSchema data = (ComposedSchema) properties.get("data");
    assertTrue(data.getOneOf().get(0) instanceof ArraySchema);
    ArraySchema items = (ArraySchema) data.getOneOf().get(0);
    Assert.assertEquals(items.getItems().get$ref(), "#/components/schemas/bank_account");
}
Also used : DateSchema(io.swagger.v3.oas.models.media.DateSchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) DateTimeSchema(io.swagger.v3.oas.models.media.DateTimeSchema) ByteArraySchema(io.swagger.v3.oas.models.media.ByteArraySchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) MapSchema(io.swagger.v3.oas.models.media.MapSchema) BinarySchema(io.swagger.v3.oas.models.media.BinarySchema) JsonNode(com.fasterxml.jackson.databind.JsonNode) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) PathItem(io.swagger.v3.oas.models.PathItem) ByteArraySchema(io.swagger.v3.oas.models.media.ByteArraySchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) Paths(io.swagger.v3.oas.models.Paths) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) Test(org.testng.annotations.Test)

Example 20 with OpenAPIDeserializer

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

the class OpenAPIDeserializerTest method readExamplesObject.

@Test(dataProvider = "data")
public void readExamplesObject(JsonNode rootNode) throws Exception {
    final OpenAPIDeserializer deserializer = new OpenAPIDeserializer();
    final SwaggerParseResult result = deserializer.deserialize(rootNode);
    Assert.assertNotNull(result);
    final OpenAPI openAPI = result.getOpenAPI();
    Assert.assertNotNull(openAPI);
    final Paths paths = openAPI.getPaths();
    Assert.assertNotNull(paths);
    Assert.assertEquals(paths.size(), 19);
    // parameters operation get
    PathItem petByStatusEndpoint = paths.get("/pet/findByStatus");
    Assert.assertNotNull(petByStatusEndpoint.getGet());
    Assert.assertNotNull(petByStatusEndpoint.getGet().getParameters());
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().size(), 1);
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getName(), "status");
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getIn(), "query");
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) Paths(io.swagger.v3.oas.models.Paths) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Aggregations

SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)23 OpenAPI (io.swagger.v3.oas.models.OpenAPI)21 Test (org.testng.annotations.Test)21 PathItem (io.swagger.v3.oas.models.PathItem)9 Paths (io.swagger.v3.oas.models.Paths)8 JsonNode (com.fasterxml.jackson.databind.JsonNode)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 YAMLFactory (com.fasterxml.jackson.dataformat.yaml.YAMLFactory)6 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)6 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)5 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)5 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)5 Schema (io.swagger.v3.oas.models.media.Schema)5 StringSchema (io.swagger.v3.oas.models.media.StringSchema)5 ByteArraySchema (io.swagger.v3.oas.models.media.ByteArraySchema)4 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)4 OpenAPIDeserializer (io.swagger.v3.parser.util.OpenAPIDeserializer)4 BinarySchema (io.swagger.v3.oas.models.media.BinarySchema)3 DateSchema (io.swagger.v3.oas.models.media.DateSchema)3 DateTimeSchema (io.swagger.v3.oas.models.media.DateTimeSchema)3