Giter Club home page Giter Club logo

Comments (6)

federecio avatar federecio commented on August 15, 2024

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/

from dropwizard-swagger.

epugh avatar epugh commented on August 15, 2024

I think I figured out the issue! The use of the 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();
     }
 }

from dropwizard-swagger.

federecio avatar federecio commented on August 15, 2024

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.

from dropwizard-swagger.

looztra avatar looztra commented on August 15, 2024

I stumbled apon the same bug, any news?

from dropwizard-swagger.

looztra avatar looztra commented on August 15, 2024

seems related to swagger-api/swagger-core#609

looks like we need to wait for swagger-core 1.5.0

from dropwizard-swagger.

looztra avatar looztra commented on August 15, 2024

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());

    }

from dropwizard-swagger.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.