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);
}
}
}
}
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));
}
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);
}
}
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")));
}
}
}
}
}
}
}
}
}
Aggregations