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;
}
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);
}
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);
}
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);
}
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);
}
Aggregations