Search in sources :

Example 6 with ResourceNode

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

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

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

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

ResourceNode (org.ballerinalang.model.tree.ResourceNode)9 Lists (com.google.common.collect.Lists)5 ExternalDocs (io.swagger.models.ExternalDocs)5 Model (io.swagger.models.Model)5 ModelImpl (io.swagger.models.ModelImpl)5 Operation (io.swagger.models.Operation)5 Path (io.swagger.models.Path)5 RefModel (io.swagger.models.RefModel)5 Response (io.swagger.models.Response)5 Scheme (io.swagger.models.Scheme)5 Swagger (io.swagger.models.Swagger)5 BodyParameter (io.swagger.models.parameters.BodyParameter)5 Parameter (io.swagger.models.parameters.Parameter)5 PathParameter (io.swagger.models.parameters.PathParameter)5 ArrayProperty (io.swagger.models.properties.ArrayProperty)5 BooleanProperty (io.swagger.models.properties.BooleanProperty)5 IntegerProperty (io.swagger.models.properties.IntegerProperty)5 Property (io.swagger.models.properties.Property)5 StringProperty (io.swagger.models.properties.StringProperty)5 ArrayList (java.util.ArrayList)5