mirror of
https://github.com/opennetworkinglab/onos.git
synced 2026-05-04 11:51:43 +02:00
Swagger annotations library support
Change-Id: Ib2843e541580c4accca2a6411449f77a7ea7a02b
This commit is contained in:
parent
a9ff68bb8d
commit
ac691981f6
8
lib/BUCK
8
lib/BUCK
@ -1519,3 +1519,11 @@ remote_jar (
|
||||
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' ],
|
||||
)
|
||||
@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
@ -62,6 +62,7 @@ public class SwaggerGenerator {
|
||||
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 @@ public class SwaggerGenerator {
|
||||
|
||||
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 @@ public class SwaggerGenerator {
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user