Search in sources :

Example 6 with OpenAPIDeserializer

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

the class OpenAPIDeserializerTest method readEmptySecurityRequirement.

@Test
public void readEmptySecurityRequirement() 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);
    SecurityRequirement securityRequirement = openAPI.getSecurity().get(0);
    assertTrue(securityRequirement.isEmpty());
    assertEquals(openAPI.getSecurity().size(), 4);
}
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) SecurityRequirement(io.swagger.v3.oas.models.security.SecurityRequirement) Test(org.testng.annotations.Test)

Example 7 with OpenAPIDeserializer

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

the class ResolverCache method deserializeFragment.

private <T> T deserializeFragment(JsonNode node, Class<T> expectedType, String file, String definitionPath) {
    OpenAPIDeserializer deserializer = new OpenAPIDeserializer();
    OpenAPIDeserializer.ParseResult parseResult = new OpenAPIDeserializer.ParseResult();
    T result = null;
    if (expectedType.equals(Schema.class)) {
        result = (T) deserializer.getSchema((ObjectNode) node, definitionPath.replace("/", "."), parseResult);
    } else if (expectedType.equals(RequestBody.class)) {
        result = (T) deserializer.getRequestBody((ObjectNode) node, definitionPath.replace("/", "."), parseResult);
    } else if (expectedType.equals(ApiResponse.class)) {
        result = (T) deserializer.getResponse((ObjectNode) node, definitionPath.replace("/", "."), parseResult);
    } else if (expectedType.equals(Callback.class)) {
        result = (T) deserializer.getCallback((ObjectNode) node, definitionPath.replace("/", "."), parseResult);
    } else if (expectedType.equals(Example.class)) {
        result = (T) deserializer.getExample((ObjectNode) node, definitionPath.replace("/", "."), parseResult);
    } else if (expectedType.equals(Header.class)) {
        result = (T) deserializer.getHeader((ObjectNode) node, definitionPath.replace("/", "."), parseResult);
    } else if (expectedType.equals(Link.class)) {
        result = (T) deserializer.getLink((ObjectNode) node, definitionPath.replace("/", "."), parseResult);
    } else if (expectedType.equals(Parameter.class)) {
        result = (T) deserializer.getParameter((ObjectNode) node, definitionPath.replace("/", "."), parseResult);
    } else if (expectedType.equals(SecurityScheme.class)) {
        result = (T) deserializer.getSecurityScheme((ObjectNode) node, definitionPath.replace("/", "."), parseResult);
    } else if (expectedType.equals(PathItem.class)) {
        result = (T) deserializer.getPathItem((ObjectNode) node, definitionPath.replace("/", "."), parseResult);
    }
    parseResult.getMessages().forEach((m) -> {
        resolveValidationMessages.add(m + " (" + file + ")");
    });
    if (result != null) {
        return result;
    }
    // TODO ensure core deserialization exceptions get added to result messages resolveValidationMessages
    return DeserializationUtils.deserialize(node, file, expectedType);
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) OpenAPIDeserializer(io.swagger.v3.parser.util.OpenAPIDeserializer) Callback(io.swagger.v3.oas.models.callbacks.Callback) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Header(io.swagger.v3.oas.models.headers.Header) Parameter(io.swagger.v3.oas.models.parameters.Parameter) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody)

Example 8 with OpenAPIDeserializer

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

the class OpenAPIResolverTest method componentsResolver.

@Test
public void componentsResolver() throws Exception {
    final ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
    String pathFile = FileUtils.readFileToString(new File("src/test/resources/oas3.yaml.template"));
    pathFile = pathFile.replace("${dynamicPort}", String.valueOf(this.serverPort));
    final JsonNode rootNode = mapper.readTree(pathFile.getBytes());
    final OpenAPIDeserializer deserializer = new OpenAPIDeserializer();
    final SwaggerParseResult result = deserializer.deserialize(rootNode);
    Assert.assertNotNull(result);
    final OpenAPI openAPI = result.getOpenAPI();
    Assert.assertNotNull(openAPI);
    assertEquals(new OpenAPIResolver(openAPI, new ArrayList<>(), null).resolve(), openAPI);
    Map<String, Schema> schemas = openAPI.getComponents().getSchemas();
    // internal url schema
    Schema pet = schemas.get("Pet");
    Schema category = (Schema) pet.getProperties().get("category");
    assertEquals(category.get$ref(), "#/components/schemas/Category");
    // remote url schema
    Schema user = (Schema) pet.getProperties().get("user");
    assertEquals(user.get$ref(), "#/components/schemas/User");
    // ArraySchema items
    ArraySchema tagsProperty = (ArraySchema) pet.getProperties().get("tags");
    assertEquals(tagsProperty.getItems().get$ref(), "#/components/schemas/ExampleSchema");
    assertEquals(tagsProperty.getType(), "array");
    Assert.assertNotNull(openAPI.getComponents().getSchemas().get("ExampleSchema"));
    // Schema not
    assertEquals(schemas.get("OrderRef").getNot().get$ref(), "#/components/schemas/Category");
    // Schema additionalProperties
    assertTrue(schemas.get("OrderRef").getAdditionalProperties() instanceof Schema);
    Schema additionalProperties = (Schema) schemas.get("OrderRef").getAdditionalProperties();
    assertEquals(additionalProperties.get$ref(), "#/components/schemas/User");
    // AllOfSchema
    ComposedSchema extended = (ComposedSchema) schemas.get("ExtendedErrorModel");
    Schema root = (Schema) extended.getAllOf().get(0).getProperties().get("rootCause");
    assertEquals(root.get$ref(), "#/components/schemas/Category");
    Map<String, ApiResponse> responses = openAPI.getComponents().getResponses();
    // internal response headers
    ApiResponse illegalInput = responses.get("IllegalInput");
    assertEquals(illegalInput.getHeaders().get("X-Ref-Limit-Limit").get$ref(), "#/components/headers/X-Rate-Limit-Reset");
    // internal response links
    assertEquals(illegalInput.getLinks().get("address").get$ref(), "#/components/links/unsubscribe");
    // internal url response schema
    MediaType generalError = responses.get("GeneralError").getContent().get("application/json");
    assertEquals(generalError.getSchema().get$ref(), "#/components/schemas/ExtendedErrorModel");
    Map<String, RequestBody> requestBodies = openAPI.getComponents().getRequestBodies();
    // internal url requestBody schema
    RequestBody requestBody1 = requestBodies.get("requestBody1");
    MediaType xmlMedia = requestBody1.getContent().get("application/json");
    assertEquals(xmlMedia.getSchema().get$ref(), "#/components/schemas/Pet");
    // internal url requestBody ArraySchema
    RequestBody requestBody2 = requestBodies.get("requestBody2");
    MediaType jsonMedia = requestBody2.getContent().get("application/json");
    ArraySchema items = (ArraySchema) jsonMedia.getSchema();
    assertEquals(items.getItems().get$ref(), "#/components/schemas/User");
    // internal request body
    assertEquals("#/components/requestBodies/requestBody2", requestBodies.get("requestBody3").get$ref());
    // remote request body url
    assertEquals(requestBodies.get("reference").get$ref(), "#/components/requestBodies/remote_requestBody");
    Map<String, Parameter> parameters = openAPI.getComponents().getParameters();
    // remote url parameter
    assertEquals(parameters.get("remoteParameter").get$ref(), "#/components/parameters/parameter");
    // internal Schema Parameter
    assertEquals(parameters.get("newParam").getSchema().get$ref(), "#/components/schemas/Tag");
    // parameter examples
    assertEquals(parameters.get("contentParameter").getExamples().get("cat"), openAPI.getComponents().getExamples().get("cat"));
    // parameter content schema
    assertEquals(parameters.get("contentParameter").getContent().get("application/json").getSchema().get$ref(), "#/components/schemas/ExtendedErrorModel");
    // internal Schema header
    Map<String, Header> headers = openAPI.getComponents().getHeaders();
    // header remote schema ref
    assertEquals(headers.get("X-Rate-Limit-Remaining").getSchema().get$ref(), "#/components/schemas/User");
    // header examples
    assertEquals(headers.get("X-Rate-Limit-Reset").getExamples().get("headerExample").get$ref(), "#/components/examples/dog");
    // remote header ref
    assertEquals(headers.get("X-Ref-Limit-Limit").get$ref(), "#/components/headers/X-Rate-Limit-Reset");
    // header content
    assertEquals(headers.get("X-Rate-Limit-Reset").getContent().get("application/json").getSchema().get$ref(), "#/components/schemas/ExtendedErrorModel");
    Map<String, Example> examples = openAPI.getComponents().getExamples();
    // internal url example
    Example frogExample = examples.get("frog");
    assertEquals(frogExample.get$ref(), "#/components/examples/cat");
    // remote example url
    assertEquals(examples.get("referenceCat").get$ref(), "#/components/examples/example");
    // internal url securityScheme
    SecurityScheme scheme = openAPI.getComponents().getSecuritySchemes().get("reference");
    assertEquals(scheme.getType(), SecurityScheme.Type.APIKEY);
    SecurityScheme remoteScheme = openAPI.getComponents().getSecuritySchemes().get("remote_reference");
    assertEquals(remoteScheme.getType(), SecurityScheme.Type.OAUTH2);
    Map<String, Link> links = openAPI.getComponents().getLinks();
    // internal link
    assertEquals(openAPI.getComponents().getLinks().get("referenced").get$ref(), "#/components/links/unsubscribe");
    // remote ref link
    assertEquals(openAPI.getComponents().getLinks().get("subscribe").get$ref(), "#/components/links/link");
    Map<String, Callback> callbacks = openAPI.getComponents().getCallbacks();
    // internal callback reference
    assertEquals(callbacks.get("referenced").get$ref(), "#/components/callbacks/failed");
    // callback pathItem -> operation ->requestBody
    assertEquals(callbacks.get("heartbeat").get("$request.query.heartbeat-url").getPost().getRequestBody().get$ref(), "#/components/requestBodies/requestBody3");
    // remote callback ref
    assertEquals(callbacks.get("remoteCallback").get$ref(), "#/components/callbacks/callback");
}
Also used : OpenAPIDeserializer(io.swagger.v3.parser.util.OpenAPIDeserializer) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) 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) JsonNode(com.fasterxml.jackson.databind.JsonNode) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Example(io.swagger.v3.oas.models.examples.Example) YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) MediaType(io.swagger.v3.oas.models.media.MediaType) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) SecurityScheme(io.swagger.v3.oas.models.security.SecurityScheme) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) Callback(io.swagger.v3.oas.models.callbacks.Callback) Header(io.swagger.v3.oas.models.headers.Header) OpenAPIResolver(io.swagger.v3.parser.OpenAPIResolver) Parameter(io.swagger.v3.oas.models.parameters.Parameter) QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) PathParameter(io.swagger.v3.oas.models.parameters.PathParameter) File(java.io.File) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Link(io.swagger.v3.oas.models.links.Link) Test(org.testng.annotations.Test)

Example 9 with OpenAPIDeserializer

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

the class OpenAPIDeserializerTest method readPathsObject.

@Test(dataProvider = "data")
public void readPathsObject(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);
    PathItem petRef = paths.get("/pathItemRef");
    PathItem petEndpoint = paths.get("/pet");
    Assert.assertNotNull(petEndpoint);
    Assert.assertEquals(petEndpoint.getSummary(), "summary");
    Assert.assertEquals(petEndpoint.getDescription(), "description");
    Assert.assertNotNull(petEndpoint.getPost().getExternalDocs());
    Assert.assertEquals(petEndpoint.getPost().getExternalDocs().getUrl(), "http://swagger.io");
    Assert.assertEquals(petEndpoint.getPost().getExternalDocs().getDescription(), "Find out more");
    // Operation trace
    Assert.assertNotNull(petEndpoint.getTrace());
    Assert.assertNotNull(petEndpoint.getDescription());
    // Operation post
    Assert.assertNotNull(petEndpoint.getPost());
    Assert.assertNotNull(petEndpoint.getPost().getTags());
    Assert.assertEquals(petEndpoint.getPost().getTags().size(), 1);
    Assert.assertEquals(petEndpoint.getPost().getSummary(), "Add a new pet to the store");
    Assert.assertEquals(petEndpoint.getPost().getDescription(), "");
    Assert.assertEquals(petEndpoint.getPost().getOperationId(), "addPet");
    Assert.assertNotNull(petEndpoint.getServers());
    Assert.assertEquals(petEndpoint.getServers().size(), 1);
    Assert.assertNotNull(petEndpoint.getParameters());
    Assert.assertEquals(petEndpoint.getParameters().size(), 2);
    Assert.assertNotNull(petEndpoint.getPost().getParameters());
    Assert.assertEquals(petEndpoint.getPost().getSecurity().get(0).get("petstore_auth").get(0), "write:pets");
    Assert.assertEquals(petEndpoint.getPost().getSecurity().get(0).get("petstore_auth").get(1), "read:pets");
    ApiResponses responses = petEndpoint.getPost().getResponses();
    Assert.assertNotNull(responses);
    assertTrue(responses.containsKey("405"));
    ApiResponse response = responses.get("405");
    Assert.assertEquals(response.getDescription(), "Invalid input");
    Assert.assertEquals(response.getHeaders().get("X-Rate-Limit").getDescription(), "calls per hour allowed by the user");
    // parameters operation get
    PathItem petByStatusEndpoint = paths.get("/pet/findByStatus");
    Assert.assertNotNull(petByStatusEndpoint.getGet());
    Assert.assertNotNull(petByStatusEndpoint.getGet().getTags());
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().size(), 1);
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getIn(), "query");
    Assert.assertEquals(petByStatusEndpoint.getGet().getCallbacks().get("mainHook").get("$request.body#/url").getPost().getResponses().get("200").getDescription(), "webhook successfully processed operation");
}
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) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Test(org.testng.annotations.Test)

Example 10 with OpenAPIDeserializer

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

the class OpenAPIDeserializerTest method readExternalDocsObject.

@Test(dataProvider = "data")
public void readExternalDocsObject(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 ExternalDocumentation externalDocumentation = openAPI.getExternalDocs();
    Assert.assertNotNull(externalDocumentation);
    Assert.assertNotNull(externalDocumentation.getUrl());
    Assert.assertEquals(externalDocumentation.getUrl(), "http://swagger.io");
    Assert.assertNotNull(externalDocumentation.getDescription());
    Assert.assertEquals(externalDocumentation.getDescription(), "Find out more about Swagger");
}
Also used : ExternalDocumentation(io.swagger.v3.oas.models.ExternalDocumentation) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) 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