Search in sources :

Example 21 with AnnotationAttachmentNode

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

the class SwaggerResourceMapper method parseResponsesAnnotationAttachment.

/**
 * Parses the 'Responses' annotation attachment and build swagger operation.
 * @param resource The ballerina resource definition.
 * @param op The swagger operation.
 */
private void parseResponsesAnnotationAttachment(ResourceNode resource, Operation op) {
    Optional<? extends AnnotationAttachmentNode> responsesAnnotation = resource.getAnnotationAttachments().stream().filter(a -> null != swaggerAlias && this.swaggerAlias.equals(a.getPackageAlias().getValue()) && "Responses".equals(a.getAnnotationName().getValue())).findFirst();
    if (responsesAnnotation.isPresent()) {
        Map<String, AnnotationAttachmentAttributeValueNode> responsesAttributes = this.listToMap(responsesAnnotation.get());
        if (responsesAttributes.containsKey("value")) {
            List<? extends AnnotationAttachmentAttributeValueNode> responsesValues = responsesAttributes.get("value").getValueArray();
            if (responsesValues.size() > 0) {
                Map<String, Response> responses = new HashMap<>();
                for (AnnotationAttachmentAttributeValueNode responsesValue : responsesValues) {
                    AnnotationAttachmentNode responseAnnotationAttachment = (AnnotationAttachmentNode) responsesValue.getValue();
                    Map<String, AnnotationAttachmentAttributeValueNode> responseAttributes = this.listToMap(responseAnnotationAttachment);
                    if (responseAttributes.containsKey("code")) {
                        String code = this.getStringLiteralValue(responseAttributes.get("code"));
                        Response response = new Response();
                        if (responseAttributes.containsKey("description")) {
                            response.setDescription(this.getStringLiteralValue(responseAttributes.get("description")));
                        }
                        // TODO: Parse 'response' attribute for $.paths./resource-path.responses[*]["code"].schema
                        this.createHeadersModel(responseAttributes.get("headers"), response);
                        responses.put(code, response);
                    }
                }
                op.setResponses(responses);
            }
        }
    }
}
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) Response(io.swagger.models.Response) HashMap(java.util.HashMap) AnnotationAttachmentAttributeValueNode(org.ballerinalang.model.tree.expressions.AnnotationAttachmentAttributeValueNode) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode)

Example 22 with AnnotationAttachmentNode

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

the class SwaggerResourceMapper method createExternalDocsModel.

/**
 * Creates external docs swagger definitions.
 * @param annotationAttributeValue The annotation attribute value for external docs.
 * @param operation The swagger operation.
 */
private void createExternalDocsModel(AnnotationAttachmentAttributeValueNode annotationAttributeValue, Operation operation) {
    if (null != annotationAttributeValue) {
        if (annotationAttributeValue instanceof AnnotationAttachmentNode) {
            AnnotationAttachmentNode externalDocAnnotationAttachment = (AnnotationAttachmentNode) annotationAttributeValue;
            ExternalDocs externalDocs = new ExternalDocs();
            Map<String, AnnotationAttachmentAttributeValueNode> externalDocAttributes = this.listToMap(externalDocAnnotationAttachment);
            if (externalDocAttributes.containsKey("description")) {
                externalDocs.setDescription(this.getStringLiteralValue(externalDocAttributes.get("description")));
            }
            if (externalDocAttributes.containsKey("url")) {
                externalDocs.setUrl(this.getStringLiteralValue(externalDocAttributes.get("url")));
            }
            operation.setExternalDocs(externalDocs);
        }
    }
}
Also used : AnnotationAttachmentAttributeValueNode(org.ballerinalang.model.tree.expressions.AnnotationAttachmentAttributeValueNode) ExternalDocs(io.swagger.models.ExternalDocs) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode)

Example 23 with AnnotationAttachmentNode

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

the class SwaggerResourceMapper method getHttpMethods.

/**
 * Gets the http methods of a resource.
 * @param resource The ballerina resource.
 * @param useDefaults True to add default http methods, else false.
 * @return A list of http methods.
 */
private List<String> getHttpMethods(ResourceNode resource, boolean useDefaults) {
    Optional<? extends AnnotationAttachmentNode> responsesAnnotationAttachment = resource.getAnnotationAttachments().stream().filter(a -> null != this.httpAlias && this.httpAlias.equals(a.getPackageAlias().getValue()) && "resourceConfig".equals(a.getAnnotationName().getValue())).findFirst();
    Set<String> httpMethods = new LinkedHashSet<>();
    if (responsesAnnotationAttachment.isPresent()) {
        Map<String, AnnotationAttachmentAttributeValueNode> responsesAttributes = this.listToMap(responsesAnnotationAttachment.get());
        if (responsesAttributes.containsKey("methods") && responsesAttributes.get("methods").getValueArray().size() > 0) {
            for (AnnotationAttachmentAttributeValueNode methodsValue : responsesAttributes.get("methods").getValueArray()) {
                httpMethods.add(this.getStringLiteralValue(methodsValue));
            }
        }
    }
    if (httpMethods.isEmpty() && useDefaults) {
        // By default http methods is supported.
        httpMethods.add("GET");
        httpMethods.add("POST");
        httpMethods.add("HEAD");
        httpMethods.add("OPTIONS");
        httpMethods.add("DELETE");
        httpMethods.add("PUT");
    }
    return Lists.reverse(new ArrayList<>(httpMethods));
}
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) LinkedHashSet(java.util.LinkedHashSet) AnnotationAttachmentAttributeValueNode(org.ballerinalang.model.tree.expressions.AnnotationAttachmentAttributeValueNode)

Example 24 with AnnotationAttachmentNode

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

the class SwaggerResourceMapper method parseResourceConfigAnnotationAttachment.

/**
 * Parse 'ResourceConfig' annotation attachment and build a resource operation.
 * @param resource The ballerina resource definition.
 * @param operation The swagger operation.
 */
private void parseResourceConfigAnnotationAttachment(ResourceNode resource, Operation operation) {
    Optional<? extends AnnotationAttachmentNode> resourceConfigAnnotation = resource.getAnnotationAttachments().stream().filter(a -> null != swaggerAlias && this.swaggerAlias.equals(a.getPackageAlias().getValue()) && "ResourceConfig".equals(a.getAnnotationName().getValue())).findFirst();
    if (resourceConfigAnnotation.isPresent()) {
        Map<String, AnnotationAttachmentAttributeValueNode> configAttributes = this.listToMap(resourceConfigAnnotation.get());
        if (configAttributes.containsKey("schemes")) {
            List<Scheme> schemes = new LinkedList<>();
            for (AnnotationAttachmentAttributeValueNode schemesValue : configAttributes.get("schemes").getValueArray()) {
                if (null != Scheme.forValue(this.getStringLiteralValue(schemesValue))) {
                    schemes.add(Scheme.forValue(this.getStringLiteralValue(schemesValue)));
                }
            }
            operation.setSchemes(schemes);
        }
    // TODO: Implement security definitions.
    // this.createSecurityDefinitions(resourceConfigAnnotation.get().getAttributeNameValuePairs()
    // .get("authorizations"), operation);
    }
}
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) Scheme(io.swagger.models.Scheme) AnnotationAttachmentAttributeValueNode(org.ballerinalang.model.tree.expressions.AnnotationAttachmentAttributeValueNode) LinkedList(java.util.LinkedList)

Example 25 with AnnotationAttachmentNode

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

the class SwaggerResourceMapper method parseParametersInfoAnnotationAttachment.

/**
 * Parses the 'ParametersInfo' annotation and build swagger operation.
 * @param resource The ballerina resource definition.
 * @param operation The swagger operation.
 */
private void parseParametersInfoAnnotationAttachment(ResourceNode resource, Operation operation) {
    Optional<? extends AnnotationAttachmentNode> parametersInfoAnnotation = resource.getAnnotationAttachments().stream().filter(a -> null != swaggerAlias && this.swaggerAlias.equals(a.getPackageAlias().getValue()) && "ParametersInfo".equals(a.getAnnotationName().getValue())).findFirst();
    if (parametersInfoAnnotation.isPresent()) {
        Map<String, AnnotationAttachmentAttributeValueNode> infoAttributes = this.listToMap(parametersInfoAnnotation.get());
        if (infoAttributes.containsKey("value")) {
            List<? extends AnnotationAttachmentAttributeValueNode> parametersInfoValues = infoAttributes.get("value").getValueArray();
            for (AnnotationAttachmentAttributeValueNode parametersInfoValue : parametersInfoValues) {
                AnnotationAttachmentNode parameterInfoAnnotation = (AnnotationAttachmentNode) parametersInfoValue.getValue();
                Map<String, AnnotationAttachmentAttributeValueNode> parameterAttributes = this.listToMap(parameterInfoAnnotation);
                if (parameterAttributes.containsKey("name")) {
                    if (null != operation.getParameters()) {
                        for (Parameter parameter : operation.getParameters()) {
                            if (parameter.getName().equals(this.getStringLiteralValue(parameterAttributes.get("name")))) {
                                if (parameterAttributes.containsKey("description")) {
                                    parameter.setDescription(this.getStringLiteralValue(parameterAttributes.get("description")));
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
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) BodyParameter(io.swagger.models.parameters.BodyParameter) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) 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