Search in sources :

Example 1 with RequestBody

use of io.swagger.v3.oas.annotations.parameters.RequestBody in project swagger-parser by swagger-api.

the class OpenAPIDeserializer method getOperation.

public Operation getOperation(ObjectNode obj, String location, ParseResult result) {
    if (obj == null) {
        return null;
    }
    Operation operation = new Operation();
    ArrayNode array = getArray("tags", obj, false, location, result);
    List<String> tags = getTagsStrings(array, String.format("%s.%s", location, "tags"), result);
    if (tags != null) {
        operation.setTags(tags);
    }
    String value = getString("summary", obj, false, location, result);
    if ((result.isAllowEmptyStrings() && value != null) || (!result.isAllowEmptyStrings() && !StringUtils.isBlank(value))) {
        operation.setSummary(value);
    }
    value = getString("description", obj, false, location, result);
    if ((result.isAllowEmptyStrings() && value != null) || (!result.isAllowEmptyStrings() && !StringUtils.isBlank(value))) {
        operation.setDescription(value);
    }
    ObjectNode externalDocs = getObject("externalDocs", obj, false, location, result);
    ExternalDocumentation docs = getExternalDocs(externalDocs, String.format("%s.%s", location, "externalDocs"), result);
    if (docs != null) {
        operation.setExternalDocs(docs);
    }
    value = getString("operationId", obj, false, location, result, operationIDs);
    if ((result.isAllowEmptyStrings() && value != null) || (!result.isAllowEmptyStrings() && !StringUtils.isBlank(value))) {
        operation.operationId(value);
    }
    ArrayNode parameters = getArray("parameters", obj, false, location, result);
    if (parameters != null) {
        operation.setParameters(getParameterList(parameters, String.format("%s.%s", location, "parameters"), result));
    }
    final ObjectNode requestObjectNode = getObject("requestBody", obj, false, location, result);
    if (requestObjectNode != null) {
        operation.setRequestBody(getRequestBody(requestObjectNode, String.format("%s.%s", location, "requestBody"), result));
    }
    ObjectNode responsesNode = getObject("responses", obj, true, location, result);
    ApiResponses responses = getResponses(responsesNode, String.format("%s.%s", location, "responses"), result, false);
    if (responses != null) {
        operation.setResponses(responses);
    }
    ObjectNode callbacksNode = getObject("callbacks", obj, false, location, result);
    Map<String, Callback> callbacks = getCallbacks(callbacksNode, String.format("%s.%s", location, "callbacks"), result, false);
    if (callbacks != null) {
        operation.setCallbacks(callbacks);
    }
    Boolean deprecated = getBoolean("deprecated", obj, false, location, result);
    if (deprecated != null) {
        operation.setDeprecated(deprecated);
    }
    array = getArray("servers", obj, false, location, result);
    if (array != null && array.size() > 0) {
        operation.setServers(getServersList(array, String.format("%s.%s", location, "servers"), result));
    }
    array = getArray("security", obj, false, location, result);
    if (array != null) {
        operation.setSecurity(getSecurityRequirementsList(array, String.format("%s.%s", location, "security"), result));
    }
    Map<String, Object> extensions = getExtensions(obj);
    if (extensions != null && extensions.size() > 0) {
        operation.setExtensions(extensions);
    }
    Set<String> keys = getKeys(obj);
    for (String key : keys) {
        if (!OPERATION_KEYS.contains(key) && !key.startsWith("x-")) {
            result.extra(location, key, obj.get(key));
        }
    }
    return operation;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Operation(io.swagger.v3.oas.models.Operation) ExternalDocumentation(io.swagger.v3.oas.models.ExternalDocumentation) Callback(io.swagger.v3.oas.models.callbacks.Callback) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses)

Example 2 with RequestBody

use of io.swagger.v3.oas.annotations.parameters.RequestBody in project swagger-parser by swagger-api.

the class OpenAPIDeserializer method getLink.

public Link getLink(ObjectNode linkNode, String location, ParseResult result) {
    if (linkNode == null) {
        return null;
    }
    Link link = new Link();
    JsonNode ref = linkNode.get("$ref");
    if (ref != null) {
        if (ref.getNodeType().equals(JsonNodeType.STRING)) {
            String mungedRef = mungedRef(ref.textValue());
            if (mungedRef != null) {
                link.set$ref(mungedRef);
            } else {
                link.set$ref(ref.textValue());
            }
            return link;
        } else {
            result.invalidType(location, "$ref", "string", linkNode);
            return null;
        }
    }
    String value = getString("operationRef", linkNode, false, location, result);
    if ((result.isAllowEmptyStrings() && value != null) || (!result.isAllowEmptyStrings() && !StringUtils.isBlank(value))) {
        link.setOperationRef(value);
    }
    value = getString("operationId", linkNode, false, location, result);
    if ((result.isAllowEmptyStrings() && value != null) || (!result.isAllowEmptyStrings() && !StringUtils.isBlank(value))) {
        link.setOperationId(value);
    }
    ObjectNode parametersObject = getObject("parameters", linkNode, false, location, result);
    if (parametersObject != null) {
        link.setParameters(getLinkParameters(parametersObject, location, result));
    }
    String requestBody = getString("requestBody", linkNode, false, location, result);
    if (result.isAllowEmptyStrings() && requestBody != null) {
        link.setRequestBody(requestBody);
    }
    ObjectNode headerObject = getObject("headers", linkNode, false, location, result);
    if (headerObject != null) {
        link.setHeaders(getHeaders(headerObject, location, result, false));
    }
    ObjectNode serverObject = getObject("server", linkNode, false, location, result);
    if (serverObject != null) {
        link.setServer(getServer(serverObject, location, result));
    }
    value = getString("description", linkNode, false, location, result);
    if ((result.isAllowEmptyStrings() && value != null) || (!result.isAllowEmptyStrings() && !StringUtils.isBlank(value))) {
        link.setDescription(value);
    }
    Map<String, Object> extensions = getExtensions(linkNode);
    if (extensions != null && extensions.size() > 0) {
        link.setExtensions(extensions);
    }
    Set<String> keys = getKeys(linkNode);
    for (String key : keys) {
        if (!LINK_KEYS.contains(key) && !key.startsWith("x-")) {
            result.extra(location, key, linkNode.get(key));
        }
    }
    return link;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) Link(io.swagger.v3.oas.models.links.Link)

Example 3 with RequestBody

use of io.swagger.v3.oas.annotations.parameters.RequestBody in project swagger-parser by swagger-api.

the class OpenAPIDeserializer method getRequestBody.

public RequestBody getRequestBody(ObjectNode node, String location, ParseResult result) {
    if (node == null) {
        return null;
    }
    final RequestBody body = new RequestBody();
    JsonNode ref = node.get("$ref");
    if (ref != null) {
        if (ref.getNodeType().equals(JsonNodeType.STRING)) {
            String mungedRef = mungedRef(ref.textValue());
            if (mungedRef != null) {
                body.set$ref(mungedRef);
            } else {
                body.set$ref(ref.textValue());
            }
            return body;
        } else {
            result.invalidType(location, "$ref", "string", node);
            return null;
        }
    }
    final String description = getString("description", node, false, location, result);
    if (result.isAllowEmptyStrings() && description != null) {
        body.setDescription(description);
    }
    final Boolean required = getBoolean("required", node, false, location, result);
    if (required != null) {
        body.setRequired(required);
    }
    final ObjectNode contentNode = getObject("content", node, true, location, result);
    Content content = getContent(contentNode, location + ".content", result);
    if (content != null && content.isEmpty()) {
        result.unsupported(location, "content with no media type", contentNode);
        result.invalid();
    } else {
        body.setContent(content);
    }
    Map<String, Object> extensions = getExtensions(node);
    if (extensions != null && extensions.size() > 0) {
        body.setExtensions(extensions);
    }
    Set<String> keys = getKeys(node);
    for (String key : keys) {
        if (!REQUEST_BODY_KEYS.contains(key) && !key.startsWith("x-")) {
            result.extra(location, key, node.get(key));
        }
    }
    return body;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Content(io.swagger.v3.oas.models.media.Content) JsonNode(com.fasterxml.jackson.databind.JsonNode) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody)

Example 4 with RequestBody

use of io.swagger.v3.oas.annotations.parameters.RequestBody in project swagger-parser by swagger-api.

the class FileReferenceTest method testIssue421.

@Test
public void testIssue421() {
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    SwaggerParseResult result = new OpenAPIV3Parser().readLocation("./src/test/resources/nested-file-references/issue-421.yaml", null, options);
    assertNotNull(result.getOpenAPI());
    OpenAPI swagger = result.getOpenAPI();
    assertNotNull(swagger.getPaths().get("/pet/{petId}"));
    assertNotNull(swagger.getPaths().get("/pet/{petId}").getGet());
    assertNotNull(swagger.getPaths().get("/pet/{petId}").getGet().getParameters());
    assertTrue(swagger.getPaths().get("/pet/{petId}").getGet().getParameters().size() == 1);
    assertTrue(swagger.getPaths().get("/pet/{petId}").getGet().getParameters().get(0).getName().equals("petId"));
    assertTrue(swagger.getComponents().getSchemas().get("Pet") instanceof Schema);
    assertTrue(swagger.getComponents().getSchemas().get("Pet").getProperties().size() == 6);
    assertNotNull(swagger.getPaths().get("/pet/{petId}").getPost());
    assertNotNull(swagger.getPaths().get("/pet/{petId}").getPost().getParameters());
    assertTrue(swagger.getPaths().get("/pet/{petId}").getPost().getParameters().size() == 1);
    assertTrue(swagger.getPaths().get("/pet/{petId}").getPost().getRequestBody() != null);
    assertTrue(swagger.getPaths().get("/pet/{petId}").getPost().getRequestBody().get$ref() != null);
    assertEquals(swagger.getPaths().get("/pet/{petId}").getPost().getRequestBody().get$ref(), "#/components/requestBodies/requestBody");
    assertTrue(swagger.getPaths().get("/pet/{petId}").getPost().getRequestBody().get$ref().equals("#/components/requestBodies/requestBody"));
    assertNotNull(swagger.getPaths().get("/store/order"));
    assertNotNull(swagger.getPaths().get("/store/order").getPost());
    assertNotNull(swagger.getPaths().get("/store/order").getPost().getRequestBody());
    assertNotNull(swagger.getPaths().get("/store/order").getPost().getRequestBody().getContent().get("application/json").getSchema());
    assertTrue(swagger.getPaths().get("/store/order").getPost().getRequestBody().getContent().get("application/json").getSchema().get$ref() != null);
    assertTrue(swagger.getPaths().get("/store/order").getPost().getRequestBody().getContent().get("application/json").getSchema().get$ref().equals("#/components/schemas/Order"));
    assertTrue(swagger.getComponents().getSchemas().get("Order") instanceof Schema);
    assertTrue(swagger.getComponents().getSchemas().get("Order").getProperties().size() == 6);
}
Also used : ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) Schema(io.swagger.v3.oas.models.media.Schema) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 5 with RequestBody

use of io.swagger.v3.oas.annotations.parameters.RequestBody in project swagger-parser by swagger-api.

the class ResolverFully method resolvePath.

public void resolvePath(PathItem pathItem) {
    for (Operation op : pathItem.readOperations()) {
        // inputs
        if (op.getParameters() != null) {
            for (Parameter parameter : op.getParameters()) {
                parameter = parameter.get$ref() != null ? resolveParameter(parameter) : parameter;
                if (parameter.getSchema() != null) {
                    Schema resolved = resolveSchema(parameter.getSchema());
                    if (resolved != null) {
                        parameter.setSchema(resolved);
                    }
                }
                if (parameter.getContent() != null) {
                    Map<String, MediaType> content = parameter.getContent();
                    for (String key : content.keySet()) {
                        if (content.get(key) != null && content.get(key).getSchema() != null) {
                            Schema resolvedSchema = resolveSchema(content.get(key).getSchema());
                            if (resolvedSchema != null) {
                                content.get(key).setSchema(resolvedSchema);
                            }
                        }
                    }
                }
            }
        }
        if (op.getCallbacks() != null) {
            Map<String, Callback> callbacks = op.getCallbacks();
            for (String name : callbacks.keySet()) {
                Callback callback = callbacks.get(name);
                callback = callback.get$ref() != null ? resolveCallback(callback) : callback;
                if (callback != null) {
                    for (String callbackName : callback.keySet()) {
                        PathItem path = callback.get(callbackName);
                        if (path != null) {
                            resolvePath(path);
                        }
                    }
                }
                op.getCallbacks().put(name, callback);
            }
        }
        RequestBody refRequestBody = op.getRequestBody();
        if (refRequestBody != null) {
            RequestBody requestBody = refRequestBody.get$ref() != null ? resolveRequestBody(refRequestBody) : refRequestBody;
            op.setRequestBody(requestBody);
            if (requestBody.getContent() != null) {
                Map<String, MediaType> content = requestBody.getContent();
                for (String key : content.keySet()) {
                    if (content.get(key) != null && content.get(key).getSchema() != null) {
                        Schema resolved = resolveSchema(content.get(key).getSchema());
                        if (resolved != null) {
                            content.get(key).setSchema(resolved);
                        }
                    }
                }
            }
        }
        // responses
        ApiResponses responses = op.getResponses();
        if (responses != null) {
            for (String code : responses.keySet()) {
                ApiResponse response = responses.get(code);
                if (response.getContent() != null) {
                    Map<String, MediaType> content = response.getContent();
                    for (String mediaType : content.keySet()) {
                        if (content.get(mediaType).getSchema() != null) {
                            Schema resolved = resolveSchema(content.get(mediaType).getSchema());
                            response.getContent().get(mediaType).setSchema(resolved);
                        }
                        if (content.get(mediaType).getExamples() != null) {
                            Map<String, Example> resolved = resolveExample(content.get(mediaType).getExamples());
                            response.getContent().get(mediaType).setExamples(resolved);
                        }
                    }
                }
                resolveHeaders(response.getHeaders());
                Map<String, Link> links = response.getLinks();
                if (links != null) {
                    for (Map.Entry<String, Link> link : links.entrySet()) {
                        Link value = link.getValue();
                        Link resolvedValue = value.get$ref() != null ? resolveLink(value) : value;
                        link.setValue(resolvedValue);
                    }
                }
            }
        }
    }
}
Also used : Operation(io.swagger.v3.oas.models.Operation) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) PathItem(io.swagger.v3.oas.models.PathItem) Callback(io.swagger.v3.oas.models.callbacks.Callback) Example(io.swagger.v3.oas.models.examples.Example) Parameter(io.swagger.v3.oas.models.parameters.Parameter) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) Link(io.swagger.v3.oas.models.links.Link) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody)

Aggregations

Test (org.testng.annotations.Test)67 OpenAPI (io.swagger.v3.oas.models.OpenAPI)62 RequestBody (io.swagger.v3.oas.models.parameters.RequestBody)59 Schema (io.swagger.v3.oas.models.media.Schema)46 Operation (io.swagger.v3.oas.annotations.Operation)41 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)40 MediaType (io.swagger.v3.oas.models.media.MediaType)36 StringSchema (io.swagger.v3.oas.models.media.StringSchema)35 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)32 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)32 Content (io.swagger.v3.oas.models.media.Content)31 ApiResponses (io.swagger.v3.oas.annotations.responses.ApiResponses)28 Operation (io.swagger.v3.oas.models.Operation)27 PathItem (io.swagger.v3.oas.models.PathItem)23 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)21 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)19 Parameter (io.swagger.v3.oas.models.parameters.Parameter)15 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)14 Components (io.swagger.v3.oas.models.Components)13 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)12