Search in sources :

Example 11 with Contact

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;
    }
}
Also used : BusinessInformation(org.wso2.carbon.apimgt.core.models.BusinessInformation) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) APIResource(org.wso2.carbon.apimgt.core.models.APIResource) Info(io.swagger.models.Info) ServiceMethodInfo(org.wso2.msf4j.ServiceMethodInfo) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate) Contact(io.swagger.models.Contact) SwaggerParser(io.swagger.parser.SwaggerParser) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) Swagger(io.swagger.models.Swagger) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API)

Example 12 with Contact

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);
}
Also used : Path(io.swagger.models.Path) BusinessInformation(org.wso2.carbon.apimgt.core.models.BusinessInformation) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Operation(io.swagger.models.Operation) Info(io.swagger.models.Info) ServiceMethodInfo(org.wso2.msf4j.ServiceMethodInfo) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate) Contact(io.swagger.models.Contact) Swagger(io.swagger.models.Swagger) FormParameter(io.swagger.models.parameters.FormParameter) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) BodyParameter(io.swagger.models.parameters.BodyParameter) URITemplateParam(org.wso2.carbon.apimgt.core.models.URITemplateParam)

Example 13 with Contact

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;
}
Also used : License(io.swagger.models.License) Info(io.swagger.models.Info) Contact(io.swagger.models.Contact)

Example 14 with Contact

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;
}
Also used : Contact(io.swagger.models.Contact)

Example 15 with 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");
}
Also used : QueryParameter(io.swagger.models.parameters.QueryParameter) OAuth2Definition(io.swagger.models.auth.OAuth2Definition) License(io.swagger.models.License) SecuritySchemeDefinition(io.swagger.models.auth.SecuritySchemeDefinition) Operation(io.swagger.models.Operation) Info(io.swagger.models.Info) Contact(io.swagger.models.Contact) ApiKeyAuthDefinition(io.swagger.models.auth.ApiKeyAuthDefinition) Swagger(io.swagger.models.Swagger) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) Test(org.testng.annotations.Test)

Aggregations

Contact (io.swagger.models.Contact)22 Info (io.swagger.models.Info)14 License (io.swagger.models.License)11 Swagger (io.swagger.models.Swagger)11 Operation (io.swagger.models.Operation)6 Path (io.swagger.models.Path)5 ApiKeyAuthDefinition (io.swagger.models.auth.ApiKeyAuthDefinition)5 QueryParameter (io.swagger.models.parameters.QueryParameter)5 HashMap (java.util.HashMap)5 Model (io.swagger.models.Model)4 OAuth2Definition (io.swagger.models.auth.OAuth2Definition)4 Test (org.testng.annotations.Test)4 Info (io.swagger.annotations.Info)3 Person (io.swagger.models.Person)3 RefModel (io.swagger.models.RefModel)3 PathParameter (io.swagger.models.parameters.PathParameter)3 Error (io.swagger.models.Error)2 Response (io.swagger.models.Response)2 BasicAuthDefinition (io.swagger.models.auth.BasicAuthDefinition)2 SecuritySchemeDefinition (io.swagger.models.auth.SecuritySchemeDefinition)2