Swagger annotations library support
Change-Id: Ib2843e541580c4accca2a6411449f77a7ea7a02b
diff --git a/lib/BUCK b/lib/BUCK
index 8b6dc1d..5436710 100644
--- a/lib/BUCK
+++ b/lib/BUCK
@@ -1519,3 +1519,11 @@
visibility = [ 'PUBLIC' ],
)
+remote_jar (
+ name = 'swagger-annotations',
+ out = 'swagger-annotations-1.5.16.jar',
+ url = 'mvn:io.swagger:swagger-annotations:jar:1.5.16',
+ sha1 = '935f1f2fed2cbdd7a0513981d6c53201e21155f4',
+ maven_coords = 'io.swagger:swagger-annotations:1.5.16',
+ visibility = [ 'PUBLIC' ],
+)
\ No newline at end of file
diff --git a/lib/deps.json b/lib/deps.json
index 071d95f..f0f796c 100644
--- a/lib/deps.json
+++ b/lib/deps.json
@@ -270,6 +270,7 @@
"google-instrumentation-0.3.0": "mvn:com.google.instrumentation:instrumentation-api:0.3.0",
"bcpkix-jdk15on": "mvn:org.bouncycastle:bcpkix-jdk15on:1.58",
"bcprov-jdk15on": "mvn:org.bouncycastle:bcprov-jdk15on:1.58",
- "hamcrest-optional": "mvn:com.spotify:hamcrest-optional:1.1.0"
+ "hamcrest-optional": "mvn:com.spotify:hamcrest-optional:1.1.0",
+ "swagger-annotations": "mvn:io.swagger:swagger-annotations:1.5.16"
}
}
diff --git a/tools/build/buck-plugin/src/main/java/org/onosproject/onosjar/SwaggerGenerator.java b/tools/build/buck-plugin/src/main/java/org/onosproject/onosjar/SwaggerGenerator.java
index 2f54899..40ba70d 100644
--- a/tools/build/buck-plugin/src/main/java/org/onosproject/onosjar/SwaggerGenerator.java
+++ b/tools/build/buck-plugin/src/main/java/org/onosproject/onosjar/SwaggerGenerator.java
@@ -62,6 +62,7 @@
private static final String CONSUMES = "javax.ws.rs.Consumes";
private static final String JSON = "MediaType.APPLICATION_JSON";
private static final String OCTET_STREAM = "MediaType.APPLICATION_OCTET_STREAM";
+ private static final String RESPONSES = "io.swagger.annotations.ApiResponses";
private final List<File> srcs;
private final List<File> resources;
@@ -258,11 +259,10 @@
processConsumesProduces(methodNode, "consumes", consumes);
processConsumesProduces(methodNode, "produces", produces);
- if (tag == null || ((method.toLowerCase().equals("post") || method.toLowerCase().equals("put"))
- && !(tag.getParameters().size() > 1))) {
- addResponses(methodNode, tag, false);
+ if (tag == null || !(tag.getParameters().size() > 1)) {
+ addResponses(javaMethod, methodNode, tag, false);
} else {
- addResponses(methodNode, tag, true);
+ addResponses(javaMethod, methodNode, tag, true);
}
ObjectNode operations = pathMap.get(fullPath);
@@ -327,24 +327,49 @@
}
}
- // Temporary solution to add responses to a method
- private void addResponses(ObjectNode methodNode, DocletTag tag, boolean responseJson) {
+ private Optional<JavaAnnotation> getResponsesAnnotation(JavaMethod javaMethod) {
+ return javaMethod.getAnnotations().stream().filter(
+ a -> a.getType().getName().equals(RESPONSES)
+ ).findAny();
+ }
+
+ // TODO: add json schema for responses
+ private void addResponses(JavaMethod javaMethod, ObjectNode methodNode, DocletTag tag, boolean responseJson) {
ObjectNode responses = mapper.createObjectNode();
methodNode.set("responses", responses);
- ObjectNode success = mapper.createObjectNode();
- success.put("description", "successful operation");
- responses.set("200", success);
- if (tag != null && responseJson) {
- ObjectNode schema = mapper.createObjectNode();
- tag.getParameters().stream().forEach(
- param -> schema.put("$ref", "#/definitions/" + param));
- success.set("schema", schema);
- }
+ Optional<JavaAnnotation> responsesAnnotation = getResponsesAnnotation(javaMethod);
- ObjectNode defaultObj = mapper.createObjectNode();
- defaultObj.put("description", "Unexpected error");
- responses.set("default", defaultObj);
+ if (responsesAnnotation.isPresent()) {
+ Object annotationsObj = responsesAnnotation.get().getNamedParameter("value");
+ if (annotationsObj != null && annotationsObj instanceof List) {
+ List<JavaAnnotation> responseAnnotation = (List<JavaAnnotation>) annotationsObj;
+ responseAnnotation.forEach(
+ javaAnnotation -> {
+ ObjectNode response = mapper.createObjectNode();
+ response.put("description",
+ String.valueOf(javaAnnotation.getNamedParameter("message"))
+ .replaceAll("^\"|\"$", ""));
+ responses.set(String.valueOf(javaAnnotation.getNamedParameter("code")), response);
+ }
+ );
+ }
+ } else {
+ ObjectNode success = mapper.createObjectNode();
+ success.put("description", "successful operation");
+ responses.set("200", success);
+
+ ObjectNode defaultObj = mapper.createObjectNode();
+ defaultObj.put("description", "Unexpected error");
+ responses.set("default", defaultObj);
+
+ if (tag != null && responseJson) {
+ ObjectNode schema = mapper.createObjectNode();
+ tag.getParameters().stream().forEach(
+ param -> schema.put("$ref", "#/definitions/" + param));
+ success.set("schema", schema);
+ }
+ }
}
// Checks if the annotations has a value of JSON and returns the string