use of io.swagger.v3.oas.models.Paths 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.Paths in project carbon-apimgt by wso2.
the class OAS3Parser method addOrUpdatePathToSwagger.
/**
* Add a new path based on the provided URI template to swagger if it does not exists. If it exists,
* adds the respective operation to the existing path
*
* @param openAPI swagger object
* @param resource API resource data
*/
private void addOrUpdatePathToSwagger(OpenAPI openAPI, SwaggerData.Resource resource) {
PathItem path;
if (openAPI.getPaths() == null) {
openAPI.setPaths(new Paths());
}
if (openAPI.getPaths().get(resource.getPath()) != null) {
path = openAPI.getPaths().get(resource.getPath());
} else {
path = new PathItem();
}
Operation operation = createOperation(resource);
PathItem.HttpMethod httpMethod = PathItem.HttpMethod.valueOf(resource.getVerb());
path.operation(httpMethod, operation);
openAPI.getPaths().addPathItem(resource.getPath(), path);
}
use of io.swagger.v3.oas.models.Paths in project carbon-apimgt by wso2.
the class OAS3Parser method modifyGraphQLSwagger.
/**
* Construct openAPI definition for graphQL. Add get and post operations
*
* @param openAPI OpenAPI
* @return modified openAPI for GraphQL
*/
private void modifyGraphQLSwagger(OpenAPI openAPI) {
SwaggerData.Resource resource = new SwaggerData.Resource();
resource.setAuthType(APIConstants.AUTH_APPLICATION_OR_USER_LEVEL_TOKEN);
resource.setPolicy(APIConstants.DEFAULT_SUB_POLICY_UNLIMITED);
resource.setPath("/");
resource.setVerb(APIConstants.HTTP_POST);
Operation postOperation = createOperation(resource);
// post operation
RequestBody requestBody = new RequestBody();
requestBody.setDescription("Query or mutation to be passed to graphQL API");
requestBody.setRequired(true);
JSONObject typeOfPayload = new JSONObject();
JSONObject payload = new JSONObject();
typeOfPayload.put(APIConstants.TYPE, APIConstants.STRING);
payload.put(APIConstants.OperationParameter.PAYLOAD_PARAM_NAME, typeOfPayload);
Schema postSchema = new Schema();
postSchema.setType(APIConstants.OBJECT);
postSchema.setProperties(payload);
MediaType mediaType = new MediaType();
mediaType.setSchema(postSchema);
Content content = new Content();
content.addMediaType(APPLICATION_JSON_MEDIA_TYPE, mediaType);
requestBody.setContent(content);
postOperation.setRequestBody(requestBody);
// add post and get operations to path /*
PathItem pathItem = new PathItem();
pathItem.setPost(postOperation);
Paths paths = new Paths();
paths.put("/", pathItem);
openAPI.setPaths(paths);
}
use of io.swagger.v3.oas.models.Paths in project carbon-apimgt by wso2.
the class OAS3Parser method removeExamplesFromOpenAPI.
/**
* Remove x-examples from all the paths from the OpenAPI definition.
*
* @param apiDefinition OpenAPI definition as String
*/
public static String removeExamplesFromOpenAPI(String apiDefinition) throws APIManagementException {
try {
OpenAPIV3Parser openAPIV3Parser = new OpenAPIV3Parser();
SwaggerParseResult parseAttemptForV3 = openAPIV3Parser.readContents(apiDefinition, null, null);
if (CollectionUtils.isNotEmpty(parseAttemptForV3.getMessages())) {
log.debug("Errors found when parsing OAS definition");
}
OpenAPI openAPI = parseAttemptForV3.getOpenAPI();
for (Map.Entry<String, PathItem> entry : openAPI.getPaths().entrySet()) {
String path = entry.getKey();
List<Operation> operations = openAPI.getPaths().get(path).readOperations();
for (Operation operation : operations) {
if (operation.getExtensions() != null && operation.getExtensions().keySet().contains(APIConstants.SWAGGER_X_EXAMPLES)) {
operation.getExtensions().remove(APIConstants.SWAGGER_X_EXAMPLES);
}
}
}
return Yaml.pretty().writeValueAsString(openAPI);
} catch (JsonProcessingException e) {
throw new APIManagementException("Error while removing examples from OpenAPI definition", e, ExceptionCodes.ERROR_REMOVING_EXAMPLES);
}
}
use of io.swagger.v3.oas.models.Paths in project carbon-apimgt by wso2.
the class OAS3Parser method processDisableSecurityExtension.
/**
* This method will extractX-WSO2-disable-security extension provided in API level
* by mgw and inject that extension to all resources in OAS file
*
* @param swaggerContent String
* @return String
* @throws APIManagementException
*/
@Override
public String processDisableSecurityExtension(String swaggerContent) throws APIManagementException {
OpenAPI openAPI = getOpenAPI(swaggerContent);
Map<String, Object> apiExtensions = openAPI.getExtensions();
if (apiExtensions == null) {
return swaggerContent;
}
// Check Disable Security is enabled in API level
boolean apiLevelDisableSecurity = OASParserUtil.getDisableSecurity(apiExtensions);
Paths paths = openAPI.getPaths();
for (String pathKey : paths.keySet()) {
Map<PathItem.HttpMethod, Operation> operationsMap = paths.get(pathKey).readOperationsMap();
for (Map.Entry<PathItem.HttpMethod, Operation> entry : operationsMap.entrySet()) {
Operation operation = entry.getValue();
Map<String, Object> resourceExtensions = operation.getExtensions();
boolean extensionsAreEmpty = false;
if (apiLevelDisableSecurity) {
if (resourceExtensions == null) {
resourceExtensions = new HashMap<>();
extensionsAreEmpty = true;
}
resourceExtensions.put(APIConstants.SWAGGER_X_AUTH_TYPE, "None");
if (extensionsAreEmpty) {
operation.setExtensions(resourceExtensions);
}
} else if (resourceExtensions != null && resourceExtensions.containsKey(APIConstants.X_WSO2_DISABLE_SECURITY)) {
// Check Disable Security is enabled in resource level
boolean resourceLevelDisableSecurity = Boolean.parseBoolean(String.valueOf(resourceExtensions.get(APIConstants.X_WSO2_DISABLE_SECURITY)));
if (resourceLevelDisableSecurity) {
resourceExtensions.put(APIConstants.SWAGGER_X_AUTH_TYPE, "None");
}
}
}
}
return Json.pretty(openAPI);
}
Aggregations