Search in sources :

Example 11 with OpenAPIDeserializer

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

the class OpenAPIDeserializerTest method readSecurityRequirementsObject.

@Test(dataProvider = "data")
public void readSecurityRequirementsObject(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<SecurityRequirement> requirements = openAPI.getSecurity();
    Assert.assertNotNull(requirements);
    Assert.assertEquals(requirements.size(), 2);
    SecurityRequirement requirement = requirements.get(0);
    assertTrue(requirement.containsKey("api_key"));
    requirement = requirements.get(1);
    assertTrue(requirement.containsKey("tokenAuth"));
}
Also used : SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPI(io.swagger.v3.oas.models.OpenAPI) SecurityRequirement(io.swagger.v3.oas.models.security.SecurityRequirement) Test(org.testng.annotations.Test)

Example 12 with OpenAPIDeserializer

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

the class OpenAPIDeserializerTest method readComponentsObject.

@Test(dataProvider = "data")
public void readComponentsObject(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);
    Assert.assertEquals(openAPI.getOpenapi(), "3.0.1");
    final Components component = openAPI.getComponents();
    Assert.assertNotNull(component);
    Assert.assertNotNull(component.getCallbacks());
    Assert.assertEquals(component.getCallbacks().get("heartbeat").get("$request.query.heartbeat-url").getPost().getResponses().get("200").getDescription(), "Consumer acknowledged the callback");
    // System.out.println(component.getCallbacks().get("referenced"));
    Assert.assertEquals(component.getCallbacks().get("failed").get("$response.body#/failedUrl").getPost().getResponses().get("200").getDescription(), "Consumer acknowledged the callback failed");
    Assert.assertNotNull(component.getExamples());
    Assert.assertEquals(component.getExamples().get("cat").getSummary(), "An example of a cat");
    Assert.assertNotNull(component.getExamples().get("cat").getValue());
    Assert.assertNotNull(component.getHeaders());
    Assert.assertEquals(component.getHeaders().get("X-Rate-Limit-Limit").getDescription(), "The number of allowed requests in the current period");
    Assert.assertEquals(component.getHeaders().get("X-Rate-Limit-Limit").getSchema().getType(), "integer");
    Assert.assertNotNull(component.getLinks());
    Assert.assertEquals(component.getLinks().get("unsubscribe").getOperationId(), "cancelHookCallback");
    Assert.assertNotNull(component.getLinks().get("unsubscribe").getParameters());
    Assert.assertEquals(component.getLinks().get("unsubscribe").getExtensions().get("x-link"), "link extension");
    Assert.assertNotNull(component.getParameters());
    Assert.assertEquals(component.getParameters().get("skipParam").getName(), "skip");
    Assert.assertEquals(component.getParameters().get("skipParam").getIn(), "query");
    Assert.assertEquals(component.getParameters().get("skipParam").getDescription(), "number of items to skip");
    assertTrue(component.getParameters().get("skipParam").getRequired());
    Assert.assertEquals(component.getParameters().get("skipParam").getSchema().getType(), "integer");
    Assert.assertNotNull(component.getRequestBodies());
    Assert.assertEquals(component.getRequestBodies().get("requestBody1").getDescription(), "request body in components");
    Assert.assertEquals(component.getRequestBodies().get("requestBody1").getContent().get("application/json").getSchema().get$ref(), "#/components/schemas/Pet");
    Assert.assertEquals(component.getRequestBodies().get("requestBody1").getContent().get("application/xml").getSchema().get$ref(), "#/components/schemas/Pet");
    Assert.assertEquals(component.getRequestBodies().get("requestBody2").getContent().get("application/json").getSchema().getType().toString(), "array");
    Assert.assertNotNull(component.getRequestBodies().get("requestBody2").getContent().get("application/json").getSchema());
    Assert.assertNotNull(component.getResponses());
    Assert.assertEquals(component.getResponses().get("NotFound").getDescription(), "Entity not found.");
    Assert.assertEquals(component.getResponses().get("IllegalInput").getDescription(), "Illegal input for operation.");
    Assert.assertEquals(component.getResponses().get("GeneralError").getDescription(), "General Error");
    Assert.assertEquals(component.getResponses().get("GeneralError").getContent().get("application/json").getSchema().get$ref(), "#/components/schemas/ExtendedErrorModel");
    Assert.assertNotNull(component.getSchemas());
    Assert.assertEquals(component.getSchemas().get("Category").getType(), "object");
    Assert.assertEquals(component.getSchemas().get("ApiResponse").getRequired().get(0), "name");
    Assert.assertEquals(component.getSchemas().get("Order").getType(), "object");
    Assert.assertEquals(component.getSchemas().get("Order").getNot().getType(), "integer");
    assertTrue(component.getSchemas().get("Order").getAdditionalProperties() instanceof Schema);
    Schema additionalProperties = (Schema) component.getSchemas().get("Order").getAdditionalProperties();
    Assert.assertEquals(additionalProperties.getType(), "integer");
    Schema schema = (Schema) component.getSchemas().get("Order").getProperties().get("status");
    Map<String, Schema> properties = (Map<String, Schema>) component.getSchemas().get("Order").getProperties();
    Assert.assertNotNull(properties);
    Assert.assertEquals(properties.get("status").getType(), "string");
    Assert.assertEquals(properties.get("status").getDescription(), "Order Status");
    Assert.assertEquals(properties.get("status").getEnum().get(0), "placed");
    Assert.assertNotNull(component.getSecuritySchemes());
    Assert.assertEquals(component.getSecuritySchemes().get("petstore_auth").getType().toString(), "oauth2");
    Assert.assertEquals(component.getSecuritySchemes().get("petstore_auth").getFlows().getImplicit().getAuthorizationUrl(), "http://petstore.swagger.io/oauth/dialog");
    // TODO
    Assert.assertNotNull(component.getSecuritySchemes().get("petstore_auth").getFlows().getImplicit().getScopes());
    Assert.assertNotNull(component.getExtensions());
    assertTrue(component.getExtensions().containsKey("x-component"));
    Object object = component.getExtensions().get("x-component");
    assertTrue(object instanceof List);
    List elements = (List) object;
    Assert.assertEquals(elements.size(), 1);
    Map<String, Object> map = (Map) elements.get(0);
    Assert.assertEquals(map.get("url"), "http://component.swagger.io/v2/swagger.json");
    Assert.assertEquals(map.get("format"), "OAS");
    Assert.assertEquals(map.get("version"), "3.0");
    Map<String, Object> converter = (Map<String, Object>) map.get("converter");
    Assert.assertNotNull(converter);
    Assert.assertEquals(converter.get("url"), "https://github.com/mermade/oas3");
    Assert.assertEquals(converter.get("version"), "1.2.3");
    object = component.getExtensions().get("x-api-title");
    assertTrue(object instanceof String);
    Assert.assertEquals("pet store test api in components", object.toString());
}
Also used : Components(io.swagger.v3.oas.models.Components) 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) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 13 with OpenAPIDeserializer

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

the class OpenAPIDeserializerTest method readProducesTestEndpoint.

@Test(dataProvider = "data")
public void readProducesTestEndpoint(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 producesTestEndpoint = paths.get("/producesTest");
    Assert.assertNotNull(producesTestEndpoint.getGet());
    Assert.assertNotNull(producesTestEndpoint.getGet().getParameters());
    Assert.assertTrue(producesTestEndpoint.getGet().getParameters().isEmpty());
    Operation operation = producesTestEndpoint.getGet();
    ApiResponses responses = operation.getResponses();
    Assert.assertNotNull(responses);
    Assert.assertFalse(responses.isEmpty());
    ApiResponse response = responses.get("200");
    Assert.assertNotNull(response);
    Assert.assertEquals("it works", response.getDescription());
    Content content = response.getContent();
    Assert.assertNotNull(content);
    MediaType mediaType = content.get("application/json");
    Assert.assertNotNull(mediaType);
    Schema schema = mediaType.getSchema();
    Assert.assertNotNull(schema);
    Assert.assertTrue(schema instanceof ObjectSchema);
    ObjectSchema objectSchema = (ObjectSchema) schema;
    schema = objectSchema.getProperties().get("name");
    Assert.assertNotNull(schema);
    Assert.assertTrue(schema instanceof StringSchema);
}
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) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) Operation(io.swagger.v3.oas.models.Operation) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) PathItem(io.swagger.v3.oas.models.PathItem) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) Content(io.swagger.v3.oas.models.media.Content) MediaType(io.swagger.v3.oas.models.media.MediaType) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Paths(io.swagger.v3.oas.models.Paths) OpenAPI(io.swagger.v3.oas.models.OpenAPI) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) Test(org.testng.annotations.Test)

Example 14 with OpenAPIDeserializer

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

the class OpenAPIDeserializerTest method readTagObject.

@Test(dataProvider = "data")
public void readTagObject(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<Tag> Tag = openAPI.getTags();
    Assert.assertNotNull(Tag);
    Assert.assertNotNull(Tag.get(0));
    Assert.assertNotNull(Tag.get(0).getName());
    Assert.assertEquals(Tag.get(0).getName(), "pet");
    Assert.assertNotNull(Tag.get(0).getDescription());
    Assert.assertEquals(Tag.get(0).getDescription(), "Everything about your Pets");
    Assert.assertNotNull(Tag.get(0).getExternalDocs());
    Assert.assertNotNull(Tag.get(1));
    Assert.assertNotNull(Tag.get(1).getName());
    Assert.assertNotNull(Tag.get(1).getDescription());
    Assert.assertEquals(Tag.get(1).getName(), "store");
    Assert.assertEquals(Tag.get(1).getDescription(), "Access to Petstore orders");
}
Also used : SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) Tag(io.swagger.v3.oas.models.tags.Tag) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 15 with OpenAPIDeserializer

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

the class OpenAPIDeserializerTest method readExtensions.

@Test(dataProvider = "data")
public void readExtensions(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);
    Assert.assertNotNull(openAPI.getExtensions());
    assertTrue(openAPI.getExtensions().containsKey("x-origin"));
    Object object = openAPI.getExtensions().get("x-origin");
    assertTrue(object instanceof List);
    List elements = (List) object;
    Assert.assertEquals(elements.size(), 1);
    Map<String, Object> map = (Map) elements.get(0);
    Assert.assertEquals(map.get("url"), "http://petstore.swagger.io/v2/swagger.json");
    Assert.assertEquals(map.get("format"), "swagger");
    Assert.assertEquals(map.get("version"), "2.0");
    Map<String, Object> converter = (Map<String, Object>) map.get("converter");
    Assert.assertNotNull(converter);
    Assert.assertEquals(converter.get("url"), "https://github.com/mermade/swagger2openapi");
    Assert.assertEquals(converter.get("version"), "1.2.1");
    object = openAPI.getExtensions().get("x-api-title");
    assertTrue(object instanceof String);
    Assert.assertEquals("pet store test api", object.toString());
}
Also used : Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Map(java.util.Map) 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