Search in sources :

Example 6 with Callback

use of io.swagger.v3.oas.annotations.callbacks.Callback in project carbon-apimgt by wso2.

the class RestApiCommonUtil method generateOpenAPIForAsync.

/**
 * Generate a basic OpenAPI definition with given details.
 *
 * @param name             name of the API.
 * @param version          version of the API.
 * @param context          context of the API.
 * @param callbackEndpoint callback URL of the async API.
 * @return OpenAPI definition as String.
 * @throws JsonProcessingException Error occurred while generating the OpenAPI.
 */
public static String generateOpenAPIForAsync(String name, String version, String context, String callbackEndpoint) throws JsonProcessingException {
    OpenAPI openAPI = new OpenAPI();
    Info info = new Info();
    info.setTitle(name);
    info.setDescription("API Definition of " + name);
    info.setVersion(version);
    openAPI.setInfo(info);
    ArrayList<Server> servers = new ArrayList<>();
    Server server = new Server();
    server.setUrl("/");
    servers.add(server);
    openAPI.setServers(Arrays.asList(server));
    Paths paths = new Paths();
    PathItem pathItem = new PathItem();
    Operation operation = new Operation();
    ApiResponses apiResponses = new ApiResponses();
    ApiResponse apiResponse = new ApiResponse();
    apiResponse.setDescription("Default response");
    apiResponses.addApiResponse("default", apiResponse);
    operation.setResponses(apiResponses);
    pathItem.setPost(operation);
    paths.addPathItem("/*", pathItem);
    openAPI.paths(paths);
    List<String> urls = new ArrayList<>();
    urls.add(callbackEndpoint);
    Map<String, Object> tempMap = new HashMap();
    tempMap.put("type", "http");
    tempMap.put("urls", urls);
    openAPI.addExtension(X_WSO2_PRODUCTION_ENDPOINTS, tempMap);
    openAPI.addExtension(X_WSO2_SANDBOX_ENDPOINTS, tempMap);
    openAPI.addExtension(X_WSO2_AUTH_HEADER, "Authorization");
    openAPI.addExtension(X_WSO2_BASEPATH, context + "/" + version);
    openAPI.addExtension(X_WSO2_DISABLE_SECURITY, true);
    return Json.mapper().writeValueAsString(openAPI);
}
Also used : Server(io.swagger.v3.oas.models.servers.Server) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Operation(io.swagger.v3.oas.models.Operation) Info(io.swagger.v3.oas.models.info.Info) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) PathItem(io.swagger.v3.oas.models.PathItem) Paths(io.swagger.v3.oas.models.Paths) OpenAPI(io.swagger.v3.oas.models.OpenAPI) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses)

Example 7 with Callback

use of io.swagger.v3.oas.annotations.callbacks.Callback in project swagger-parser by swagger-api.

the class OpenAPIDeserializer method getCallback.

public Callback getCallback(ObjectNode node, String location, ParseResult result) {
    if (node == null) {
        return null;
    }
    Callback callback = new Callback();
    Set<String> keys = getKeys(node);
    for (String name : keys) {
        JsonNode value = node.get(name);
        if (node != null) {
            JsonNode ref = node.get("$ref");
            if (ref != null) {
                if (ref.getNodeType().equals(JsonNodeType.STRING)) {
                    String mungedRef = mungedRef(ref.textValue());
                    if (mungedRef != null) {
                        callback.set$ref(mungedRef);
                    } else {
                        callback.set$ref(ref.textValue());
                    }
                    return callback;
                } else {
                    result.invalidType(location, "$ref", "string", node);
                    return null;
                }
            }
            if (value.isObject()) {
                callback.addPathItem(name, getPathItem((ObjectNode) value, location, result));
            } else {
                result.invalidType(location, name, "object", value);
            }
            Map<String, Object> extensions = getExtensions(node);
            if (extensions != null && extensions.size() > 0) {
                callback.setExtensions(extensions);
            }
        }
    }
    return callback;
}
Also used : Callback(io.swagger.v3.oas.models.callbacks.Callback) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 8 with Callback

use of io.swagger.v3.oas.annotations.callbacks.Callback 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 9 with Callback

use of io.swagger.v3.oas.annotations.callbacks.Callback 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)

Example 10 with Callback

use of io.swagger.v3.oas.annotations.callbacks.Callback in project swagger-parser by swagger-api.

the class ResolverFully method resolveCallback.

public Callback resolveCallback(Callback callback) {
    RefFormat refFormat = computeRefFormat(callback.get$ref());
    String $ref = callback.get$ref();
    if (!isAnExternalRefFormat(refFormat)) {
        if (callbacks != null && !callbacks.isEmpty()) {
            String referenceKey = computeDefinitionName($ref);
            return callbacks.getOrDefault(referenceKey, callback);
        }
    }
    return callback;
}
Also used : RefUtils.computeRefFormat(io.swagger.v3.parser.util.RefUtils.computeRefFormat) RefUtils.isAnExternalRefFormat(io.swagger.v3.parser.util.RefUtils.isAnExternalRefFormat) RefFormat(io.swagger.v3.parser.models.RefFormat)

Aggregations

Callback (io.swagger.v3.oas.models.callbacks.Callback)19 Operation (io.swagger.v3.oas.models.Operation)13 PathItem (io.swagger.v3.oas.models.PathItem)13 OpenAPI (io.swagger.v3.oas.models.OpenAPI)12 Parameter (io.swagger.v3.oas.models.parameters.Parameter)9 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)9 Test (org.testng.annotations.Test)9 Components (io.swagger.v3.oas.models.Components)7 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)6 Schema (io.swagger.v3.oas.models.media.Schema)6 RequestBody (io.swagger.v3.oas.models.parameters.RequestBody)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 Operation (io.swagger.v3.oas.annotations.Operation)4 Example (io.swagger.v3.oas.models.examples.Example)4 Link (io.swagger.v3.oas.models.links.Link)4 StringSchema (io.swagger.v3.oas.models.media.StringSchema)4 ApiResponses (io.swagger.v3.oas.models.responses.ApiResponses)4 SecurityScheme (io.swagger.v3.oas.models.security.SecurityScheme)4 HashMap (java.util.HashMap)4 LinkedHashMap (java.util.LinkedHashMap)4