Search in sources :

Example 6 with Callback

use of io.swagger.v3.oas.models.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.models.callbacks.Callback in project swagger-parser by swagger-api.

the class CallbackProcessor method processCallback.

public void processCallback(Callback callback) {
    if (callback.get$ref() != null) {
        processReferenceCallback(callback);
    }
    // Resolver PathItem
    for (String name : callback.keySet()) {
        PathItem pathItem = callback.get(name);
        final Map<PathItem.HttpMethod, Operation> operationMap = pathItem.readOperationsMap();
        for (PathItem.HttpMethod httpMethod : operationMap.keySet()) {
            Operation operation = operationMap.get(httpMethod);
            operationProcessor.processOperation(operation);
        }
        List<Parameter> parameters = pathItem.getParameters();
        if (parameters != null) {
            for (Parameter parameter : parameters) {
                parameterProcessor.processParameter(parameter);
            }
        }
    }
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) Parameter(io.swagger.v3.oas.models.parameters.Parameter) Operation(io.swagger.v3.oas.models.Operation)

Example 8 with Callback

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

the class ExternalRefProcessor method processRefToExternalCallback.

public String processRefToExternalCallback(String $ref, RefFormat refFormat) {
    String renamedRef = cache.getRenamedRef($ref);
    if (renamedRef != null) {
        return renamedRef;
    }
    final Callback callback = cache.loadRef($ref, refFormat, Callback.class);
    if (callback == null) {
        // stop!  There's a problem.  retain the original ref
        LOGGER.warn("unable to load model reference from `" + $ref + "`.  It may not be available " + "or the reference isn't a valid model schema");
        return $ref;
    }
    String newRef;
    if (openAPI.getComponents() == null) {
        openAPI.setComponents(new Components());
    }
    Map<String, Callback> callbacks = openAPI.getComponents().getCallbacks();
    if (callbacks == null) {
        callbacks = new LinkedHashMap<>();
    }
    final String possiblyConflictingDefinitionName = computeDefinitionName($ref);
    Callback existingCallback = callbacks.get(possiblyConflictingDefinitionName);
    if (existingCallback != null) {
        LOGGER.debug("A model for " + existingCallback + " already exists");
        if (existingCallback.get$ref() != null) {
            // use the new model
            existingCallback = null;
        }
    }
    newRef = possiblyConflictingDefinitionName;
    cache.putRenamedRef($ref, newRef);
    if (existingCallback == null) {
        // don't overwrite existing model reference
        openAPI.getComponents().addCallbacks(newRef, callback);
        cache.addReferencedKey(newRef);
        String file = $ref.split("#/")[0];
        if (callback.get$ref() != null) {
            if (callback.get$ref() != null) {
                RefFormat format = computeRefFormat(callback.get$ref());
                if (isAnExternalRefFormat(format)) {
                    callback.set$ref(processRefToExternalCallback(callback.get$ref(), format));
                } else {
                    processRefToExternalCallback(file + callback.get$ref(), RefFormat.RELATIVE);
                }
            }
        }
    }
    return newRef;
}
Also used : Components(io.swagger.v3.oas.models.Components) Callback(io.swagger.v3.oas.models.callbacks.Callback) 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)

Example 9 with Callback

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

the class OperationProcessor method processOperation.

public void processOperation(Operation operation) {
    final List<Parameter> processedOperationParameters = parameterProcessor.processParameters(operation.getParameters());
    if (processedOperationParameters != null) {
        operation.setParameters(processedOperationParameters);
    }
    final RequestBody requestBody = operation.getRequestBody();
    if (requestBody != null) {
        requestBodyProcessor.processRequestBody(requestBody);
    }
    final Map<String, ApiResponse> responses = operation.getResponses();
    if (responses != null) {
        for (String responseCode : responses.keySet()) {
            ApiResponse response = responses.get(responseCode);
            if (response != null) {
                // This part allows parser to put response schema inline without the resolveFully option set to true
                if (response.get$ref() != null) {
                    responseProcessor.processResponse(response);
                    RefFormat refFormat = computeRefFormat(response.get$ref());
                    ApiResponse resolvedResponse = cache.loadRef(response.get$ref(), refFormat, ApiResponse.class);
                    if (resolvedResponse != null) {
                        response = resolvedResponse;
                        responses.put(responseCode, resolvedResponse);
                    }
                }
                responseProcessor.processResponse(response);
            }
        }
    }
    final Map<String, Callback> callbacks = operation.getCallbacks();
    if (callbacks != null) {
        for (String name : callbacks.keySet()) {
            Callback callback = callbacks.get(name);
            if (callback != null) {
                if (callback.get$ref() != null) {
                    String $ref = callback.get$ref();
                    RefFormat refFormat = computeRefFormat($ref);
                    if (isAnExternalRefFormat(refFormat)) {
                        final String newRef = externalRefProcessor.processRefToExternalCallback($ref, refFormat);
                        if (newRef != null) {
                            callback.set$ref(newRef);
                        }
                    }
                }
                for (String callbackName : callback.keySet()) {
                    PathItem pathItem = callback.get(callbackName);
                    final Map<PathItem.HttpMethod, Operation> operationMap = pathItem.readOperationsMap();
                    for (PathItem.HttpMethod httpMethod : operationMap.keySet()) {
                        Operation op = operationMap.get(httpMethod);
                        processOperation(op);
                    }
                    List<Parameter> parameters = pathItem.getParameters();
                    if (parameters != null) {
                        for (Parameter parameter : parameters) {
                            parameterProcessor.processParameter(parameter);
                        }
                    }
                }
            }
        }
    }
}
Also used : RefUtils.isAnExternalRefFormat(io.swagger.v3.parser.util.RefUtils.isAnExternalRefFormat) RefUtils.computeRefFormat(io.swagger.v3.parser.util.RefUtils.computeRefFormat) RefFormat(io.swagger.v3.parser.models.RefFormat) 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) Parameter(io.swagger.v3.oas.models.parameters.Parameter) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody)

Example 10 with Callback

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

Aggregations

Callback (io.swagger.v3.oas.models.callbacks.Callback)17 Operation (io.swagger.v3.oas.models.Operation)12 PathItem (io.swagger.v3.oas.models.PathItem)12 OpenAPI (io.swagger.v3.oas.models.OpenAPI)11 Test (org.testng.annotations.Test)9 Parameter (io.swagger.v3.oas.models.parameters.Parameter)8 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)8 Components (io.swagger.v3.oas.models.Components)6 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)5 Schema (io.swagger.v3.oas.models.media.Schema)5 RequestBody (io.swagger.v3.oas.models.parameters.RequestBody)5 ApiResponses (io.swagger.v3.oas.models.responses.ApiResponses)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 RefFormat (io.swagger.v3.parser.models.RefFormat)4 RefUtils.computeRefFormat (io.swagger.v3.parser.util.RefUtils.computeRefFormat)4 RefUtils.isAnExternalRefFormat (io.swagger.v3.parser.util.RefUtils.isAnExternalRefFormat)4 HashMap (java.util.HashMap)4 LinkedHashMap (java.util.LinkedHashMap)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3