Search in sources :

Example 1 with AnnotationAttachmentAttributeValueNode

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

the class SwaggerResourceMapper method createTagModel.

/**
 * Creates tag model for swagger operation.
 * @param annotationAttributeValue The annotation attribute value which has tags.
 * @param operation The swagger operation.
 */
private void createTagModel(AnnotationAttachmentAttributeValueNode annotationAttributeValue, Operation operation) {
    if (null != annotationAttributeValue) {
        if (annotationAttributeValue.getValueArray().size() > 0) {
            List<String> tags = new LinkedList<>();
            for (AnnotationAttachmentAttributeValueNode tagAttributeValue : annotationAttributeValue.getValueArray()) {
                tags.add(this.getStringLiteralValue(tagAttributeValue));
            }
            operation.setTags(tags);
        }
    }
}
Also used : AnnotationAttachmentAttributeValueNode(org.ballerinalang.model.tree.expressions.AnnotationAttachmentAttributeValueNode) LinkedList(java.util.LinkedList)

Example 2 with AnnotationAttachmentAttributeValueNode

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

the class SwaggerResourceMapper method parseHttpResourceConfig.

/**
 * Parses the 'http:resourceConfig' annotation attachment and build swagger operation.
 * @param resource The ballerina resource definition.
 * @param operationAdaptor The operation adaptor..
 */
private void parseHttpResourceConfig(ResourceNode resource, OperationAdaptor operationAdaptor) {
    Optional<? extends AnnotationAttachmentNode> responsesAnnotation = resource.getAnnotationAttachments().stream().filter(a -> null != this.httpAlias && this.httpAlias.equals(a.getPackageAlias().getValue()) && "resourceConfig".equals(a.getAnnotationName().getValue())).findFirst();
    if (responsesAnnotation.isPresent()) {
        Map<String, AnnotationAttachmentAttributeValueNode> configAttributes = this.listToMap(responsesAnnotation.get());
        if (configAttributes.containsKey("methods") && configAttributes.get("methods").getValueArray().size() > 0) {
            List<? extends AnnotationAttachmentAttributeValueNode> methodsValues = configAttributes.get("methods").getValueArray();
            // Since there is only one http method.
            operationAdaptor.setHttpOperation(this.getStringLiteralValue(methodsValues.get(0)));
        }
        if (configAttributes.containsKey("path")) {
            operationAdaptor.setPath(this.getStringLiteralValue(configAttributes.get("path")));
        }
        if (configAttributes.containsKey("produces") && configAttributes.get("produces").getValueArray().size() > 0) {
            List<String> produces = new LinkedList<>();
            List<? extends AnnotationAttachmentAttributeValueNode> producesValues = configAttributes.get("produces").getValueArray();
            for (AnnotationAttachmentAttributeValueNode producesValue : producesValues) {
                produces.add(this.getStringLiteralValue(producesValue));
            }
            operationAdaptor.getOperation().setProduces(produces);
        }
        if (configAttributes.containsKey("consumes") && configAttributes.get("consumes").getValueArray().size() > 0) {
            List<String> consumes = new LinkedList<>();
            List<? extends AnnotationAttachmentAttributeValueNode> consumesValues = configAttributes.get("consumes").getValueArray();
            for (AnnotationAttachmentAttributeValueNode consumesValue : consumesValues) {
                consumes.add(this.getStringLiteralValue(consumesValue));
            }
            operationAdaptor.getOperation().setConsumes(consumes);
        }
    }
}
Also used : Scheme(io.swagger.models.Scheme) Swagger(io.swagger.models.Swagger) StringProperty(io.swagger.models.properties.StringProperty) ModelImpl(io.swagger.models.ModelImpl) HashMap(java.util.HashMap) ArrayProperty(io.swagger.models.properties.ArrayProperty) ResourceNode(org.ballerinalang.model.tree.ResourceNode) Model(io.swagger.models.Model) ArrayList(java.util.ArrayList) VariableNode(org.ballerinalang.model.tree.VariableNode) MediaType(javax.ws.rs.core.MediaType) Lists(com.google.common.collect.Lists) Path(io.swagger.models.Path) Locale(java.util.Locale) Map(java.util.Map) Operation(io.swagger.models.Operation) LinkedList(java.util.LinkedList) LinkedHashSet(java.util.LinkedHashSet) Property(io.swagger.models.properties.Property) RefModel(io.swagger.models.RefModel) BodyParameter(io.swagger.models.parameters.BodyParameter) PathParameter(io.swagger.models.parameters.PathParameter) Set(java.util.Set) Parameter(io.swagger.models.parameters.Parameter) HttpConstants(org.ballerinalang.net.http.HttpConstants) Collectors(java.util.stream.Collectors) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode) Response(io.swagger.models.Response) AnnotationAttachmentAttributeValueNode(org.ballerinalang.model.tree.expressions.AnnotationAttachmentAttributeValueNode) List(java.util.List) IntegerProperty(io.swagger.models.properties.IntegerProperty) AnnotationAttachmentAttributeNode(org.ballerinalang.model.tree.expressions.AnnotationAttachmentAttributeNode) BooleanProperty(io.swagger.models.properties.BooleanProperty) ExternalDocs(io.swagger.models.ExternalDocs) LiteralNode(org.ballerinalang.model.tree.expressions.LiteralNode) Optional(java.util.Optional) AnnotationAttachmentAttributeValueNode(org.ballerinalang.model.tree.expressions.AnnotationAttachmentAttributeValueNode) LinkedList(java.util.LinkedList)

Example 3 with AnnotationAttachmentAttributeValueNode

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

the class SwaggerServiceMapper method createSecurityDefinitionsModel.

/**
 * Creates the security definition models for swagger definition.
 * @param annotationAttributeValue The annotation attribute value for security definitions.
 * @param swagger The swagger definition.
 */
private void createSecurityDefinitionsModel(AnnotationAttachmentAttributeValueNode annotationAttributeValue, Swagger swagger) {
    if (null != annotationAttributeValue) {
        Map<String, SecuritySchemeDefinition> securitySchemeDefinitionMap = new HashMap<>();
        for (AnnotationAttachmentAttributeValueNode authorizationValues : annotationAttributeValue.getValueArray()) {
            if (authorizationValues instanceof AnnotationAttachmentNode) {
                AnnotationAttachmentNode authAnnotationAttachment = (AnnotationAttachmentNode) authorizationValues;
                Map<String, AnnotationAttachmentAttributeValueNode> authAttributes = this.listToMap(authAnnotationAttachment);
                if (authAttributes.containsKey("name") && authAttributes.containsKey("authType")) {
                    String name = this.getStringLiteralValue(authAttributes.get("name"));
                    String type = this.getStringLiteralValue(authAttributes.get("authType"));
                    String description = "";
                    if (authAttributes.containsKey("description")) {
                        description = this.getStringLiteralValue(authAttributes.get("description"));
                    }
                    if ("basic".equals(type)) {
                        BasicAuthDefinition basicAuthDefinition = new BasicAuthDefinition();
                        basicAuthDefinition.setDescription(description);
                        securitySchemeDefinitionMap.put(name, basicAuthDefinition);
                    } else if ("apiKey".equals(type)) {
                        ApiKeyAuthDefinition apiKeyAuthDefinition = new ApiKeyAuthDefinition();
                        apiKeyAuthDefinition.setName(this.getStringLiteralValue(authAttributes.get("apiName")));
                        apiKeyAuthDefinition.setIn(In.forValue(this.getStringLiteralValue(authAttributes.get("in"))));
                        apiKeyAuthDefinition.setDescription(description);
                        securitySchemeDefinitionMap.put(name, apiKeyAuthDefinition);
                    } else if ("oauth2".equals(type)) {
                        OAuth2Definition oAuth2Definition = new OAuth2Definition();
                        oAuth2Definition.setFlow(this.getStringLiteralValue(authAttributes.get("flow")));
                        oAuth2Definition.setAuthorizationUrl(this.getStringLiteralValue(authAttributes.get("authorizationUrl")));
                        oAuth2Definition.setTokenUrl(this.getStringLiteralValue(authAttributes.get("tokenUrl")));
                        this.createSecurityDefinitionScopesModel(authAttributes.get("authorizationScopes"), oAuth2Definition);
                        oAuth2Definition.setDescription(description);
                        securitySchemeDefinitionMap.put(name, oAuth2Definition);
                    }
                }
            }
        }
        swagger.setSecurityDefinitions(securitySchemeDefinitionMap);
    }
}
Also used : ApiKeyAuthDefinition(io.swagger.models.auth.ApiKeyAuthDefinition) HashMap(java.util.HashMap) AnnotationAttachmentAttributeValueNode(org.ballerinalang.model.tree.expressions.AnnotationAttachmentAttributeValueNode) OAuth2Definition(io.swagger.models.auth.OAuth2Definition) SecuritySchemeDefinition(io.swagger.models.auth.SecuritySchemeDefinition) BasicAuthDefinition(io.swagger.models.auth.BasicAuthDefinition) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode)

Example 4 with AnnotationAttachmentAttributeValueNode

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

the class SwaggerServiceMapper method createOrganizationModel.

/**
 * Creates vendor extension for organization.
 * @param annotationAttributeValue The annotation attribute value for organization vendor extension.
 * @param info The info definition.
 */
private void createOrganizationModel(AnnotationAttachmentAttributeValueNode annotationAttributeValue, Info info) {
    if (null != annotationAttributeValue) {
        AnnotationAttachmentNode organizationAnnotationAttachment = (AnnotationAttachmentNode) annotationAttributeValue.getValue();
        Map<String, AnnotationAttachmentAttributeValueNode> organizationAttributes = this.listToMap(organizationAnnotationAttachment);
        Organization organization = new Organization();
        if (organizationAttributes.containsKey("name")) {
            organization.setName(this.getStringLiteralValue(organizationAttributes.get("name")));
        }
        if (organizationAttributes.containsKey("url")) {
            organization.setUrl(this.getStringLiteralValue(organizationAttributes.get("url")));
        }
        info.setVendorExtension("x-organization", organization);
    }
}
Also used : Organization(org.ballerinalang.ballerina.swagger.convertor.service.model.Organization) AnnotationAttachmentAttributeValueNode(org.ballerinalang.model.tree.expressions.AnnotationAttachmentAttributeValueNode) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode)

Example 5 with AnnotationAttachmentAttributeValueNode

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

the class SwaggerServiceMapper method createExternalDocModel.

/**
 * Creates external docs swagger definition.
 * @param annotationAttributeValue The ballerina annotation attribute value for external docs.
 * @param swagger The swagger definition which the external docs needs to be build on.
 */
private void createExternalDocModel(AnnotationAttachmentAttributeValueNode annotationAttributeValue, Swagger swagger) {
    if (null != annotationAttributeValue) {
        AnnotationAttachmentNode externalDocAnnotationAttachment = (AnnotationAttachmentNode) annotationAttributeValue.getValue();
        Map<String, AnnotationAttachmentAttributeValueNode> externalDocAttributes = this.listToMap(externalDocAnnotationAttachment);
        ExternalDocs externalDocs = new ExternalDocs();
        if (externalDocAttributes.containsKey("description")) {
            externalDocs.setDescription(this.getStringLiteralValue(externalDocAttributes.get("description")));
        }
        if (externalDocAttributes.containsKey("url")) {
            externalDocs.setUrl(this.getStringLiteralValue(externalDocAttributes.get("url")));
        }
        swagger.setExternalDocs(externalDocs);
    }
}
Also used : AnnotationAttachmentAttributeValueNode(org.ballerinalang.model.tree.expressions.AnnotationAttachmentAttributeValueNode) ExternalDocs(io.swagger.models.ExternalDocs) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode)

Aggregations

AnnotationAttachmentAttributeValueNode (org.ballerinalang.model.tree.expressions.AnnotationAttachmentAttributeValueNode)19 AnnotationAttachmentNode (org.ballerinalang.model.tree.AnnotationAttachmentNode)18 HashMap (java.util.HashMap)11 LinkedList (java.util.LinkedList)11 ExternalDocs (io.swagger.models.ExternalDocs)10 Lists (com.google.common.collect.Lists)8 Scheme (io.swagger.models.Scheme)8 Swagger (io.swagger.models.Swagger)8 List (java.util.List)8 Map (java.util.Map)8 Optional (java.util.Optional)8 Collectors (java.util.stream.Collectors)8 AnnotationAttachmentAttributeNode (org.ballerinalang.model.tree.expressions.AnnotationAttachmentAttributeNode)8 LiteralNode (org.ballerinalang.model.tree.expressions.LiteralNode)8 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 Model (io.swagger.models.Model)5