Search in sources :

Example 61 with PathItem

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());
        }
    }
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) JSONObject(org.json.simple.JSONObject) Operation(io.swagger.v3.oas.models.Operation) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HttpMethod(io.swagger.models.HttpMethod)

Example 62 with PathItem

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);
            }
        }
    }
}
Also used : RefPath(io.swagger.models.RefPath) Path(io.swagger.models.Path) Set(java.util.Set) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) Components(io.swagger.v3.oas.models.Components) PathItem(io.swagger.v3.oas.models.PathItem) Scope(org.wso2.carbon.apimgt.api.model.Scope) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) Swagger(io.swagger.models.Swagger) OpenAPI(io.swagger.v3.oas.models.OpenAPI) API(org.wso2.carbon.apimgt.api.model.API) List(java.util.List) ArrayList(java.util.ArrayList) Paths(io.swagger.v3.oas.models.Paths) Map(java.util.Map) HashMap(java.util.HashMap) OpenAPI(io.swagger.v3.oas.models.OpenAPI)

Example 63 with PathItem

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);
                        }
                    }
                }
            }
        }
    }
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) Scope(org.wso2.carbon.apimgt.api.model.Scope) Operation(io.swagger.v3.oas.models.Operation) Paths(io.swagger.v3.oas.models.Paths) SecurityRequirement(io.swagger.v3.oas.models.security.SecurityRequirement)

Example 64 with PathItem

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);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) PathItem(io.swagger.v3.oas.models.PathItem) SwaggerData(org.wso2.carbon.apimgt.api.model.SwaggerData) Paths(io.swagger.v3.oas.models.Paths)

Example 65 with PathItem

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;
}
Also used : ArrayList(java.util.ArrayList) Operation(io.swagger.v3.oas.models.Operation) PathItem(io.swagger.v3.oas.models.PathItem) List(java.util.List) ArrayList(java.util.ArrayList) Paths(io.swagger.v3.oas.models.Paths) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HttpMethod(io.swagger.models.HttpMethod) SecurityRequirement(io.swagger.v3.oas.models.security.SecurityRequirement)

Aggregations

PathItem (io.swagger.v3.oas.models.PathItem)66 Operation (io.swagger.v3.oas.models.Operation)52 OpenAPI (io.swagger.v3.oas.models.OpenAPI)50 Test (org.testng.annotations.Test)39 Paths (io.swagger.v3.oas.models.Paths)24 Map (java.util.Map)19 HashMap (java.util.HashMap)18 Schema (io.swagger.v3.oas.models.media.Schema)16 Components (io.swagger.v3.oas.models.Components)15 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)14 ApiResponses (io.swagger.v3.oas.models.responses.ApiResponses)14 LinkedHashMap (java.util.LinkedHashMap)14 ArrayList (java.util.ArrayList)11 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)10 Parameter (io.swagger.v3.oas.models.parameters.Parameter)10 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)8 MediaType (io.swagger.v3.oas.models.media.MediaType)8 HttpMethod (io.swagger.models.HttpMethod)7 Callback (io.swagger.v3.oas.models.callbacks.Callback)7 Content (io.swagger.v3.oas.models.media.Content)7