use of org.wso2.carbon.apimgt.api.model.SwaggerData in project carbon-apimgt by wso2.
the class OAS2Parser method generateAPIDefinition.
/**
* This method generates API definition to the given api
*
* @param swaggerData api
* @return API definition in string format
* @throws APIManagementException
*/
@Override
public String generateAPIDefinition(SwaggerData swaggerData) throws APIManagementException {
Swagger swagger = new Swagger();
// Create info object
Info info = new Info();
info.setTitle(swaggerData.getTitle());
if (swaggerData.getDescription() != null) {
info.setDescription(swaggerData.getDescription());
}
Contact contact = new Contact();
// Create contact object and map business owner info
if (swaggerData.getContactName() != null) {
contact.setName(swaggerData.getContactName());
}
if (swaggerData.getContactEmail() != null) {
contact.setEmail(swaggerData.getContactEmail());
}
if (swaggerData.getContactName() != null || swaggerData.getContactEmail() != null) {
// put contact object to info object
info.setContact(contact);
}
info.setVersion(swaggerData.getVersion());
swagger.setInfo(info);
updateSwaggerSecurityDefinition(swagger, swaggerData, "https://test.com");
updateLegacyScopesFromSwagger(swagger, swaggerData);
for (SwaggerData.Resource resource : swaggerData.getResources()) {
addOrUpdatePathToSwagger(swagger, resource);
}
return getSwaggerJsonString(swagger);
}
use of org.wso2.carbon.apimgt.api.model.SwaggerData in project carbon-apimgt by wso2.
the class OAS2Parser method updateSwaggerSecurityDefinition.
/**
* Update swagger with security definition
*
* @param swagger swagger object
* @param swaggerData Swagger related data
*/
private void updateSwaggerSecurityDefinition(Swagger swagger, SwaggerData swaggerData, String authUrl) {
OAuth2Definition oAuth2Definition = new OAuth2Definition().implicit(authUrl);
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() : "";
oAuth2Definition.addScope(scope.getKey(), description);
String roles = (StringUtils.isNotBlank(scope.getRoles()) && scope.getRoles().trim().split(",").length > 0) ? scope.getRoles() : StringUtils.EMPTY;
scopeBindings.put(scope.getKey(), roles);
}
oAuth2Definition.setVendorExtension(APIConstants.SWAGGER_X_SCOPES_BINDINGS, scopeBindings);
}
swagger.addSecurityDefinition(APIConstants.SWAGGER_APIM_DEFAULT_SECURITY, oAuth2Definition);
if (swagger.getSecurity() == null) {
SecurityRequirement securityRequirement = new SecurityRequirement();
securityRequirement.setRequirements(APIConstants.SWAGGER_APIM_DEFAULT_SECURITY, new ArrayList<String>());
swagger.addSecurity(securityRequirement);
}
}
use of org.wso2.carbon.apimgt.api.model.SwaggerData in project carbon-apimgt by wso2.
the class OAS2Parser 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 swaggerObj swagger
* @return API definition in string format
* @throws APIManagementException if error occurred when generating API Definition
*/
private String generateAPIDefinition(SwaggerData swaggerData, Swagger swaggerObj) throws APIManagementException {
// Generates below model using the API's URI template
// path -> [verb1 -> template1, verb2 -> template2, ..]
Map<String, Map<String, SwaggerData.Resource>> resourceMap = getResourceMap(swaggerData);
Iterator<Map.Entry<String, Path>> itr = swaggerObj.getPaths().entrySet().iterator();
while (itr.hasNext()) {
Map.Entry<String, Path> pathEntry = itr.next();
String pathName = pathEntry.getKey();
Path path = pathEntry.getValue();
Map<String, SwaggerData.Resource> resourcesForPath = resourceMap.get(pathName);
if (resourcesForPath == null) {
// remove paths that are not in URI Templates
itr.remove();
} else {
// If path is available in the URI template, then check for operations(verbs)
for (Map.Entry<HttpMethod, Operation> operationEntry : path.getOperationMap().entrySet()) {
HttpMethod httpMethod = operationEntry.getKey();
Operation operation = operationEntry.getValue();
SwaggerData.Resource resource = resourcesForPath.get(httpMethod.toString().toUpperCase());
if (resource == null) {
// if particular operation is not available in URI templates, then remove it from swagger
path.set(httpMethod.toString().toLowerCase(), null);
} else {
// if operation is available in URI templates, update swagger operation
// with auth type, scope etc
updateOperationManagedInfo(resource, operation);
}
}
// if there are any verbs (operations) not defined in swagger then add them
for (Map.Entry<String, SwaggerData.Resource> resourcesForPathEntry : resourcesForPath.entrySet()) {
String verb = resourcesForPathEntry.getKey();
SwaggerData.Resource resource = resourcesForPathEntry.getValue();
HttpMethod method = HttpMethod.valueOf(verb.toUpperCase());
Operation operation = path.getOperationMap().get(method);
if (operation == null) {
operation = createOperation(resource);
path.set(resource.getVerb().toLowerCase(), operation);
}
}
}
}
// add to swagger if there are any new templates
for (Map.Entry<String, Map<String, SwaggerData.Resource>> resourceMapEntry : resourceMap.entrySet()) {
String path = resourceMapEntry.getKey();
Map<String, SwaggerData.Resource> verbMap = resourceMapEntry.getValue();
if (swaggerObj.getPath(path) == null) {
for (Map.Entry<String, SwaggerData.Resource> verbMapEntry : verbMap.entrySet()) {
SwaggerData.Resource resource = verbMapEntry.getValue();
addOrUpdatePathToSwagger(swaggerObj, resource);
}
}
}
updateSwaggerSecurityDefinition(swaggerObj, swaggerData, "https://test.com");
updateLegacyScopesFromSwagger(swaggerObj, swaggerData);
if (StringUtils.isEmpty(swaggerObj.getInfo().getTitle())) {
swaggerObj.getInfo().setTitle(swaggerData.getTitle());
}
if (StringUtils.isEmpty(swaggerObj.getInfo().getVersion())) {
swaggerObj.getInfo().setVersion(swaggerData.getVersion());
}
preserveResourcePathOrderFromAPI(swaggerData, swaggerObj);
return getSwaggerJsonString(swaggerObj);
}
use of org.wso2.carbon.apimgt.api.model.SwaggerData in project carbon-apimgt by wso2.
the class OAS2Parser method setLegacyScopeExtensionToSwagger.
/**
* Set scopes to the swagger extension
*
* @param swagger swagger object
* @param swaggerData Swagger API data
*/
private void setLegacyScopeExtensionToSwagger(Swagger swagger, SwaggerData swaggerData) {
Set<Scope> scopes = swaggerData.getScopes();
if (scopes != null && !scopes.isEmpty()) {
List<Map<String, String>> xSecurityScopesArray = new ArrayList<>();
for (Scope scope : scopes) {
Map<String, String> xWso2ScopesObject = new LinkedHashMap<>();
xWso2ScopesObject.put(APIConstants.SWAGGER_SCOPE_KEY, scope.getKey());
xWso2ScopesObject.put(APIConstants.SWAGGER_NAME, scope.getName());
xWso2ScopesObject.put(APIConstants.SWAGGER_ROLES, scope.getRoles());
xWso2ScopesObject.put(APIConstants.SWAGGER_DESCRIPTION, scope.getDescription());
xSecurityScopesArray.add(xWso2ScopesObject);
}
Map<String, Object> xWSO2Scopes = new LinkedHashMap<>();
xWSO2Scopes.put(APIConstants.SWAGGER_X_WSO2_SCOPES, xSecurityScopesArray);
Map<String, Object> xWSO2SecurityDefinitionObject = new LinkedHashMap<>();
xWSO2SecurityDefinitionObject.put(APIConstants.SWAGGER_OBJECT_NAME_APIM, xWSO2Scopes);
swagger.setVendorExtension(APIConstants.SWAGGER_X_WSO2_SECURITY, xWSO2SecurityDefinitionObject);
}
}
use of org.wso2.carbon.apimgt.api.model.SwaggerData in project carbon-apimgt by wso2.
the class OAS3Parser method setLegacyScopeExtensionToSwagger.
/**
* Set scopes to the openAPI extension
*
* @param openAPI OpenAPI object
* @param swaggerData Swagger API data
*/
private void setLegacyScopeExtensionToSwagger(OpenAPI openAPI, SwaggerData swaggerData) {
Set<Scope> scopes = swaggerData.getScopes();
if (scopes != null && !scopes.isEmpty()) {
List<Map<String, String>> xSecurityScopesArray = new ArrayList<>();
for (Scope scope : scopes) {
Map<String, String> xWso2ScopesObject = new LinkedHashMap<>();
xWso2ScopesObject.put(APIConstants.SWAGGER_SCOPE_KEY, scope.getKey());
xWso2ScopesObject.put(APIConstants.SWAGGER_NAME, scope.getName());
xWso2ScopesObject.put(APIConstants.SWAGGER_ROLES, scope.getRoles());
xWso2ScopesObject.put(APIConstants.SWAGGER_DESCRIPTION, scope.getDescription());
xSecurityScopesArray.add(xWso2ScopesObject);
}
Map<String, Object> xWSO2Scopes = new LinkedHashMap<>();
xWSO2Scopes.put(APIConstants.SWAGGER_X_WSO2_SCOPES, xSecurityScopesArray);
Map<String, Object> xWSO2SecurityDefinitionObject = new LinkedHashMap<>();
xWSO2SecurityDefinitionObject.put(APIConstants.SWAGGER_OBJECT_NAME_APIM, xWSO2Scopes);
openAPI.addExtension(APIConstants.SWAGGER_X_WSO2_SECURITY, xWSO2SecurityDefinitionObject);
}
}
Aggregations