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:
When I attempt to use it, it puts these inside the REST request.
This of course causes spring to throw exceptions about invalid characters
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