use of io.swagger.v3.oas.models.PathItem in project carbon-apimgt by wso2.
the class OAS3Parser method removePublisherSpecificInfo.
/**
* Remove MG related information
*
* @param openAPI OpenAPI
*/
private void removePublisherSpecificInfo(OpenAPI openAPI) {
Map<String, Object> extensions = openAPI.getExtensions();
OASParserUtil.removePublisherSpecificInfo(extensions);
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();
OASParserUtil.removePublisherSpecificInfofromOperation(operation.getExtensions());
}
}
}
use of io.swagger.v3.oas.models.PathItem in project carbon-apimgt by wso2.
the class OASParserUtil method extractRelevantSourceData.
private static void extractRelevantSourceData(Map<API, List<APIProductResource>> apiToProductResourceMapping, SwaggerUpdateContext context) throws APIManagementException {
// Extract Paths that exist in the destination swagger from the source swagger
for (Map.Entry<API, List<APIProductResource>> mappingEntry : apiToProductResourceMapping.entrySet()) {
String sourceSwagger = mappingEntry.getKey().getSwaggerDefinition();
SwaggerVersion sourceSwaggerVersion = getSwaggerVersion(sourceSwagger);
if (sourceSwaggerVersion == SwaggerVersion.OPEN_API) {
OpenAPI srcOpenAPI = ((OAS3Parser) oas3Parser).getOpenAPI(sourceSwagger);
Set<Components> aggregatedComponents = context.getAggregatedComponents();
Components components = srcOpenAPI.getComponents();
if (components != null) {
aggregatedComponents.add(components);
}
Set<Scope> allScopes = oas3Parser.getScopes(sourceSwagger);
Paths srcPaths = srcOpenAPI.getPaths();
List<APIProductResource> apiProductResources = mappingEntry.getValue();
for (APIProductResource apiProductResource : apiProductResources) {
URITemplate uriTemplate = apiProductResource.getUriTemplate();
PathItem srcPathItem = srcPaths.get(uriTemplate.getUriTemplate());
readPathsAndScopes(srcPathItem, uriTemplate, allScopes, context);
}
} else if (sourceSwaggerVersion == SwaggerVersion.SWAGGER) {
Swagger srcSwagger = ((OAS2Parser) oas2Parser).getSwagger(sourceSwagger);
Set<Components> aggregatedComponents = context.getAggregatedComponents();
Components components = swaggerConverter.readContents(sourceSwagger, null, null).getOpenAPI().getComponents();
if (components != null) {
aggregatedComponents.add(components);
}
Set<Scope> allScopes = oas2Parser.getScopes(sourceSwagger);
Map<String, Path> srcPaths = srcSwagger.getPaths();
List<APIProductResource> apiProductResources = mappingEntry.getValue();
for (APIProductResource apiProductResource : apiProductResources) {
URITemplate uriTemplate = apiProductResource.getUriTemplate();
Path srcPath = srcPaths.get(uriTemplate.getUriTemplate());
readPathsAndScopes(swaggerConverter.convert(srcPath), uriTemplate, allScopes, context);
}
}
}
}
use of io.swagger.v3.oas.models.PathItem in project carbon-apimgt by wso2.
the class OASParserUtil method readPathsAndScopes.
private static void readPathsAndScopes(PathItem srcPathItem, URITemplate uriTemplate, final Set<Scope> allScopes, SwaggerUpdateContext context) {
Map<PathItem.HttpMethod, Operation> srcOperations = srcPathItem.readOperationsMap();
PathItem.HttpMethod httpMethod = PathItem.HttpMethod.valueOf(uriTemplate.getHTTPVerb().toUpperCase());
Operation srcOperation = srcOperations.get(httpMethod);
Paths paths = context.getPaths();
Set<Scope> aggregatedScopes = context.getAggregatedScopes();
if (!paths.containsKey(uriTemplate.getUriTemplate())) {
paths.put(uriTemplate.getUriTemplate(), new PathItem());
}
PathItem pathItem = paths.get(uriTemplate.getUriTemplate());
pathItem.operation(httpMethod, srcOperation);
readReferenceObjects(srcOperation, context);
List<SecurityRequirement> srcOperationSecurity = srcOperation.getSecurity();
if (srcOperationSecurity != null) {
for (SecurityRequirement requirement : srcOperationSecurity) {
List<String> scopes = requirement.get(OAS3Parser.OPENAPI_SECURITY_SCHEMA_KEY);
if (scopes != null) {
for (String scopeKey : scopes) {
for (Scope scope : allScopes) {
if (scope.getKey().equals(scopeKey)) {
aggregatedScopes.add(scope);
}
}
}
}
}
}
}
use of io.swagger.v3.oas.models.PathItem in project carbon-apimgt by wso2.
the class OAS3Parser method preserveResourcePathOrderFromAPI.
/**
* Preserve and rearrange the OpenAPI definition according to the resource path order of the updating API payload.
*
* @param swaggerData Updating API swagger data
* @param openAPI Updated OpenAPI definition
*/
private void preserveResourcePathOrderFromAPI(SwaggerData swaggerData, OpenAPI openAPI) {
Set<String> orderedResourcePaths = new LinkedHashSet<>();
Paths orderedOpenAPIPaths = new Paths();
// order in OpenAPI with relevance to the first matching resource path item from the swagger data path list.
for (SwaggerData.Resource resource : swaggerData.getResources()) {
String path = resource.getPath();
if (!orderedResourcePaths.contains(path)) {
orderedResourcePaths.add(path);
// Get the resource path item for the path from existing OpenAPI
PathItem resourcePathItem = openAPI.getPaths().get(path);
orderedOpenAPIPaths.addPathItem(path, resourcePathItem);
}
}
openAPI.setPaths(orderedOpenAPIPaths);
}
use of io.swagger.v3.oas.models.PathItem in project carbon-apimgt by wso2.
the class OAS3Parser method injectOtherResourceScopesToDefaultScheme.
/**
* This method returns URI templates according to the given swagger file(Swagger version 3)
*
* @param openAPI OpenAPI
* @return OpenAPI
* @throws APIManagementException
*/
private OpenAPI injectOtherResourceScopesToDefaultScheme(OpenAPI openAPI) throws APIManagementException {
List<String> schemes = getOtherSchemes();
Paths paths = openAPI.getPaths();
for (String pathKey : paths.keySet()) {
PathItem pathItem = paths.get(pathKey);
Map<PathItem.HttpMethod, Operation> operationsMap = pathItem.readOperationsMap();
for (Map.Entry<PathItem.HttpMethod, Operation> entry : operationsMap.entrySet()) {
SecurityRequirement updatedDefaultSecurityRequirement = new SecurityRequirement();
PathItem.HttpMethod httpMethod = entry.getKey();
Operation operation = entry.getValue();
List<SecurityRequirement> securityRequirements = operation.getSecurity();
if (securityRequirements == null) {
securityRequirements = new ArrayList<>();
}
if (APIConstants.SUPPORTED_METHODS.contains(httpMethod.name().toLowerCase())) {
List<String> opScopesDefault = new ArrayList<>();
List<String> opScopesDefaultInstance = getScopeOfOperations(OPENAPI_SECURITY_SCHEMA_KEY, operation);
if (opScopesDefaultInstance != null) {
opScopesDefault.addAll(opScopesDefaultInstance);
}
updatedDefaultSecurityRequirement.put(OPENAPI_SECURITY_SCHEMA_KEY, opScopesDefault);
for (Map<String, List<String>> input : securityRequirements) {
for (String scheme : schemes) {
if (!OPENAPI_SECURITY_SCHEMA_KEY.equals(scheme)) {
List<String> opScopesOthers = getScopeOfOperations(scheme, operation);
if (opScopesOthers != null) {
for (String scope : opScopesOthers) {
if (!opScopesDefault.contains(scope)) {
opScopesDefault.add(scope);
}
}
}
}
updatedDefaultSecurityRequirement.put(OPENAPI_SECURITY_SCHEMA_KEY, opScopesDefault);
}
}
securityRequirements.add(updatedDefaultSecurityRequirement);
}
operation.setSecurity(securityRequirements);
entry.setValue(operation);
operationsMap.put(httpMethod, operation);
}
paths.put(pathKey, pathItem);
}
openAPI.setPaths(paths);
return openAPI;
}
Aggregations