Search in sources :

Example 6 with AnnotationAttachmentNode

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

the class Generator method paramAnnotation.

/**
 * Get description annotation of the parameter.
 * @param node parent node.
 * @param param parameter.
 * @return description of the parameter.
 */
private static String paramAnnotation(BLangNode node, BLangVariable param) {
    String subName = param.getName() == null ? param.type.tsymbol.name.value : param.getName().getValue();
    for (AnnotationAttachmentNode annotation : getAnnotationAttachments(node)) {
        BLangRecordLiteral bLangRecordLiteral = (BLangRecordLiteral) annotation.getExpression();
        if (bLangRecordLiteral.getKeyValuePairs().size() != 1) {
            continue;
        }
        BLangExpression bLangLiteral = bLangRecordLiteral.getKeyValuePairs().get(0).getValue();
        String attribVal = bLangLiteral.toString();
        if ((annotation.getAnnotationName().getValue().equals("Param")) && attribVal.startsWith(subName + ":")) {
            return attribVal.split(subName + ":")[1].trim();
        }
    }
    return "";
}
Also used : BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode)

Example 7 with AnnotationAttachmentNode

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

the class Generator method annotFieldAnnotation.

/**
 * Get description annotation of the annotation attribute.
 * @param annotationNode parent node.
 * @param annotAttribute annotation attribute.
 * @return description of the annotation attribute.
 */
private static String annotFieldAnnotation(BLangAnnotation annotationNode, BLangAnnotAttribute annotAttribute) {
    List<? extends AnnotationAttachmentNode> annotationAttachments = getAnnotationAttachments(annotationNode);
    for (AnnotationAttachmentNode annotation : annotationAttachments) {
        if ("Field".equals(annotation.getAnnotationName().getValue())) {
            BLangRecordLiteral bLangRecordLiteral = (BLangRecordLiteral) annotation.getExpression();
            BLangExpression bLangLiteral = bLangRecordLiteral.getKeyValuePairs().get(0).getValue();
            String value = bLangLiteral.toString();
            if (value.startsWith(annotAttribute.getName().getValue())) {
                String[] valueParts = value.split(":");
                return valueParts.length == 2 ? valueParts[1] : valueParts[0];
            }
        }
    }
    return "";
}
Also used : BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode)

Example 8 with AnnotationAttachmentNode

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

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

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

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