Search in sources :

Example 26 with AnnotationAttachmentNode

use of org.ballerinalang.model.tree.AnnotationAttachmentNode in project ballerina by ballerina-lang.

the class SwaggerResourceMapper method createHeadersModel.

/**
 * Creates headers definitions for swagger response.
 * @param annotationAttributeValue The annotation attribute value which has the headers.
 * @param response The swagger response.
 */
private void createHeadersModel(AnnotationAttachmentAttributeValueNode annotationAttributeValue, Response response) {
    if (null != annotationAttributeValue) {
        List<? extends AnnotationAttachmentAttributeValueNode> headersValueArray = annotationAttributeValue.getValueArray();
        for (AnnotationAttachmentAttributeValueNode headersValue : headersValueArray) {
            if (headersValue instanceof AnnotationAttachmentNode) {
                AnnotationAttachmentNode headerAnnotationAttachment = (AnnotationAttachmentNode) headersValue;
                Map<String, Property> headers = new HashMap<>();
                Map<String, AnnotationAttachmentAttributeValueNode> headersAttributes = this.listToMap(headerAnnotationAttachment);
                if (headersAttributes.containsKey("name") && headersAttributes.containsKey("headerType")) {
                    String headerName = this.getStringLiteralValue(headersAttributes.get("name"));
                    String type = this.getStringLiteralValue(headersAttributes.get("headerType"));
                    Property property = null;
                    if ("string".equals(type)) {
                        property = new StringProperty();
                    } else if ("number".equals(type) || "integer".equals(type)) {
                        property = new IntegerProperty();
                    } else if ("boolean".equals(type)) {
                        property = new BooleanProperty();
                    } else if ("array".equals(type)) {
                        property = new ArrayProperty();
                    }
                    if (null != property) {
                        if (headersAttributes.containsKey("description")) {
                            property.setDescription(this.getStringLiteralValue(headersAttributes.get("description")));
                        }
                        headers.put(headerName, property);
                    }
                }
                response.setHeaders(headers);
            }
        }
    }
}
Also used : IntegerProperty(io.swagger.models.properties.IntegerProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) HashMap(java.util.HashMap) BooleanProperty(io.swagger.models.properties.BooleanProperty) AnnotationAttachmentAttributeValueNode(org.ballerinalang.model.tree.expressions.AnnotationAttachmentAttributeValueNode) StringProperty(io.swagger.models.properties.StringProperty) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) Property(io.swagger.models.properties.Property) IntegerProperty(io.swagger.models.properties.IntegerProperty) BooleanProperty(io.swagger.models.properties.BooleanProperty) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode)

Example 27 with AnnotationAttachmentNode

use of org.ballerinalang.model.tree.AnnotationAttachmentNode 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)

Example 28 with AnnotationAttachmentNode

use of org.ballerinalang.model.tree.AnnotationAttachmentNode in project ballerina by ballerina-lang.

the class SwaggerServiceMapper method parseServiceInfoAnnotationAttachment.

/**
 * Parses the 'ServiceInfo' annotation and build the swagger definition for it.
 * @param service The ballerina service which has the 'ServiceInfo' annotation attachment.
 * @param swagger The swagger definition to be built up.
 */
private void parseServiceInfoAnnotationAttachment(ServiceNode service, Swagger swagger) {
    Optional<? extends AnnotationAttachmentNode> swaggerInfoAnnotation = service.getAnnotationAttachments().stream().filter(a -> null != swaggerAlias && this.swaggerAlias.equals(a.getPackageAlias().getValue()) && "ServiceInfo".equals(a.getAnnotationName().getValue())).findFirst();
    Info info = new Info().version("1.0.0").title(service.getName().getValue());
    if (swaggerInfoAnnotation.isPresent()) {
        Map<String, AnnotationAttachmentAttributeValueNode> attributes = this.listToMap(swaggerInfoAnnotation.get());
        if (attributes.containsKey("serviceVersion")) {
            info.version(this.getStringLiteralValue(attributes.get("serviceVersion")));
        }
        if (attributes.containsKey("title")) {
            info.title(this.getStringLiteralValue(attributes.get("title")));
        }
        if (attributes.containsKey("description")) {
            info.description(this.getStringLiteralValue(attributes.get("description")));
        }
        if (attributes.containsKey("termsOfService")) {
            info.termsOfService(this.getStringLiteralValue(attributes.get("termsOfService")));
        }
        this.createContactModel(attributes.get("contact"), info);
        this.createLicenseModel(attributes.get("license"), info);
        this.createExternalDocModel(attributes.get("externalDoc"), swagger);
        this.createTagModel(attributes.get("tags"), swagger);
        this.createOrganizationModel(attributes.get("organization"), info);
        this.createDevelopersModel(attributes.get("developers"), info);
    }
    swagger.setInfo(info);
}
Also used : Scheme(io.swagger.models.Scheme) ApiKeyAuthDefinition(io.swagger.models.auth.ApiKeyAuthDefinition) Swagger(io.swagger.models.Swagger) Tag(io.swagger.models.Tag) License(io.swagger.models.License) Json(io.swagger.util.Json) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) BasicAuthDefinition(io.swagger.models.auth.BasicAuthDefinition) Lists(com.google.common.collect.Lists) Map(java.util.Map) Organization(org.ballerinalang.ballerina.swagger.convertor.service.model.Organization) LinkedList(java.util.LinkedList) Contact(io.swagger.models.Contact) OAuth2Definition(io.swagger.models.auth.OAuth2Definition) Logger(org.slf4j.Logger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SecuritySchemeDefinition(io.swagger.models.auth.SecuritySchemeDefinition) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Collectors(java.util.stream.Collectors) Info(io.swagger.models.Info) Developer(org.ballerinalang.ballerina.swagger.convertor.service.model.Developer) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode) ServiceNode(org.ballerinalang.model.tree.ServiceNode) AnnotationAttachmentAttributeValueNode(org.ballerinalang.model.tree.expressions.AnnotationAttachmentAttributeValueNode) List(java.util.List) AnnotationAttachmentAttributeNode(org.ballerinalang.model.tree.expressions.AnnotationAttachmentAttributeNode) ExternalDocs(io.swagger.models.ExternalDocs) LiteralNode(org.ballerinalang.model.tree.expressions.LiteralNode) Optional(java.util.Optional) In(io.swagger.models.auth.In) AnnotationAttachmentAttributeValueNode(org.ballerinalang.model.tree.expressions.AnnotationAttachmentAttributeValueNode) Info(io.swagger.models.Info)

Example 29 with AnnotationAttachmentNode

use of org.ballerinalang.model.tree.AnnotationAttachmentNode in project ballerina by ballerina-lang.

the class SwaggerServiceMapper method createSecurityDefinitionScopesModel.

/**
 * Creates the security definition scopes for oAuth2.
 * @param authorizationScopes The annotation attribute value of authorization scopes.
 * @param oAuth2Definition The oAuth2 definition.
 */
private void createSecurityDefinitionScopesModel(AnnotationAttachmentAttributeValueNode authorizationScopes, OAuth2Definition oAuth2Definition) {
    Map<String, String> scopes = new HashMap<>();
    for (AnnotationAttachmentAttributeValueNode authScopeValue : authorizationScopes.getValueArray()) {
        if (authScopeValue instanceof AnnotationAttachmentNode) {
            AnnotationAttachmentNode authScopeAnnotationAttachment = (AnnotationAttachmentNode) authScopeValue;
            Map<String, AnnotationAttachmentAttributeValueNode> authScopeAttributes = this.listToMap(authScopeAnnotationAttachment);
            String name = this.getStringLiteralValue(authScopeAttributes.get("name"));
            String description = this.getStringLiteralValue(authScopeAttributes.get("description"));
            scopes.put(name, description);
        }
    }
    oAuth2Definition.setScopes(scopes);
}
Also used : HashMap(java.util.HashMap) AnnotationAttachmentAttributeValueNode(org.ballerinalang.model.tree.expressions.AnnotationAttachmentAttributeValueNode) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode)

Example 30 with AnnotationAttachmentNode

use of org.ballerinalang.model.tree.AnnotationAttachmentNode in project ballerina by ballerina-lang.

the class SwaggerServiceMapper method createLicenseModel.

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

Aggregations

AnnotationAttachmentNode (org.ballerinalang.model.tree.AnnotationAttachmentNode)35 AnnotationAttachmentAttributeValueNode (org.ballerinalang.model.tree.expressions.AnnotationAttachmentAttributeValueNode)18 HashMap (java.util.HashMap)12 List (java.util.List)12 LinkedList (java.util.LinkedList)11 ExternalDocs (io.swagger.models.ExternalDocs)10 Map (java.util.Map)9 Collectors (java.util.stream.Collectors)9 Lists (com.google.common.collect.Lists)8 Scheme (io.swagger.models.Scheme)8 Swagger (io.swagger.models.Swagger)8 ArrayList (java.util.ArrayList)8 Optional (java.util.Optional)8 AnnotationAttachmentAttributeNode (org.ballerinalang.model.tree.expressions.AnnotationAttachmentAttributeNode)7 LiteralNode (org.ballerinalang.model.tree.expressions.LiteralNode)7 ArrayProperty (io.swagger.models.properties.ArrayProperty)6 BooleanProperty (io.swagger.models.properties.BooleanProperty)6 IntegerProperty (io.swagger.models.properties.IntegerProperty)6 Property (io.swagger.models.properties.Property)6 StringProperty (io.swagger.models.properties.StringProperty)6