Search in sources :

Example 1 with OpenAPIDeserializer

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

the class OpenAPIDeserializerTest method readRequestBodyObject.

@Test(dataProvider = "data")
public void readRequestBodyObject(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/findByStatus");
    Assert.assertNotNull(petByStatusEndpoint.getGet().getRequestBody());
    Assert.assertEquals(petByStatusEndpoint.getGet().getRequestBody().getDescription(), "pet store to add to the system");
    assertTrue(petByStatusEndpoint.getGet().getRequestBody().getRequired(), "true");
    Assert.assertNotNull(petByStatusEndpoint.getGet().getRequestBody().getContent().get("multipart/mixed"));
    Assert.assertEquals(petByStatusEndpoint.getGet().getRequestBody().getContent().get("multipart/mixed").getSchema().getType(), "object");
    Assert.assertNotNull(petByStatusEndpoint.getGet().getRequestBody().getContent().get("multipart/mixed").getSchema().getProperties());
    Assert.assertEquals(petByStatusEndpoint.getGet().getRequestBody().getContent().get("multipart/mixed").getEncoding().get("historyMetadata").getContentType(), "application/xml; charset=utf-8");
    Assert.assertNotNull(petByStatusEndpoint.getGet().getRequestBody().getContent().get("multipart/mixed").getEncoding().get("profileImage").getHeaders());
    Assert.assertNotNull(petByStatusEndpoint.getGet().getRequestBody().getContent().get("multipart/mixed").getEncoding().get("profileImage").getHeaders().get("X-Rate-Limit"));
}
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 2 with OpenAPIDeserializer

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

the class OpenAPIDeserializerTest method readEmptyServerObject.

@Test
public void readEmptyServerObject() throws Exception {
    final ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
    final JsonNode rootNode = mapper.readTree(Files.readAllBytes(java.nio.file.Paths.get(getClass().getResource("/oas2.yaml.template").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 3 with OpenAPIDeserializer

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

the class OpenAPIDeserializerTest method readSecuritySchemesObject.

@Test(dataProvider = "data")
public void readSecuritySchemesObject(JsonNode rootNode) throws Exception {
    final OpenAPIDeserializer deserializer = new OpenAPIDeserializer();
    final SwaggerParseResult result = deserializer.deserialize(rootNode);
    Assert.assertNotNull(result);
    List<String> messages = result.getMessages();
    assertTrue(!messages.contains("attribute components.securitySchemes'.petstore_auth'.name is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.petstore_auth'.in is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.petstore_auth'.scheme is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.petstore_auth'.openIdConnectUrl is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.petstore_auth'.tokenUrl is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.petstore_auth_password'.authorizationUrl is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.petstore_auth_clientCredentials'.authorizationUrl is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.api_key'.scheme is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.api_key'.flows is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.api_key'.openIdConnectUrl is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.http'.name is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.http'.in is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.http'.flows is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.http'.openIdConnectUrl is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.openID'.name is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.openID'.in is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.openID'.scheme is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.openID'.flows is missing"));
    final OpenAPI openAPI = result.getOpenAPI();
    Assert.assertNotNull(openAPI);
    final Map<String, SecurityScheme> securitySchemes = openAPI.getComponents().getSecuritySchemes();
    Assert.assertNotNull(securitySchemes);
    Assert.assertEquals(securitySchemes.size(), 10);
    SecurityScheme securityScheme = securitySchemes.get("reference");
    assertTrue(securityScheme.get$ref().equals("#/components/securitySchemes/api_key"));
    securityScheme = securitySchemes.get("remote_reference");
    assertTrue(securityScheme.get$ref().equals("http://localhost:${dynamicPort}/remote/security#/petstore_remote"));
    securityScheme = securitySchemes.get("petstore_auth");
    assertTrue(securityScheme.getType() == SecurityScheme.Type.OAUTH2);
    securityScheme = securitySchemes.get("petstore_auth_password");
    assertTrue(securityScheme.getType() == SecurityScheme.Type.OAUTH2);
    securityScheme = securitySchemes.get("petstore_auth_clientCredentials");
    assertTrue(securityScheme.getType() == SecurityScheme.Type.OAUTH2);
    securityScheme = securitySchemes.get("petstore_auth_authorizationCode");
    assertTrue(securityScheme.getType() == SecurityScheme.Type.OAUTH2);
    securityScheme = securitySchemes.get("api_key");
    assertTrue(securityScheme.getType() == SecurityScheme.Type.APIKEY);
    assertTrue(securityScheme.getIn() == SecurityScheme.In.HEADER);
    securityScheme = securitySchemes.get("api_key_cookie");
    assertTrue(securityScheme.getType() == SecurityScheme.Type.APIKEY);
    assertTrue(securityScheme.getIn() == SecurityScheme.In.COOKIE);
    securityScheme = securitySchemes.get("http");
    assertTrue(securityScheme.getType() == SecurityScheme.Type.HTTP);
    securityScheme = securitySchemes.get("openID");
    assertTrue(securityScheme.getType() == SecurityScheme.Type.OPENIDCONNECT);
}
Also used : SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPI(io.swagger.v3.oas.models.OpenAPI) SecurityScheme(io.swagger.v3.oas.models.security.SecurityScheme) Test(org.testng.annotations.Test)

Example 4 with OpenAPIDeserializer

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

the class OpenAPIDeserializerTest method readInfoObject.

@Test(dataProvider = "data")
public void readInfoObject(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 Info info = openAPI.getInfo();
    Assert.assertNotNull(info);
    Assert.assertEquals(info.getTitle(), "Sample Pet Store App");
    Assert.assertEquals(info.getDescription(), "This is a sample server Petstore");
    Assert.assertEquals(info.getTermsOfService(), "http://swagger.io/terms/");
    Assert.assertNotNull(info.getExtensions().get("x-info"));
    Assert.assertEquals(info.getExtensions().get("x-info").toString(), "info extension");
    final Contact contact = info.getContact();
    Assert.assertNotNull(contact);
    Assert.assertEquals(contact.getName(), "API Support");
    Assert.assertEquals(contact.getUrl(), "http://www.example.com/support");
    Assert.assertEquals(contact.getEmail(), "support@example.com");
    Assert.assertNotNull(contact.getExtensions().get("x-contact"));
    Assert.assertEquals(contact.getExtensions().get("x-contact").toString(), "contact extension");
    final License license = info.getLicense();
    Assert.assertNotNull(license);
    Assert.assertEquals(license.getName(), "Apache 2.0");
    Assert.assertEquals(license.getUrl(), "http://www.apache.org/licenses/LICENSE-2.0.html");
    Assert.assertNotNull(license.getExtensions());
    Assert.assertEquals(info.getVersion(), "1.0.1");
}
Also used : License(io.swagger.v3.oas.models.info.License) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) Info(io.swagger.v3.oas.models.info.Info) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Contact(io.swagger.v3.oas.models.info.Contact) Test(org.testng.annotations.Test)

Example 5 with OpenAPIDeserializer

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

the class OpenAPIDeserializerTest method readSchemaArray.

@Test(dataProvider = "data")
public void readSchemaArray(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/findByTags");
    Assert.assertNotNull(petByStatusEndpoint.getGet());
    Assert.assertNotNull(petByStatusEndpoint.getGet().getParameters());
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().size(), 1);
    Assert.assertNotNull(petByStatusEndpoint.getGet().getParameters().get(0).getSchema());
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getSchema().getType(), "array");
    Assert.assertEquals(((ArraySchema) (petByStatusEndpoint.getGet().getParameters().get(0).getSchema())).getItems().getType(), "string");
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getName(), "tags");
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getExplode(), Boolean.TRUE);
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getStyle(), StyleEnum.FORM);
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) ByteArraySchema(io.swagger.v3.oas.models.media.ByteArraySchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) 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