use of io.swagger.models.parameters.Parameter in project carbon-apimgt by wso2.
the class APIDefinitionFromSwagger20 method generateMergedResourceDefinition.
@Override
public String generateMergedResourceDefinition(String resourceConfigJson, API api) {
SwaggerParser swaggerParser = new SwaggerParser();
Swagger swagger = swaggerParser.parse(resourceConfigJson);
addSecuritySchemeToSwaggerDefinition(swagger, api);
String securityName = getOauthSecurityName(swagger);
if (!StringUtils.isEmpty(securityName)) {
List<SecurityRequirement> securityRequirements = swagger.getSecurity();
if (securityRequirements != null) {
for (SecurityRequirement securityRequirement : securityRequirements) {
Map<String, List<String>> requirementMap = securityRequirement.getRequirements();
if (requirementMap.containsKey(securityName)) {
requirementMap.replace(securityName, api.getScopes());
} else {
if (!api.getScopes().isEmpty()) {
requirementMap.put(securityName, api.getScopes());
}
}
}
} else {
if (!api.getScopes().isEmpty()) {
SecurityRequirement securityRequirement = new SecurityRequirement();
securityRequirement.setRequirements(securityName, api.getScopes());
swagger.addSecurity(securityRequirement);
}
}
Map<String, UriTemplate> uriTemplateMap = new HashMap<>(api.getUriTemplates());
swagger.getPaths().entrySet().removeIf(entry -> isPathNotExist(uriTemplateMap, securityName, entry));
uriTemplateMap.forEach((k, v) -> {
Path path = swagger.getPath(v.getUriTemplate());
if (path == null) {
path = new Path();
swagger.path(v.getUriTemplate(), path);
}
Map<HttpMethod, Operation> operationMap = path.getOperationMap();
Operation operation = operationMap.get(getHttpMethodForVerb(v.getHttpVerb().toUpperCase()));
if (operation != null) {
assignScopesToOperation(operation, securityName, v.getScopes());
} else {
Operation operationTocCreate = new Operation();
operationTocCreate.addSecurity(securityName, v.getScopes());
operationTocCreate.addResponse("200", getDefaultResponse());
List<Parameter> parameterList = getParameters(v.getUriTemplate());
if (!HttpMethod.GET.toString().equalsIgnoreCase(v.getHttpVerb()) && !HttpMethod.DELETE.toString().equalsIgnoreCase(v.getHttpVerb()) && !HttpMethod.OPTIONS.toString().equalsIgnoreCase(v.getHttpVerb()) && !HttpMethod.HEAD.toString().equalsIgnoreCase(v.getHttpVerb())) {
parameterList.add(getDefaultBodyParameter());
}
operationTocCreate.setParameters(parameterList);
path.set(v.getHttpVerb().toLowerCase(), operationTocCreate);
}
});
}
return Json.pretty(swagger);
}
use of io.swagger.models.parameters.Parameter in project carbon-apimgt by wso2.
the class APIDefinitionFromSwagger20 method generateSwaggerFromResources.
/**
* generate the swagger from uri templates.
*
* @param api API Object
* @return Generated swagger resources as string.
*/
@Override
public String generateSwaggerFromResources(API.APIBuilder api) {
Swagger swagger = new Swagger();
Info info = new Info();
info.setTitle(api.getName());
info.setDescription(api.getDescription());
Contact contact = new Contact();
if (api.getBusinessInformation() != null) {
BusinessInformation businessInformation = api.getBusinessInformation();
contact.setName(businessInformation.getBusinessOwner());
contact.setEmail(businessInformation.getBusinessOwnerEmail());
}
info.setContact(contact);
info.setVersion(api.getVersion());
swagger.setInfo(info);
addSecuritySchemeToSwaggerDefinition(swagger, api.build());
Map<String, Path> stringPathMap = new HashMap();
for (UriTemplate uriTemplate : api.getUriTemplates().values()) {
String uriTemplateString = uriTemplate.getUriTemplate();
List<Parameter> parameterList = getParameters(uriTemplateString);
if (uriTemplate.getParameters() == null || uriTemplate.getParameters().isEmpty()) {
if (!HttpMethod.GET.toString().equalsIgnoreCase(uriTemplate.getHttpVerb()) && !HttpMethod.DELETE.toString().equalsIgnoreCase(uriTemplate.getHttpVerb()) && !HttpMethod.OPTIONS.toString().equalsIgnoreCase(uriTemplate.getHttpVerb()) && !HttpMethod.HEAD.toString().equalsIgnoreCase(uriTemplate.getHttpVerb())) {
parameterList.add(getDefaultBodyParameter());
}
} else {
for (URITemplateParam uriTemplateParam : uriTemplate.getParameters()) {
Parameter parameter = getParameterFromURITemplateParam(uriTemplateParam);
parameterList.add(parameter);
}
}
Operation operation = new Operation();
operation.setParameters(parameterList);
operation.setOperationId(uriTemplate.getTemplateId());
// having content types like */* can break swagger definition
if (!StringUtils.isEmpty(uriTemplate.getContentType()) && !uriTemplate.getContentType().contains("*")) {
List<String> consumesList = new ArrayList<>();
consumesList.add(uriTemplate.getContentType());
operation.setConsumes(consumesList);
}
operation.addResponse("200", getDefaultResponse());
if (!APIMgtConstants.AUTH_NO_AUTHENTICATION.equals(uriTemplate.getAuthType()) && ((api.getSecurityScheme() & 2) == 2)) {
log.debug("API security scheme : API Key Scheme ---- Resource Auth Type : Not None");
operation.addSecurity(APIMgtConstants.SWAGGER_APIKEY, null);
}
if (!APIMgtConstants.AUTH_NO_AUTHENTICATION.equals(uriTemplate.getAuthType()) && ((api.getSecurityScheme() & 1) == 1)) {
log.debug("API security scheme : Oauth Scheme ---- Resource Auth Type : Not None");
operation.addSecurity(APIMgtConstants.SWAGGER_OAUTH2, null);
}
if (stringPathMap.containsKey(uriTemplateString)) {
Path path = stringPathMap.get(uriTemplateString);
path.set(uriTemplate.getHttpVerb().toLowerCase(), operation);
} else {
Path path = new Path();
path.set(uriTemplate.getHttpVerb().toLowerCase(), operation);
stringPathMap.put(uriTemplateString, path);
}
}
swagger.setPaths(stringPathMap);
swagger.setPaths(stringPathMap);
return Json.pretty(swagger);
}
Aggregations