use of io.swagger.models.AuthorizationScope in project swagger-parser by swagger-api.
the class SwaggerCompatConverter method convertOperation.
public Operation convertOperation(String tag, io.swagger.models.apideclaration.Operation operation, ApiDeclaration apiDeclaration) {
Method method;
if (operation.getMethod() == null) {
JsonNode node = (JsonNode) operation.getExtraFields().get("httpMethod");
method = Method.forValue(node.asText());
operation.setMethod(method);
}
Operation output = new Operation().summary(operation.getSummary()).description(operation.getNotes()).operationId(operation.getNickname());
if (tag != null) {
output.tag(tag);
}
for (io.swagger.models.apideclaration.Parameter parameter : operation.getParameters()) {
output.parameter(convertParameter(parameter));
}
if (operation.getConsumes() != null && !operation.getConsumes().isEmpty()) {
for (String consumes : operation.getConsumes()) {
output.consumes(consumes);
}
} else if (apiDeclaration.getConsumes() != null) {
for (String consumes : apiDeclaration.getConsumes()) {
output.consumes(consumes);
}
}
if (operation.getProduces() != null && !operation.getProduces().isEmpty()) {
for (String produces : operation.getProduces()) {
output.produces(produces);
}
} else if (apiDeclaration.getProduces() != null) {
for (String produces : apiDeclaration.getProduces()) {
output.produces(produces);
}
}
for (ResponseMessage message : operation.getResponseMessages()) {
Response response = new Response().description(message.getMessage());
Model responseModel = null;
if (message.getResponseModel() != null) {
response.schema(new RefProperty(message.getResponseModel()));
}
output.response(message.getCode(), response);
}
// default response type
Property responseProperty = propertyFromTypedObject(operation);
Response response = new Response().description("success").schema(responseProperty);
if (output.getResponses() == null) {
output.defaultResponse(response);
} else if (responseProperty != null) {
output.response(200, response);
}
Map<String, List<AuthorizationScope>> auths = operation.getAuthorizations();
for (String securityName : auths.keySet()) {
List<AuthorizationScope> scopes = auths.get(securityName);
List<String> updatedScopes = new ArrayList<String>();
for (AuthorizationScope s : scopes) {
updatedScopes.add(s.getScope());
}
output.addSecurity(securityName, updatedScopes);
}
return output;
}
use of io.swagger.models.AuthorizationScope in project swagger-parser by swagger-api.
the class SwaggerParser method readAuthorizationScopes.
protected List<AuthorizationScope> readAuthorizationScopes(List<Map<String, Object>> map, MessageBuilder messages) {
List<AuthorizationScope> authorizationScopes = new ArrayList<>();
for (Map<String, Object> object : map) {
AuthorizationScope authorizationScope = new AuthorizationScope();
Object scope = object.get("scope");
if (scope != null) {
authorizationScope.setScope(scope.toString());
} else {
messages.append(new Message("AuthorizationScopes.scopes.scope", "missing required scope", Severity.ERROR));
}
Object description = object.get("description");
if (description != null) {
authorizationScope.setDescription(description.toString());
} else {
messages.append(new Message("AuthorizationScopes.scopes.description", "missing description", Severity.RECOMMENDED));
}
authorizationScopes.add(authorizationScope);
}
return authorizationScopes;
}
use of io.swagger.models.AuthorizationScope in project swagger-parser by swagger-api.
the class SwaggerCompatConverter method convert.
public Swagger convert(ResourceListing resourceListing, List<ApiDeclaration> apiDeclarations) {
Info info = new Info();
if (resourceListing.getInfo() != null) {
ApiInfo apiInfo = resourceListing.getInfo();
Contact contact = null;
if (apiInfo.getContact() != null) {
contact = new Contact().url(apiInfo.getContact());
}
License license = null;
if (apiInfo.getLicense() != null) {
license = new License().name(apiInfo.getLicense()).url(apiInfo.getLicenseUrl());
}
info = new Info().description(apiInfo.getDescription()).version(resourceListing.getApiVersion()).title(apiInfo.getTitle()).termsOfService(apiInfo.getTermsOfServiceUrl()).contact(contact).license(license);
} else if (resourceListing.getApiVersion() != null) {
info = new Info().version(resourceListing.getApiVersion());
}
Map<String, Path> paths = new HashMap<String, Path>();
Map<String, Model> definitions = new HashMap<String, Model>();
String basePath = null;
for (ApiDeclaration apiDeclaration : apiDeclarations) {
String tag;
if (apiDeclaration.getApiListingRef() != null) {
String refPath = apiDeclaration.getApiListingRef().getPath();
tag = refPath.substring(refPath.lastIndexOf("/") + 1);
} else {
tag = apiDeclaration.getResourcePath();
}
if (tag != null) {
tag = tag.replaceAll("/", "");
}
if (basePath != null) {
if (!basePath.equals(apiDeclaration.getBasePath()) && apiDeclaration.getBasePath() != null) {
LOGGER.warn("warning! multiple basePath values not supported!");
}
} else {
basePath = apiDeclaration.getBasePath();
}
List<Api> apis = apiDeclaration.getApis();
for (Api api : apis) {
String apiPath = api.getPath();
String description = api.getDescription();
List<io.swagger.models.apideclaration.Operation> ops = api.getOperations();
Path path = paths.get(apiPath);
if (path == null) {
path = new Path();
paths.put(apiPath, path);
}
for (io.swagger.models.apideclaration.Operation op : ops) {
Operation operation = convertOperation(tag, op, apiDeclaration);
if (op.getMethod() != null) {
path.set(op.getMethod().toString().toLowerCase(), operation);
} else {
LOGGER.info("skipping operation with missing method:\n" + Json.pretty(op));
}
}
}
// model definitions
Map<String, io.swagger.models.apideclaration.Model> apiModels = apiDeclaration.getModels();
for (String key : apiModels.keySet()) {
Model model = convertModel(apiModels.get(key));
definitions.put(key, model);
}
}
String host = null;
String scheme = "http";
if (basePath != null) {
String[] parts = basePath.split("://");
if (parts.length == 2) {
scheme = parts[0];
int pos = parts[1].indexOf("/");
if (pos != -1) {
host = parts[1].substring(0, pos);
basePath = parts[1].substring(pos);
} else {
host = parts[1];
basePath = "/";
}
}
if (!basePath.startsWith("/")) {
basePath = "/" + basePath;
}
}
Swagger swagger = new Swagger().host(host).scheme(Scheme.forValue(scheme)).basePath(basePath).info(info).paths(paths).basePath(basePath);
swagger.setDefinitions(definitions);
// host is read from the api declarations
Map<String, Authorization> authorizations = resourceListing.getAuthorizations();
if (authorizations != null) {
for (String authNickname : authorizations.keySet()) {
Authorization auth = authorizations.get(authNickname);
if (auth instanceof OAuth2Authorization) {
OAuth2Authorization o2 = (OAuth2Authorization) auth;
List<AuthorizationScope> scopes = o2.getScopes();
if (o2.getGrantTypes().getImplicit() != null) {
ImplicitGrant ig = o2.getGrantTypes().getImplicit();
OAuth2Definition oauth2 = new OAuth2Definition().implicit(ig.getLoginEndpoint().getUrl());
if (swagger.getSecurityDefinitions() != null && swagger.getSecurityDefinitions().keySet().contains(authNickname)) {
System.err.println("Warning! Authorization nickname already in use!");
} else {
swagger.securityDefinition(authNickname, oauth2);
}
for (AuthorizationScope scope : scopes) {
oauth2.scope(scope.getScope(), scope.getDescription());
}
} else if (o2.getGrantTypes().getAuthorization_code() != null) {
AuthorizationCodeGrant ac = (AuthorizationCodeGrant) o2.getGrantTypes().getAuthorization_code();
OAuth2Definition oauth2 = new OAuth2Definition().accessCode(ac.getTokenRequestEndpoint().getUrl(), ac.getTokenEndpoint().getUrl());
if (swagger.getSecurityDefinitions() != null && swagger.getSecurityDefinitions().keySet().contains(authNickname)) {
System.err.println("Warning! Authorization nickname already in use!");
} else {
swagger.securityDefinition(authNickname, oauth2);
}
for (AuthorizationScope scope : scopes) {
oauth2.scope(scope.getScope(), scope.getDescription());
}
}
} else if (auth instanceof ApiKeyAuthorization) {
ApiKeyAuthorization o2 = (ApiKeyAuthorization) auth;
ApiKeyAuthDefinition def = new ApiKeyAuthDefinition();
PassAs passAs = o2.getPassAs();
if (PassAs.HEADER.equals(passAs)) {
def.in(In.HEADER);
} else {
def.in(In.QUERY);
}
def.setName(o2.getKeyname());
swagger.securityDefinition(authNickname, def);
} else if (auth instanceof BasicAuthorization) {
BasicAuthDefinition def = new BasicAuthDefinition();
swagger.securityDefinition(authNickname, def);
}
}
}
return swagger;
}
Aggregations