dropwizard-swagger
dropwizard-swagger copied to clipboard
I am trying out dropwizard-swagger, and with both 0.5.1 and 0.5.2-SNAPSHOT getting JS error
I am trying out a basic HelloWorld Dropwizard app, using Dropwizard 0.7.1. The UI keeps throwing a javascript error:
Here is what the raw API output for my HelloWorld service looks like:
{"apiVersion":"0.0","swaggerVersion":"1.2","basePath":"http://localhost:8080","resourcePath":"/hello-world","produces":["application/json"],"apis":[{"path":"/hello-world","operations":[{"method":"GET","summary":"Say hello","notes":"More notes about this method","type":"Saying","nickname":"sayHello","authorizations":{},"parameters":[{"name":"name","description":"Name to reply back with","required":false,"items":{"type":"string"},"paramType":"query"}],"responseMessages":[{"code":400,"message":"Invalid ID supplied"}]}]}],"models":{"Saying":{"id":"Saying","properties":{"id":{"type":"integer","format":"int64"},"content":{"type":"string"}}}}}
Any ideas on debugging this?
Thanks for trying it out. Is that error stopping you from testing the resource?
Just in case, I put together a sample project which you can try out -> https://github.com/federecio/dropwizard-swagger-sample-app/
I think I figured out the issue! The use of the <Optional> with a String causes the swagger ui to fail. I am putting as a comment a diff to your example project that demonstrates the problem.
diff --git a/src/main/java/io/federecio/dropwizard/swagger/sample/SampleResource.java b/src/main/java/io/federecio/dropwizard/swagger/sample/SampleResource.java
index 3d840c5..bd2a999 100644
--- a/src/main/java/io/federecio/dropwizard/swagger/sample/SampleResource.java
+++ b/src/main/java/io/federecio/dropwizard/swagger/sample/SampleResource.java
@@ -1,10 +1,12 @@
package io.federecio.dropwizard.swagger.sample;
+import com.google.common.base.Optional;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
+import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
/**
@@ -16,7 +18,7 @@ public class SampleResource {
@GET
@ApiOperation("Sample endpoint")
- public Response get() {
- return Response.ok(new SamplePojo("Federico", 1234)).build();
+ public Response get(@QueryParam("name") Optional<String> name) {
+ return Response.ok(new SamplePojo(name.get(), 1234)).build();
}
}
It seems that the Swagger UI files bundled in the project don't support Optional parameters. I will check if the latest Swagger UI distribution handles Optional parameters.
I stumbled apon the same bug, any news?
seems related to https://github.com/wordnik/swagger-core/issues/609
looks like we need to wait for swagger-core 1.5.0
ok, found a workaround till the official patch:
- Implement a SwaggerSpecFilter that will filter out ApiParam on its access attribute (here, access="hidden" means filter out)
public class GuavaOptionalHackFilter implements SwaggerSpecFilter {
@Override
public boolean isOperationAllowed(Operation operation, ApiDescription apiDescription, Map<String, List<String>> stringListMap, Map<String, String> stringStringMap, Map<String, List<String>> stringListMap2) {
return true;
}
@Override
public boolean isParamAllowed(Parameter parameter, Operation operation, ApiDescription apiDescription, Map<String, List<String>> stringListMap, Map<String, String> stringStringMap, Map<String, List<String>> stringListMap2) {
return !parameterAccessValueIsHidden(parameter);
}
public static boolean parameterAccessValueIsHidden(Parameter parameter) {
return parameter.paramAccess().isDefined() && parameter.paramAccess().get().equalsIgnoreCase("hidden");
}
}
- actually filter out the params that are of type Optional
public Response getAbout(@ApiParam(access = "hidden") @QueryParam("qparam") Optional<String> qparam) {
return Response.ok().entity(qparam).build();
}
- use the @ApiImplicitParam annotation to provide what you want the parameters to look like
@GET
@Timed
@ApiImplicitParams(value = {@ApiImplicitParam(name = "qparam", value = "Query Parameter", dataType = "string", required = true, paramType = "query")})
public Response getAbout(@ApiParam(access = "hidden") @QueryParam("qparam") Optional<String> qparam) {
return Response.ok().entity(qparam).build();
}
- register the filter in your app run method:
@Override
public void run(Configuration configuration, Environment environment) throws Exception {
environment.jersey().register(new Resource());
swaggerDropwizard.onRun(configuration, environment);
com.wordnik.swagger.config.FilterFactory.setFilter(new GuavaOptionalHackFilter());
}