1

I've been using swagger for some time now and I have now ran into a problem where the generated request URL puts my optional parameters in the request. This is not happening on other projects I have that are using the same annotations, swagger version, and swagger maven version.

Here is a code snippet inside my @Controller

private static final String BY_USERNAME = "byUsername";
private static final String BY_NAME = "byName";
private static final String BY_PHONENUMBER = "byPhoneNumber";
private static final String BY_USERTYPENAME = "byUserTypeName";
private static final String BY_ACTIVE = "byActive";
private static final String BY_ACTIVE_AND_AVAILABLE = "byActiveAndAvailable";
@ApiOperation(value = "Read User(s)", notes = "notes removed so post wont be so long", consumes = "application/x-www-form-urlencoded")
@RequestMapping(value = "", method = RequestMethod.GET, consumes = "application/x-www-form-urlencoded")
@ApiImplicitParam(value = "Authorization Token", name = TokenAuthenticationService.HEADER_STRING, dataType = "String", paramType = "header")
@ApiResponses(value = { @ApiResponse(message = "successful read call", code = 200), @ApiResponse(message = "bad parameter provided", code = 400) })
public Page<User> readUser(
                @ApiParam(name = "queryType", value = "type of query", allowableValues = BY_USERNAME + "," + BY_NAME + "," + BY_PHONENUMBER + ","
                                + BY_USERTYPENAME + "," + BY_ACTIVE + "," + BY_ACTIVE_AND_AVAILABLE) @RequestParam(value = "queryType", required = true) String queryType,
                @ApiParam(name = "username", value = "username of user") @RequestParam(value = "username", required = false) String username,
                @ApiParam(name = "name", value = "name of user(s)") @RequestParam(value = "name", required = false) String name,
                @ApiParam(name = "phoneNumber", value = "phone number of user(s)") @RequestParam(value = "phoneNumber", required = false) String phoneNumber,
                @ApiParam(name = "userTypeName", value = "user type name of user(s)") @RequestParam(value = "userTypeName", required = false) String userTypeName,
                @ApiParam(name = "active", value = "active status of user(s)") @RequestParam(value = "active", required = false) Boolean active,
                @ApiParam(name = "available", value = "available status of user(s)") @RequestParam(value = "available", required = false) Boolean available,
                Pageable pageable) {

This is very basic swagger annotations. The problem is the resulting URL in the swagger-ui looks like this: Swagger Bad URL

When I attempt to use it, it puts these inside the REST request. REST request with optional params

This of course causes spring to throw exceptions about invalid characters Spring Exception

I tried searching all over the internet, swagger pages, and here on stackoverflow and I could not find any mention of this error. The very weird thing is, if I change the RequestMethod to be POST it works as it should. Seems to be a bug in swagger-ui handling optional parameters. Anyone else ran into this? Is there a fix besides making the request method POST?

EDIT: The "consumes = "application/x-www-form-urlencoded"" was not in my original code. I've been debugging this and adding stuff to try and make it work lol.

Versions used:

  • springfox-swagger2 version 2.6.1
  • springfox-swagger-ui version 2.6.1
Dilip Krishnan
  • 5,417
  • 3
  • 37
  • 53
GSUgambit
  • 4,459
  • 6
  • 25
  • 31
  • 2
    Possible duplicate of [Using @RequestParam annotated method with swagger ui](https://stackoverflow.com/questions/33377388/using-requestparam-annotated-method-with-swagger-ui) – Helen Jun 03 '17 at 18:28

1 Answers1

2

What version of the swagger-ui are you using?

Since you have enableUrlTemplating(true) in your docket you need to use the appropriate implementation of swagger-ui that supports it.

For Gradle:

compile group: 'io.springfox.ui', name: 'springfox-swagger-ui-rfc6570', version: '1.0.0'

For Maven:

<!-- https://mvnrepository.com/artifact/io.springfox.ui/springfox-swagger-ui-rfc6570 -->
<dependency>
    <groupId>io.springfox.ui</groupId>
    <artifactId>springfox-swagger-ui-rfc6570</artifactId>
    <version>1.0.0</version>
</dependency>

Docket supports url templating (see #21), however it is still in incubation and not officially supported by the swagger 2.0 specification. So the choices you have are:

  1. make enableUrlTemplating(false). In which case everything will work with the exception that if you have multiple paths with different query parameters, only one of them would show up.

e.g. if you have the following urls http://example.org/customers?firstName=A&lastName=B findCustomersByName http://example.org/customers?email=a@b.com findCustomersByEmail

Only one of them will show up.

  1. Keep enableUrlTemplating(true) but change the swagger-ui you're using to the one above.
Dilip Krishnan
  • 5,417
  • 3
  • 37
  • 53