question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Freeform query string parameters

See original GitHub issue

Original 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:closed
  • Created 8 years ago
  • Reactions:16
  • Comments:30 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
edudarcommented, May 20, 2016

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 with data_ (similar to HTML’s data- attributes) should be taken in to account.

1reaction
TroyMasscommented, Feb 5, 2016

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Craft Freeform 3.x - Filling Field Values from Query String
It's possible to fill field values from a GET query string. To do this, simply include field names that match the field handle(s)...
Read more >
Swashbuckle - Free form query parameters - The art of simplicity
I'm creating an api where an arbitrary number of query parameters can be added. By default when Swashbuckle generates the Swagger UI it...
Read more >
Free Form Parameters - Pyramid Help
'Free Input' or free form parameters enable users to enter input into the application using any acceptable value rather than choosing from a...
Read more >
How to document dynamic query parameter names in ...
Free-form query parameters can be described using OpenAPI 3.x, but not OpenAPI 2.0 (Swagger 2.0). The parameter must have type: object with ...
Read more >
Any way to describe a path so custom query paramet...
Looks like query params can only follow a fixed schema/structure. What we need is for the query string to be customized/flexible for certain ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found