Search in sources :

Example 66 with PathItem

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

Example 67 with PathItem

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

Example 68 with PathItem

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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) Operation(io.swagger.v3.oas.models.Operation) PathItem(io.swagger.v3.oas.models.PathItem) Scope(org.wso2.carbon.apimgt.api.model.Scope) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) JSONObject(org.json.simple.JSONObject) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HttpMethod(io.swagger.models.HttpMethod)

Example 69 with PathItem

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;
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 70 with PathItem

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;
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) Operation(io.swagger.v3.oas.models.Operation) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

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