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);
}
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);
}
}
}
}
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;
}
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);
}
}
}
}
}
}
}
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;
}
Aggregations