use of io.swagger.models.Contact in project carbon-apimgt by wso2.
the class APIDefinitionFromSwagger20 method generateApiFromSwaggerResource.
/**
* return API Object
*
* @param provider Provider of the API.
* @param apiDefinition API definition as string
* @return API object.
* @throws APIManagementException If failed to generate API from swagger.
*/
@Override
public API.APIBuilder generateApiFromSwaggerResource(String provider, String apiDefinition) throws APIManagementException {
SwaggerParser swaggerParser = new SwaggerParser();
Swagger swagger = swaggerParser.parse(apiDefinition);
if (swagger == null) {
throw new APIManagementException("Swagger could not be generated from provided API definition");
}
Info apiInfo = swagger.getInfo();
if (apiInfo == null) {
throw new APIManagementException("Swagger doesn't contains the info");
} else {
String apiName = apiInfo.getTitle();
String apiVersion = apiInfo.getVersion();
String apiDescription = apiInfo.getDescription();
Contact contact = apiInfo.getContact();
BusinessInformation businessInformation = new BusinessInformation();
if (contact != null) {
businessInformation.setBusinessOwner(contact.getName());
businessInformation.setBusinessOwnerEmail(contact.getEmail());
}
API.APIBuilder apiBuilder = new API.APIBuilder(provider, apiName, apiVersion);
apiBuilder.businessInformation(businessInformation);
apiBuilder.description(apiDescription);
apiBuilder.context(swagger.getBasePath());
List<APIResource> apiResourceList = parseSwaggerAPIResources(new StringBuilder(apiDefinition));
Map<String, UriTemplate> uriTemplateMap = new HashMap();
for (APIResource apiResource : apiResourceList) {
uriTemplateMap.put(apiResource.getUriTemplate().getTemplateId(), apiResource.getUriTemplate());
}
apiBuilder.uriTemplates(uriTemplateMap);
apiBuilder.id(UUID.randomUUID().toString());
return apiBuilder;
}
}
use of io.swagger.models.Contact 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);
}
use of io.swagger.models.Contact in project apiee by phillip-kruger.
the class SwaggerCache method getSwaggerInfo.
private Info getSwaggerInfo() {
Contact contact = getSwaggerContact();
License license = getSwaggerLicense();
String title = whiteLabel.getProperty(INFO_TITLE, null);
String description = whiteLabel.getProperty(INFO_DESC, null);
String version = whiteLabel.getProperty(INFO_VERSION, null);
String terms = whiteLabel.getProperty(INFO_TERMS, null);
if (isSet(title, description, version, terms) || contact != null || license != null) {
Info info = new Info();
if (isSet(title))
info.setTitle(title);
if (isSet(description))
info.setDescription(description);
if (isSet(version))
info.setVersion(version);
if (isSet(terms))
info.setTermsOfService(terms);
if (contact != null)
info.setContact(contact);
if (license != null)
info.setLicense(license);
return info;
}
return null;
}
use of io.swagger.models.Contact in project incubator-servicecomb-java-chassis by apache.
the class SwaggerDefinitionProcessor method convertContact.
private Contact convertContact(io.swagger.annotations.Contact contactAnnotation) {
Contact contact = new Contact();
contact.setName(contactAnnotation.name());
contact.setUrl(contactAnnotation.url());
contact.setEmail(contactAnnotation.email());
return contact;
}
use of io.swagger.models.Contact in project swagger-parser by swagger-api.
the class LegacyConverterTest method convertSingleFile.
/**
* reads a single-file swagger definition
*/
@Test
public void convertSingleFile() throws Exception {
Swagger swagger = converter.read("src/test/resources/specs/v1_2/singleFile.json");
assertTrue(swagger.getSecurityDefinitions().size() == 2);
SecuritySchemeDefinition auth = swagger.getSecurityDefinitions().get("oauth2");
assertNotNull(auth);
assertEquals(auth.getClass(), OAuth2Definition.class);
OAuth2Definition oauth2 = (OAuth2Definition) auth;
assertEquals(oauth2.getFlow(), "implicit");
assertEquals(oauth2.getAuthorizationUrl(), "http://petstore.swagger.io/oauth/dialog");
assertTrue(oauth2.getScopes().size() == 2);
Map<String, String> scopes = oauth2.getScopes();
assertEquals(scopes.get("email"), "Access to your email address");
assertEquals(scopes.get("pets"), "Access to your pets");
auth = swagger.getSecurityDefinitions().get("apiKey");
assertNotNull(auth);
assertEquals(auth.getClass(), ApiKeyAuthDefinition.class);
ApiKeyAuthDefinition apiKey = (ApiKeyAuthDefinition) auth;
assertEquals(apiKey.getName(), "api_key");
assertEquals(apiKey.getIn(), In.HEADER);
assertEquals(swagger.getSwagger(), "2.0");
assertEquals(swagger.getHost(), "petstore.swagger.io");
assertEquals(swagger.getBasePath(), "/api");
assertNotNull(swagger.getInfo());
Info info = swagger.getInfo();
assertEquals(info.getVersion(), "1.0.0");
assertEquals(info.getTitle(), "Swagger Sample App");
assertEquals(info.getTermsOfService(), "http://swagger.io/terms/");
Contact contact = info.getContact();
assertEquals(contact.getUrl(), "apiteam@swagger.io");
License license = info.getLicense();
assertEquals(license.getName(), "Apache 2.0");
assertEquals(license.getUrl(), "http://www.apache.org/licenses/LICENSE-2.0.html");
assertTrue(swagger.getDefinitions().size() == 3);
assertTrue(swagger.getPaths().size() == 5);
Operation patchOperation = swagger.getPaths().get("/pet/{petId}").getPatch();
List<Map<String, List<String>>> security = patchOperation.getSecurity();
assertTrue(security.size() == 1);
Map<String, List<String>> securityDetail = security.get(0);
String key = securityDetail.keySet().iterator().next();
assertEquals(key, "oauth2");
List<String> oauth2Scopes = securityDetail.get(key);
assertEquals(oauth2Scopes.size(), 1);
assertEquals(oauth2Scopes.get(0), "test:anything");
Operation fetchOperation = swagger.getPaths().get("/pet/findByStatus").getGet();
QueryParameter param = (QueryParameter) fetchOperation.getParameters().get(0);
assertEquals(param.getDefaultValue(), "available");
List<String> _enum = param.getEnum();
assertEquals(_enum.get(0), "available");
assertEquals(_enum.get(1), "pending");
assertEquals(_enum.get(2), "sold");
}
Aggregations