Search in sources :

Example 6 with SwaggerData

use of org.wso2.carbon.apimgt.api.model.SwaggerData in project carbon-apimgt by wso2.

the class OAS3Parser method getOASDefinitionForStore.

/**
 * Update OAS definition for store
 *
 * @param product        APIProduct
 * @param oasDefinition  OAS definition
 * @param hostsWithSchemes host addresses with protocol mapping
 * @return OAS definition
 */
@Override
public String getOASDefinitionForStore(APIProduct product, String oasDefinition, Map<String, String> hostsWithSchemes) {
    OpenAPI openAPI = getOpenAPI(oasDefinition);
    updateOperations(openAPI);
    updateEndpoints(product, hostsWithSchemes, openAPI);
    return updateSwaggerSecurityDefinitionForStore(openAPI, new SwaggerData(product), hostsWithSchemes);
}
Also used : SwaggerData(org.wso2.carbon.apimgt.api.model.SwaggerData) OpenAPI(io.swagger.v3.oas.models.OpenAPI)

Example 7 with SwaggerData

use of org.wso2.carbon.apimgt.api.model.SwaggerData in project carbon-apimgt by wso2.

the class OAS2Parser method getOASDefinitionForStore.

/**
 * Update OAS definition for store
 *
 * @param api            API
 * @param oasDefinition  OAS definition
 * @param hostsWithSchemes host addresses with protocol mapping
 * @return OAS definition
 * @throws APIManagementException throws if an error occurred
 */
@Override
public String getOASDefinitionForStore(API api, String oasDefinition, Map<String, String> hostsWithSchemes) throws APIManagementException {
    Swagger swagger = getSwagger(oasDefinition);
    updateOperations(swagger);
    updateEndpoints(api, hostsWithSchemes, swagger);
    return updateSwaggerSecurityDefinitionForStore(swagger, new SwaggerData(api), hostsWithSchemes);
}
Also used : SwaggerData(org.wso2.carbon.apimgt.api.model.SwaggerData) Swagger(io.swagger.models.Swagger)

Example 8 with SwaggerData

use of org.wso2.carbon.apimgt.api.model.SwaggerData in project carbon-apimgt by wso2.

the class OAS3Parser method updateSwaggerSecurityDefinition.

/**
 * Include Scope details to the definition
 *
 * @param openAPI     openapi definition
 * @param swaggerData Swagger related API data
 */
private void updateSwaggerSecurityDefinition(OpenAPI openAPI, SwaggerData swaggerData, String authUrl) {
    if (openAPI.getComponents() == null) {
        openAPI.setComponents(new Components());
    }
    Map<String, SecurityScheme> securitySchemes = openAPI.getComponents().getSecuritySchemes();
    if (securitySchemes == null) {
        securitySchemes = new HashMap<>();
        openAPI.getComponents().setSecuritySchemes(securitySchemes);
    }
    SecurityScheme securityScheme = securitySchemes.get(OPENAPI_SECURITY_SCHEMA_KEY);
    if (securityScheme == null) {
        securityScheme = new SecurityScheme();
        securityScheme.setType(SecurityScheme.Type.OAUTH2);
        securitySchemes.put(OPENAPI_SECURITY_SCHEMA_KEY, securityScheme);
        List<SecurityRequirement> security = new ArrayList<SecurityRequirement>();
        SecurityRequirement secReq = new SecurityRequirement();
        secReq.addList(OPENAPI_SECURITY_SCHEMA_KEY, new ArrayList<String>());
        security.add(secReq);
        openAPI.setSecurity(security);
    }
    if (securityScheme.getFlows() == null) {
        securityScheme.setFlows(new OAuthFlows());
    }
    OAuthFlow oAuthFlow = securityScheme.getFlows().getImplicit();
    if (oAuthFlow == null) {
        oAuthFlow = new OAuthFlow();
        securityScheme.getFlows().setImplicit(oAuthFlow);
    }
    oAuthFlow.setAuthorizationUrl(authUrl);
    Scopes oas3Scopes = new Scopes();
    Set<Scope> scopes = swaggerData.getScopes();
    if (scopes != null && !scopes.isEmpty()) {
        Map<String, String> scopeBindings = new HashMap<>();
        for (Scope scope : scopes) {
            String description = scope.getDescription() != null ? scope.getDescription() : "";
            oas3Scopes.put(scope.getKey(), description);
            String roles = (StringUtils.isNotBlank(scope.getRoles()) && scope.getRoles().trim().split(",").length > 0) ? scope.getRoles() : StringUtils.EMPTY;
            scopeBindings.put(scope.getKey(), roles);
        }
        oAuthFlow.addExtension(APIConstants.SWAGGER_X_SCOPES_BINDINGS, scopeBindings);
    }
    oAuthFlow.setScopes(oas3Scopes);
}
Also used : OAuthFlows(io.swagger.v3.oas.models.security.OAuthFlows) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) Components(io.swagger.v3.oas.models.Components) Scope(org.wso2.carbon.apimgt.api.model.Scope) OAuthFlow(io.swagger.v3.oas.models.security.OAuthFlow) Scopes(io.swagger.v3.oas.models.security.Scopes) SecurityScheme(io.swagger.v3.oas.models.security.SecurityScheme) SecurityRequirement(io.swagger.v3.oas.models.security.SecurityRequirement)

Example 9 with SwaggerData

use of org.wso2.carbon.apimgt.api.model.SwaggerData in project carbon-apimgt by wso2.

the class APIProviderImpl method addAPIProductSwagger.

@Override
public void addAPIProductSwagger(String productId, Map<API, List<APIProductResource>> apiToProductResourceMapping, APIProduct apiProduct, String orgId) throws APIManagementException {
    APIDefinition parser = new OAS3Parser();
    SwaggerData swaggerData = new SwaggerData(apiProduct);
    String apiProductSwagger = parser.generateAPIDefinition(swaggerData);
    apiProductSwagger = OASParserUtil.updateAPIProductSwaggerOperations(apiToProductResourceMapping, apiProductSwagger);
    saveSwaggerDefinition(productId, apiProductSwagger, orgId);
    apiProduct.setDefinition(apiProductSwagger);
}
Also used : SwaggerData(org.wso2.carbon.apimgt.api.model.SwaggerData) APIDefinition(org.wso2.carbon.apimgt.api.APIDefinition) OAS3Parser(org.wso2.carbon.apimgt.impl.definitions.OAS3Parser)

Example 10 with SwaggerData

use of org.wso2.carbon.apimgt.api.model.SwaggerData in project carbon-apimgt by wso2.

the class APIProviderImpl method updateAPIProductSwagger.

@Override
public void updateAPIProductSwagger(String productId, Map<API, List<APIProductResource>> apiToProductResourceMapping, APIProduct apiProduct, String orgId) throws APIManagementException {
    APIDefinition parser = new OAS3Parser();
    SwaggerData updatedData = new SwaggerData(apiProduct);
    String existingProductSwagger = getAPIDefinitionOfAPIProduct(apiProduct);
    String updatedProductSwagger = parser.generateAPIDefinition(updatedData, existingProductSwagger);
    updatedProductSwagger = OASParserUtil.updateAPIProductSwaggerOperations(apiToProductResourceMapping, updatedProductSwagger);
    saveSwaggerDefinition(productId, updatedProductSwagger, orgId);
    apiProduct.setDefinition(updatedProductSwagger);
}
Also used : SwaggerData(org.wso2.carbon.apimgt.api.model.SwaggerData) APIDefinition(org.wso2.carbon.apimgt.api.APIDefinition) OAS3Parser(org.wso2.carbon.apimgt.impl.definitions.OAS3Parser)

Aggregations

SwaggerData (org.wso2.carbon.apimgt.api.model.SwaggerData)21 API (org.wso2.carbon.apimgt.api.model.API)10 HashMap (java.util.HashMap)9 LinkedHashMap (java.util.LinkedHashMap)9 APIDefinition (org.wso2.carbon.apimgt.api.APIDefinition)9 Scope (org.wso2.carbon.apimgt.api.model.Scope)8 Map (java.util.Map)7 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)7 ArrayList (java.util.ArrayList)6 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)6 APIDefinitionValidationResponse (org.wso2.carbon.apimgt.api.APIDefinitionValidationResponse)5 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)5 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)5 OAS3Parser (org.wso2.carbon.apimgt.impl.definitions.OAS3Parser)5 Swagger (io.swagger.models.Swagger)3 OpenAPI (io.swagger.v3.oas.models.OpenAPI)3 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)3 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)3 HttpMethod (io.swagger.models.HttpMethod)2 PathItem (io.swagger.v3.oas.models.PathItem)2