Search in sources :

Example 16 with Contact

use of io.swagger.models.Contact 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;
}
Also used : HashMap(java.util.HashMap) BasicAuthorization(io.swagger.models.resourcelisting.BasicAuthorization) License(io.swagger.models.License) OAuth2Definition(io.swagger.models.auth.OAuth2Definition) Operation(io.swagger.models.Operation) BasicAuthorization(io.swagger.models.resourcelisting.BasicAuthorization) ApiKeyAuthorization(io.swagger.models.resourcelisting.ApiKeyAuthorization) Authorization(io.swagger.models.resourcelisting.Authorization) OAuth2Authorization(io.swagger.models.resourcelisting.OAuth2Authorization) PassAs(io.swagger.models.PassAs) Swagger(io.swagger.models.Swagger) ApiKeyAuthorization(io.swagger.models.resourcelisting.ApiKeyAuthorization) Path(io.swagger.models.Path) ApiDeclaration(io.swagger.models.apideclaration.ApiDeclaration) OAuth2Authorization(io.swagger.models.resourcelisting.OAuth2Authorization) Info(io.swagger.models.Info) ApiInfo(io.swagger.models.resourcelisting.ApiInfo) BasicAuthDefinition(io.swagger.models.auth.BasicAuthDefinition) Contact(io.swagger.models.Contact) ApiKeyAuthDefinition(io.swagger.models.auth.ApiKeyAuthDefinition) AuthorizationCodeGrant(io.swagger.models.resourcelisting.AuthorizationCodeGrant) ApiInfo(io.swagger.models.resourcelisting.ApiInfo) Model(io.swagger.models.Model) RefModel(io.swagger.models.RefModel) ArrayModel(io.swagger.models.ArrayModel) ImplicitGrant(io.swagger.models.resourcelisting.ImplicitGrant) Api(io.swagger.models.apideclaration.Api) AuthorizationScope(io.swagger.models.AuthorizationScope)

Example 17 with Contact

use of io.swagger.models.Contact in project cxf by apache.

the class Java2SwaggerMojo method configureSwagger.

private void configureSwagger() {
    swagger = new Swagger();
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
    Info info = new Info();
    Contact swaggerContact = new Contact();
    License swaggerLicense = new License();
    swaggerLicense.name(this.license).url(this.licenseUrl);
    swaggerContact.name(this.contact);
    info.version(this.version).description(this.description).contact(swaggerContact).license(swaggerLicense).title(this.title);
    swagger.setInfo(info);
    if (this.schemes != null) {
        for (String scheme : this.schemes) {
            swagger.scheme(Scheme.forValue(scheme));
        }
    }
    swagger.setHost(this.host);
    swagger.setBasePath(this.basePath);
}
Also used : Swagger(io.swagger.models.Swagger) License(io.swagger.models.License) Info(io.swagger.models.Info) Contact(io.swagger.models.Contact)

Example 18 with Contact

use of io.swagger.models.Contact in project vertx-swagger by bobxwang.

the class Reader method readInfoConfig.

private void readInfoConfig(SwaggerDefinition config) {
    final Info infoConfig = config.info();
    io.swagger.models.Info info = swagger.getInfo();
    if (info == null) {
        info = new io.swagger.models.Info();
        swagger.setInfo(info);
    }
    if (StringUtils.isNotBlank(infoConfig.description())) {
        info.setDescription(infoConfig.description());
    }
    if (StringUtils.isNotBlank(infoConfig.termsOfService())) {
        info.setTermsOfService(infoConfig.termsOfService());
    }
    if (StringUtils.isNotBlank(infoConfig.title())) {
        info.setTitle(infoConfig.title());
    }
    if (StringUtils.isNotBlank(infoConfig.version())) {
        info.setVersion(infoConfig.version());
    }
    if (StringUtils.isNotBlank(infoConfig.contact().name())) {
        Contact contact = info.getContact();
        if (contact == null) {
            contact = new Contact();
            info.setContact(contact);
        }
        contact.setName(infoConfig.contact().name());
        if (StringUtils.isNotBlank(infoConfig.contact().email())) {
            contact.setEmail(infoConfig.contact().email());
        }
        if (StringUtils.isNotBlank(infoConfig.contact().url())) {
            contact.setUrl(infoConfig.contact().url());
        }
    }
    if (StringUtils.isNotBlank(infoConfig.license().name())) {
        License license = info.getLicense();
        if (license == null) {
            license = new License();
            info.setLicense(license);
        }
        license.setName(infoConfig.license().name());
        if (StringUtils.isNotBlank(infoConfig.license().url())) {
            license.setUrl(infoConfig.license().url());
        }
    }
    info.getVendorExtensions().putAll(BaseReaderUtils.parseExtensions(infoConfig.extensions()));
}
Also used : License(io.swagger.models.License) Info(io.swagger.annotations.Info) Contact(io.swagger.models.Contact)

Example 19 with Contact

use of io.swagger.models.Contact in project elastest-instrumentation-manager by elastest.

the class Bootstrap method init.

@Override
public void init(ServletConfig config) throws ServletException {
    Info info = new Info().title("Swagger Server").description("RESTful API specification for the ElasTest Instrumentation Manager (EIM)").termsOfService("TBD").contact(new Contact().email("david.rojoa@atos.net")).license(new License().name("Apache 2.0").url("http://www.apache.org/licenses/LICENSE-2.0.html"));
    ServletContext context = config.getServletContext();
    Swagger swagger = new Swagger().info(info);
    // load properties
    String propertiesFile = "/WEB-INF/bootstrap.properties";
    Properties.load(config.getServletContext().getResourceAsStream(propertiesFile), propertiesFile);
    // create database schema
    EimDbCreator dbCreator = new EimDbCreator();
    dbCreator.createSchema();
    new SwaggerContextService().withServletConfig(config).updateSwagger(swagger);
}
Also used : SwaggerContextService(io.swagger.jaxrs.config.SwaggerContextService) Swagger(io.swagger.models.Swagger) License(io.swagger.models.License) ServletContext(javax.servlet.ServletContext) Info(io.swagger.models.Info) EimDbCreator(io.elastest.eim.database.mysql.EimDbCreator) Contact(io.swagger.models.Contact)

Example 20 with Contact

use of io.swagger.models.Contact in project ballerina by ballerina-lang.

the class SwaggerServiceMapper method createContactModel.

/**
 * Creates the contact swagger definition.
 * @param annotationAttributeValue The ballerina annotation attribute value for contact.
 * @param info The info definition which the contact needs to be build on.
 */
private void createContactModel(AnnotationAttachmentAttributeValueNode annotationAttributeValue, Info info) {
    if (null != annotationAttributeValue) {
        AnnotationAttachmentNode contactAnnotationAttachment = (AnnotationAttachmentNode) annotationAttributeValue.getValue();
        Map<String, AnnotationAttachmentAttributeValueNode> contactAttributes = this.listToMap(contactAnnotationAttachment);
        Contact contact = new Contact();
        if (contactAttributes.containsKey("name")) {
            contact.setName(this.getStringLiteralValue(contactAttributes.get("name")));
        }
        if (contactAttributes.containsKey("email")) {
            contact.setEmail(this.getStringLiteralValue(contactAttributes.get("email")));
        }
        if (contactAttributes.containsKey("url")) {
            contact.setUrl(this.getStringLiteralValue(contactAttributes.get("url")));
        }
        info.setContact(contact);
    }
}
Also used : AnnotationAttachmentAttributeValueNode(org.ballerinalang.model.tree.expressions.AnnotationAttachmentAttributeValueNode) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode) Contact(io.swagger.models.Contact)

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