Search in sources :

Example 16 with SwaggerData

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

the class OASTestBase method testGenerateAPIDefinitionWithExtension.

public String testGenerateAPIDefinitionWithExtension(APIDefinition parser, String content) throws Exception {
    JSONObject jsonObject = new JSONObject(content);
    String equalNoOfResourcesWithExtension = jsonObject.getJSONObject("equalNoOfResourcesWithExtension").toString();
    Scope newScope = new Scope();
    newScope.setName("newScope");
    newScope.setKey("newScope");
    newScope.setRoles("admin");
    newScope.setDescription("newScopeDescription");
    URITemplate updatedItemGet = new URITemplate();
    updatedItemGet.setUriTemplate("/items");
    updatedItemGet.setAuthType("Application & Application User");
    updatedItemGet.setAuthTypes("Application & Application User");
    updatedItemGet.setHTTPVerb("GET");
    updatedItemGet.setHttpVerbs("GET");
    updatedItemGet.setThrottlingTier("Unlimited");
    updatedItemGet.setThrottlingTiers("Unlimited");
    updatedItemGet.setScope(newScope);
    updatedItemGet.setScopes(newScope);
    APIIdentifier identifier = new APIIdentifier("admin", "simple", "1.0.0");
    API api = new API(identifier);
    api.setScopes(new HashSet<>(Arrays.asList(sampleScope, extensionScope, newScope)));
    api.setUriTemplates(new HashSet<>(Arrays.asList(petPost, updatedItemGet)));
    String definition = parser.generateAPIDefinition(new SwaggerData(api), equalNoOfResourcesWithExtension);
    APIDefinitionValidationResponse response = parser.validateAPIDefinition(definition, false);
    Assert.assertTrue(response.isValid());
    return definition;
}
Also used : JSONObject(org.json.JSONObject) Scope(org.wso2.carbon.apimgt.api.model.Scope) SwaggerData(org.wso2.carbon.apimgt.api.model.SwaggerData) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) API(org.wso2.carbon.apimgt.api.model.API) APIDefinitionValidationResponse(org.wso2.carbon.apimgt.api.APIDefinitionValidationResponse)

Example 17 with SwaggerData

use of org.wso2.carbon.apimgt.api.model.SwaggerData 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 18 with SwaggerData

use of org.wso2.carbon.apimgt.api.model.SwaggerData 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 19 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 product        APIProduct
 * @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(APIProduct product, String oasDefinition, Map<String, String> hostsWithSchemes) throws APIManagementException {
    Swagger swagger = getSwagger(oasDefinition);
    updateOperations(swagger);
    updateEndpoints(product, hostsWithSchemes, swagger);
    return updateSwaggerSecurityDefinitionForStore(swagger, new SwaggerData(product), hostsWithSchemes);
}
Also used : SwaggerData(org.wso2.carbon.apimgt.api.model.SwaggerData) Swagger(io.swagger.models.Swagger)

Example 20 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 api            API
 * @param oasDefinition  OAS definition
 * @param hostsWithSchemes host addresses with protocol mapping
 * @return OAS definition
 */
@Override
public String getOASDefinitionForStore(API api, String oasDefinition, Map<String, String> hostsWithSchemes) {
    OpenAPI openAPI = getOpenAPI(oasDefinition);
    updateOperations(openAPI);
    updateEndpoints(api, hostsWithSchemes, openAPI);
    return updateSwaggerSecurityDefinitionForStore(openAPI, new SwaggerData(api), hostsWithSchemes);
}
Also used : SwaggerData(org.wso2.carbon.apimgt.api.model.SwaggerData) OpenAPI(io.swagger.v3.oas.models.OpenAPI)

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