use of io.swagger.v3.oas.models.PathItem in project carbon-apimgt by wso2.
the class OAS3Parser method updateOperations.
/**
* Update OAS operations for Store
*
* @param openAPI OpenAPI to be updated
*/
private void updateOperations(OpenAPI openAPI) {
for (String pathKey : openAPI.getPaths().keySet()) {
PathItem pathItem = openAPI.getPaths().get(pathKey);
for (Map.Entry<PathItem.HttpMethod, Operation> entry : pathItem.readOperationsMap().entrySet()) {
Operation operation = entry.getValue();
Map<String, Object> extensions = operation.getExtensions();
if (extensions != null) {
// remove mediation extension
if (extensions.containsKey(APIConstants.SWAGGER_X_MEDIATION_SCRIPT)) {
extensions.remove(APIConstants.SWAGGER_X_MEDIATION_SCRIPT);
}
// set x-scope value to security definition if it not there.
if (extensions.containsKey(APIConstants.SWAGGER_X_WSO2_SCOPES)) {
String scope = (String) extensions.get(APIConstants.SWAGGER_X_WSO2_SCOPES);
List<SecurityRequirement> security = operation.getSecurity();
if (security == null) {
security = new ArrayList<>();
operation.setSecurity(security);
}
for (Map<String, List<String>> requirement : security) {
if (requirement.get(OPENAPI_SECURITY_SCHEMA_KEY) == null || !requirement.get(OPENAPI_SECURITY_SCHEMA_KEY).contains(scope)) {
requirement.put(OPENAPI_SECURITY_SCHEMA_KEY, Collections.singletonList(scope));
}
}
}
}
}
}
}
use of io.swagger.v3.oas.models.PathItem in project carbon-apimgt by wso2.
the class OAS3Parser method generateAPIDefinition.
/**
* This method generates API definition using the given api's URI templates and the swagger.
* It will alter the provided swagger definition based on the URI templates. For example: if there is a new
* URI template which is not included in the swagger, it will be added to the swagger as a basic resource. Any
* additional resources inside the swagger will be removed from the swagger. Changes to scopes, throtting policies,
* on the resource will be updated on the swagger
*
* @param swaggerData api
* @param openAPI OpenAPI
* @return API definition in string format
* @throws APIManagementException if error occurred when generating API Definition
*/
private String generateAPIDefinition(SwaggerData swaggerData, OpenAPI openAPI) throws APIManagementException {
Set<SwaggerData.Resource> copy = new HashSet<>(swaggerData.getResources());
Iterator<Map.Entry<String, PathItem>> itr = openAPI.getPaths().entrySet().iterator();
while (itr.hasNext()) {
Map.Entry<String, PathItem> pathEntry = itr.next();
String pathKey = pathEntry.getKey();
PathItem pathItem = pathEntry.getValue();
for (Map.Entry<PathItem.HttpMethod, Operation> entry : pathItem.readOperationsMap().entrySet()) {
Operation operation = entry.getValue();
boolean operationFound = false;
for (SwaggerData.Resource resource : swaggerData.getResources()) {
if (pathKey.equalsIgnoreCase(resource.getPath()) && entry.getKey().name().equalsIgnoreCase(resource.getVerb())) {
// update operations in definition
operationFound = true;
copy.remove(resource);
updateOperationManagedInfo(resource, operation);
break;
}
}
// remove operation from definition
if (!operationFound) {
pathItem.operation(entry.getKey(), null);
}
}
if (pathItem.readOperations().isEmpty()) {
itr.remove();
}
}
if (APIConstants.GRAPHQL_API.equals(swaggerData.getTransportType())) {
modifyGraphQLSwagger(openAPI);
} else {
// adding new operations to the definition
for (SwaggerData.Resource resource : copy) {
addOrUpdatePathToSwagger(openAPI, resource);
}
}
updateSwaggerSecurityDefinition(openAPI, swaggerData, OPENAPI_DEFAULT_AUTHORIZATION_URL);
updateLegacyScopesFromSwagger(openAPI, swaggerData);
openAPI.getInfo().setTitle(swaggerData.getTitle());
if (StringUtils.isEmpty(openAPI.getInfo().getVersion())) {
openAPI.getInfo().setVersion(swaggerData.getVersion());
}
if (!APIConstants.GRAPHQL_API.equals(swaggerData.getTransportType())) {
preserveResourcePathOrderFromAPI(swaggerData, openAPI);
}
return Json.pretty(openAPI);
}
use of io.swagger.v3.oas.models.PathItem in project carbon-apimgt by wso2.
the class OAS3Parser method getURITemplates.
/**
* This method returns URI templates according to the given swagger file
*
* @param resourceConfigsJSON swaggerJSON
* @return URI Templates
* @throws APIManagementException
*/
@Override
public Set<URITemplate> getURITemplates(String resourceConfigsJSON) throws APIManagementException {
OpenAPI openAPI = getOpenAPI(resourceConfigsJSON);
Set<URITemplate> urlTemplates = new LinkedHashSet<>();
Set<Scope> scopes = getScopes(resourceConfigsJSON);
for (String pathKey : openAPI.getPaths().keySet()) {
PathItem pathItem = openAPI.getPaths().get(pathKey);
for (Map.Entry<PathItem.HttpMethod, Operation> entry : pathItem.readOperationsMap().entrySet()) {
Operation operation = entry.getValue();
URITemplate template = new URITemplate();
if (APIConstants.SUPPORTED_METHODS.contains(entry.getKey().name().toLowerCase())) {
template.setHTTPVerb(entry.getKey().name().toUpperCase());
template.setHttpVerbs(entry.getKey().name().toUpperCase());
template.setUriTemplate(pathKey);
List<String> opScopes = getScopeOfOperations(OPENAPI_SECURITY_SCHEMA_KEY, operation);
if (!opScopes.isEmpty()) {
if (opScopes.size() == 1) {
String firstScope = opScopes.get(0);
if (StringUtils.isNoneBlank(firstScope)) {
Scope scope = APIUtil.findScopeByKey(scopes, firstScope);
if (scope == null) {
throw new APIManagementException("Scope '" + firstScope + "' not found.");
}
template.setScope(scope);
template.setScopes(scope);
}
} else {
template = OASParserUtil.setScopesToTemplate(template, opScopes, scopes);
}
} else if (!getScopeOfOperations("OAuth2Security", operation).isEmpty()) {
opScopes = getScopeOfOperations("OAuth2Security", operation);
if (opScopes.size() == 1) {
String firstScope = opScopes.get(0);
Scope scope = APIUtil.findScopeByKey(scopes, firstScope);
if (scope == null) {
throw new APIManagementException("Scope '" + firstScope + "' not found.");
}
template.setScope(scope);
template.setScopes(scope);
} else {
template = OASParserUtil.setScopesToTemplate(template, opScopes, scopes);
}
}
Map<String, Object> extensions = operation.getExtensions();
if (extensions != null) {
if (extensions.containsKey(APIConstants.SWAGGER_X_AUTH_TYPE)) {
String scopeKey = (String) extensions.get(APIConstants.SWAGGER_X_AUTH_TYPE);
template.setAuthType(scopeKey);
template.setAuthTypes(scopeKey);
} else {
template.setAuthType("Any");
template.setAuthTypes("Any");
}
if (extensions.containsKey(APIConstants.SWAGGER_X_THROTTLING_TIER)) {
String throttlingTier = (String) extensions.get(APIConstants.SWAGGER_X_THROTTLING_TIER);
template.setThrottlingTier(throttlingTier);
template.setThrottlingTiers(throttlingTier);
}
if (extensions.containsKey(APIConstants.SWAGGER_X_MEDIATION_SCRIPT)) {
String mediationScript = (String) extensions.get(APIConstants.SWAGGER_X_MEDIATION_SCRIPT);
template.setMediationScript(mediationScript);
template.setMediationScripts(template.getHTTPVerb(), mediationScript);
}
if (extensions.containsKey(APIConstants.SWAGGER_X_AMZN_RESOURCE_NAME)) {
template.setAmznResourceName((String) extensions.get(APIConstants.SWAGGER_X_AMZN_RESOURCE_NAME));
}
if (extensions.containsKey(APIConstants.SWAGGER_X_AMZN_RESOURCE_TIMEOUT)) {
template.setAmznResourceTimeout(((Number) extensions.get(APIConstants.SWAGGER_X_AMZN_RESOURCE_TIMEOUT)).intValue());
}
}
urlTemplates.add(template);
}
}
}
return urlTemplates;
}
use of io.swagger.v3.oas.models.PathItem in project carbon-apimgt by wso2.
the class OpenAPIUtils method getPathItemExtensions.
private static Map<String, Object> getPathItemExtensions(MessageContext synCtx, OpenAPI openAPI) {
if (openAPI != null) {
String apiElectedResource = (String) synCtx.getProperty(APIConstants.API_ELECTED_RESOURCE);
org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
String httpMethod = (String) axis2MessageContext.getProperty(APIConstants.DigestAuthConstants.HTTP_METHOD);
PathItem path = openAPI.getPaths().get(apiElectedResource);
if (path != null) {
switch(httpMethod) {
case APIConstants.HTTP_GET:
return path.getGet().getExtensions();
case APIConstants.HTTP_POST:
return path.getPost().getExtensions();
case APIConstants.HTTP_PUT:
return path.getPut().getExtensions();
case APIConstants.HTTP_DELETE:
return path.getDelete().getExtensions();
case APIConstants.HTTP_HEAD:
return path.getHead().getExtensions();
case APIConstants.HTTP_OPTIONS:
return path.getOptions().getExtensions();
case APIConstants.HTTP_PATCH:
return path.getPatch().getExtensions();
}
}
}
return null;
}
use of io.swagger.v3.oas.models.PathItem in project carbon-apimgt by wso2.
the class OpenAPIUtils method getPathItemSecurityScopes.
private static List<String> getPathItemSecurityScopes(MessageContext synCtx, OpenAPI openAPI) {
if (openAPI != null) {
String apiElectedResource = (String) synCtx.getProperty(APIConstants.API_ELECTED_RESOURCE);
org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
String httpMethod = (String) axis2MessageContext.getProperty(APIConstants.DigestAuthConstants.HTTP_METHOD);
PathItem path = openAPI.getPaths().get(apiElectedResource);
if (path != null) {
Operation operation = path.readOperationsMap().get(PathItem.HttpMethod.valueOf(httpMethod));
return getDefaultSecurityScopes(operation.getSecurity());
}
}
return null;
}
Aggregations