Error in using swagger annotations for non-primitive query params

834 views
Skip to first unread message

Sutirtha Datta

unread,
Jan 24, 2013, 3:37:51 PM1/24/13
to swagger-sw...@googlegroups.com
I was catching up with DropWizard using their demo getting started application, and wanted to hook up swagger to it.
DropWizard uses a lot of Guava abstractions in places, and the demo app has used Optional from guava for a query param.

public Saying sayHello(@ApiParam(value = "Name to greet to", required = true) @QueryParam("name") Optional<String> name) {
        return new Saying(counter.incrementAndGet(), String.format(template, name.or(defaultName)));
 }

Because of it being non-primitive, the UI is throwing an error as dataType is not being populated for this param. I checked the wiki which said to add custom models in your specification for such cases, but the spec is being auto-generated using the annotations. I wanted to know if there is someone to get around this.

Ben Lisbakken

unread,
May 30, 2013, 8:01:47 PM5/30/13
to swagger-sw...@googlegroups.com
Is there any followup on this? I have the same issue with Optional<String> parameters.

Thanks,
Ben
Message has been deleted
Message has been deleted
Message has been deleted

Damian Pawlowski

unread,
Feb 26, 2014, 10:23:25 AM2/26/14
to swagger-sw...@googlegroups.com
It affects all method parameters that are type of com.google.common.base.Optional

tony tam

unread,
Feb 26, 2014, 10:30:24 AM2/26/14
to swagger-sw...@googlegroups.com
Please post a sample test in the swagger-core project and we'll see if support can be easily added.  I'd suggest following something like this:


which you can run as such:

mvn test -Dtest=SnakeCaseConverterTest

Daniel Scott

unread,
Jun 30, 2014, 6:06:25 AM6/30/14
to swagger-sw...@googlegroups.com
Anything I can do to help with this? It's affecting me.

Dan

Ron

unread,
Jun 30, 2014, 6:21:08 AM6/30/14
to swagger-sw...@googlegroups.com
As Tony mentioned, a sample test would help.

As a workaround, for now, you can add the parameters manually using @ApiImplicitParam.

I'm not sure how easy it would be to overcome this due to type erasure, but we'll see.


--
You received this message because you are subscribed to the Google Groups "Swagger" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Daniel Scott

unread,
Jun 30, 2014, 8:36:04 AM6/30/14
to swagger-sw...@googlegroups.com
Thanks, I'll try, but I'm afraid I don't know Scala at all.

The @ApiImplicitParam doesn't seem to be working. My generated JSON contains an 'items' array:

"parameters":[
  {"name":"name",
   "description":"description",
   "required":false,
   "items":{"type":"string"},
   "paramType":"query"
   }]

Is this correct? The UI gives an error because parameters.type is null.

Thanks,

Dan
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggersocket+unsub...@googlegroups.com.

Ron

unread,
Jun 30, 2014, 8:40:02 AM6/30/14
to swagger-sw...@googlegroups.com
According to this, you declared the parameter to being an array.
Can you show me your @ApiImplicitParam declaration?


To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.

Dan Scott

unread,
Jun 30, 2014, 9:16:11 AM6/30/14
to swagger-sw...@googlegroups.com
I tried both:

@ApiImplicitParam(name = "name", value = "Network name", dataType = "string", paramType = "query")

and

@ApiImplicitParams(value = {@ApiImplicitParam(name = "name", value = "Network name", dataType = "string", paramType = "query")})

My method definition:

public Collection<NetworkView> get(@QueryParam("name") final Optional<String> name)

Thanks,

Dan



--
You received this message because you are subscribed to a topic in the Google Groups "Swagger" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/swagger-swaggersocket/i_X_mDdwIWQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to swagger-swaggers...@googlegroups.com.

Ron

unread,
Jun 30, 2014, 9:30:10 AM6/30/14
to swagger-sw...@googlegroups.com
@ApiImplicitParam should come with @ApiImplicitParams.

I'm a little curious now though. Can you try changing the name of the implicit parameter to something other than "name"? I wonder if that would affect the output.

Dan Scott

unread,
Jun 30, 2014, 9:45:48 AM6/30/14
to swagger-sw...@googlegroups.com
I tried with:

@ApiImplicitParam(name = "vpnName", value = "Virtual Network name", dataType = "string", paramType = "query")
public Collection<Environment.EnvironmentView> get(@QueryParam("vpnName") final Optional<String> vpnName)

Same JSON generated:

"parameters":[{"name":"vpnName","required":false,"items":{"type":"string"},"paramType":"query"}]

The 'value' doesn't seem to be picked up either.... I could be missing something obvious here - I've only been using swagger for a day or two.

My other methods are annotated and working correctly, so I can't be too far wrong.

Thanks,

Dan

Ron

unread,
Jun 30, 2014, 9:49:15 AM6/30/14
to swagger-sw...@googlegroups.com

Which version of swagger-core do you use?

Dan Scott

unread,
Jun 30, 2014, 9:56:08 AM6/30/14
to swagger-sw...@googlegroups.com
I'm using DropWizard, same as the OP. The dropwizard-swagger bundle provides the integration:


It packages swagger-core 1.3.2. Is it worth trying an upgrade to the latest version (1.3.6, I think)?

Dan

Ron

unread,
Jun 30, 2014, 9:57:56 AM6/30/14
to swagger-sw...@googlegroups.com

Wouldn't hurt trying. I don't recall specifically if there were related bugs.

Dan Scott

unread,
Jun 30, 2014, 9:59:40 AM6/30/14
to swagger-sw...@googlegroups.com
OK, Will take me a little while to rebuild the module. Will let you know how it turns out.

Thanks,

Dan

Dan Scott

unread,
Jun 30, 2014, 10:19:44 AM6/30/14
to swagger-sw...@googlegroups.com
Hmm, that was quicker than I thought. I rebuilt the dropwizard package using 1.3.6. No change. Still get the 'items' entry.

Ron

unread,
Jun 30, 2014, 11:09:18 AM6/30/14
to swagger-sw...@googlegroups.com
Okay, this is very odd. I just retested it and it works just fine. Even includes the value/description.

I suspect the issue is possibly related with dropwizard 0.7+ (though I can't think of a reason why).
We've had complaints about that, but I haven't had a chance to look into it yet.

We have a dropwizard 0.6 sample that works fine.

Dan Scott

unread,
Jun 30, 2014, 11:16:41 AM6/30/14
to swagger-sw...@googlegroups.com
OK, thanks. Do you have a link to the dropwizard example which uses the @ApiImplicitParam annotation?


but it doesn't use the same annotation.

One thought. Do I have to remove the @QueryParam annotation, and pull the query parameter from the request object?

I'd also like to get this fixed 'properly' if possible, any hints on how to get a test written which shows the Guava Optional dataType not working?

Thanks,

Dan

Ron

unread,
Jun 30, 2014, 11:33:36 AM6/30/14
to swagger-sw...@googlegroups.com
Okay, I didn't test the @ApiImplicitParam with our dropwizard sample before, but I did now and it works fine (using the sample you linked to).
Just added @ApiImplicitParams({@ApiImplicitParam(name = "test", value = "my description", paramType = "query", dataType = "string")}) to the getPetById operation.

The idea is that if/once that works, you can filter out the @QueryParam (no need to change application functionality), but let's get to that after the @ApiImplicitParam works properly.

As for the test, I'm not really a scala developer myself, so I can't help you much there. Perhaps Tony could give some insight on the matter.

A sample (mavenized) project with the specific use case may help.

Dan Scott

unread,
Jun 30, 2014, 12:13:15 PM6/30/14
to swagger-sw...@googlegroups.com
Hmm, I investigated a little more, and it's possible that the correct JSON may be there, but there is additional invalid JSON being generated which is causing the problem. Could you let me know how to filter out the @QueryParam generated JSON?

Is it possible that the Optional<String> is being interpreted as a collection? Which is why it's generating the 'type' array?

I have a sample project now, which I could put on github if this would help?

Dan

Ron

unread,
Jun 30, 2014, 12:18:20 PM6/30/14
to swagger-sw...@googlegroups.com
Using @ApiParam on the @QueryParam, you can use the access field and set it to something like "internal".
Then add a filter such as this with the following logic - https://github.com/wordnik/swagger-core/blob/8abac0ca61ca5a497adf481256ac2b79d18c34f0/samples/java-jaxrs/src/main/java/com/wordnik/swagger/sample/util/ApiAuthorizationFilterImpl.java#L60

I seriously doubt Optional<String> is being interpreted as a collection, especially since you change the implicit parameter's name and that one changed remained an array.

As for the sample project, sure, a github link would work.

Dan Scott

unread,
Jun 30, 2014, 12:23:49 PM6/30/14
to swagger-sw...@googlegroups.com
OK, thanks. I'll keep looking.

Here's the sample project. Hope it's OK.

Ron

unread,
Jun 30, 2014, 12:32:59 PM6/30/14
to swagger-sw...@googlegroups.com
I just tried running it and here are 3 points:
  1. Indeed, the Optional<String> is being converted to an array.
  2. The @ApiImplicitParam is of type string as expected.
  3. The @ApiImplicitParam's description is also displayed properly.

So there's probably a bug with regards to the Optional interpretation like you said, and you can still override it using the previously suggested way (I agree, it's ugly, but it would work).

There's definitely room to open an issue about it on swagger-core.

Dan Scott

unread,
Jun 30, 2014, 4:16:40 PM6/30/14
to swagger-sw...@googlegroups.com
Thanks for your help. Issue submitted:


Let me know how I can help.
Reply all
Reply to author
Forward
0 new messages