Freeform query string parameters
See original GitHub issueOriginal Issue: https://github.com/swagger-api/swagger-ui/issues/1185
Some API are designed to allow for some known parameters as well as any freeform parameters to be applied.
For example: say I have an API for users. A user has a firstName, lastName, userName, email, and many other fields. I want to create an API endpoint that would allow for me to do any combinations of GET /users?firstName=joe&lastName=smith and return matches for this. Instead of defining 2^N endpoints for every combination of the fields on the object, I define one endpoint which accepts any number of parameters and filters results based on it.
To do this (using Java - Spring MVC)
@Autowired
HttpServletRequest request;
@RequestMapping("/users", method = RequestMethod.GET)
@ApiOperation(value = "Find users filtered", freeformParams = true)
public List<User> findUsersFiltered(
@RequestParam(value = "limit") @ApiParam(value = "Limit size") final String limit) {
Map<String, String[]> filters = request.getParameterMap();
// do stuff with the filters...
}
As you can see, I added a freeformParams = true
which would tell swagger-ui that additional query params are allowed. On swagger-ui, this would add boxes where you could type the query string key and value. These params are in addition to any existing parameters defined by @ApiParam
Key | Value |
---|---|
limit | 10 |
firstName | joe |
lastName | smith |
add another | value |
This would generate a request such as GET /users?limit=10&firstName=joe&lastName=smith
Issue Analytics
- State:
- Created 8 years ago
- Reactions:16
- Comments:30 (9 by maintainers)
Top GitHub Comments
My use-case is a bit different from described above but still somewhat falls into
freeFormParams
category. I do allow prefixed parameters on couple methods of my API. I know that any parameter with name that starts withdata_
(similar to HTML’sdata-
attributes) should be taken in to account.Yes this is pretty edge case, but critical if you are building dynamic APIs with flexible schemas behind them. We use JSONpath syntax for nested attributes. So in your example it would be
?name.first=Jason
. Slashes are too much trouble in URLs