How to make object query parameters
See original GitHub issueI’m trying to figure out how to make my ApiModelProperty decorators so that Swagger would work with objects as query parameters. My case is something like this:
class PersonQueryDto {
@ApiModelProperty({
type: QueryFilterDto,
isArray: true,
example: [{ field: 'age', comparator: 'gt', value: 25 }],
})
readonly filters: QueryFilterDto[];
}
class QueryFilterDto {
@ApiModelProperty({
type: 'string',
})
readonly field: string;
@ApiModelProperty({
type: 'string',
})
readonly comparator: string;
@ApiModelProperty({
type: 'any',
})
readonly value: any;
}
The swagger UI, however, ends up looking like this
And it doesn’t know how to deserialise the object passed.
What’s the trick?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:3
- Comments:12 (1 by maintainers)
Top Results From Across the Web
How to Turn an Object into Query String Parameters in ...
As a JavaScript developer, you'll often need to construct URLs and query string parameters. One sensible way to construct query string ...
Read more >Query-string encoding of a JavaScript object - Stack Overflow
This method converts a JavaScript object into a URI query string. It also handles nested arrays and objects (in Ruby on Rails and...
Read more >How to convert an object to a query string using JavaScript
To convert an object to a query string in older browsers: Use the Object.keys() method to get all object's keys as an array....
Read more >Objects in query params - Medium
Objects in query params. There is a debate over how objects should be represented in the “query” part of URIs. Some people prefer...
Read more >Convert an Object to a Query String using JavaScript
Use the URLSearchParams() constructor to convert an object to a query string, e.g. new URLSearchParams(obj).toString() . Calling the toString() method on ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You can do something like this:
Highjacking this thread since it helped me figure out how to do a similar task with a simple object with full Swagger support:
Example:
?filters[name]=Marko&filters[age]=21
Full example here: https://gist.github.com/MarZab/c6311f83dec6401966e847c55d81a9bb