-
Notifications
You must be signed in to change notification settings - Fork 484
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Automatically detect optional @Query #30
Comments
One option will be to use a class as your query parameters: class MyParams {
@ApiModelProperty()
prop: number;
@ApiModelPropertyOptional()
optionalProp: number;
}
@Controller('MyCtrl')
class MyCtrl {
@Get()
async getList(@Query() params: MyParams) {
...
}
} |
Sure, good idea, but I would still rather have something where the API properties can be inferred by reflection rather than more decorators as it becomes much more verbose 👍 |
Keep in mind that a param could also be optional if it has a default value rather than a @Controller("transactions")
export class TransactionsController {
@ApiImplicitQuery({
name: "limit",
description: "The maximum number of transactions to return",
required: false,
type: Number
})
@Get("recent")
getRecentTransactions(@Query("limit", new ToIntPipe()) limit: Number = 10) {
...
}
} It would be ideal if swagger could detect the default value using reflection and document that the param is optional as well as the default values. Also, note that in this example the param type is explicitly declared as It would be ideal if all of these things could be inferred by reflection so that you would only need to use the |
@jaufgang + 1 |
+1 |
Reflection doesn't give us any information about optionals/default values. We'd have to use a new script that makes use of AST to properly indicate whether something is required or not. |
So how do we define optional param? |
@kunokdev, you specify an optional param by setting |
The plugin released in version 4.0.0 will automatically detect optional properties in your class https://docs.nestjs.com/recipes/swagger. However, it still doesn't check controller's methods signature (something to add in the future). |
You mentioned the custom pipe While it wouldn't be a big deal to write a custom pipe that doesn't throw (as opposed to |
@prinzdezibel and everyone else looking for a solution — there is a documented way to chain multiple pipes where one of them can inject a default value. The DefaultValue pipe must precede ParseIntType or whatever other pipe you're using. Here's a direct link to the section that documents this: https://docs.nestjs.com/pipes#providing-defaults But it's true that it took me a while to find it. |
Thanks @mareksuscak. Closing |
@nartc - that's not actually the answer to the original question, I don't think you should have closed it :) I think as @kamilmysliwiec says, "it still doesn't check controller's methods signature (something to add in the future)." |
@kamilmysliwiec does reopening means that is being worked on? |
Maybe to add But the best way of course to use getData(@Query('limit') limit?: number) |
@tamtakoe It doesn't work for me. |
@jameskuizon1315 It doesn't work. It was an idea for contributors |
I'd like to +1 that supporting using |
Wow, I was quite surprised and confused why all my optional query parameters were omitted from the swagger document. It's a shame that so great framework doesn't have support for such a pretty common thing. I tried to invastigate it further. The problem is, that |
This is a TypeScript limitation. Not much we can do on the framework side. |
Note to anyone checking this out today, |
You use |
I tried the @habogay's way but the api always return 200 without any content, how can I fix it ? |
is there any solution to not mark required on swagger docs, i tried myProp? not working |
@ermarkar it looks like only the solution above is currently working. i failed to found anything else
|
This seems not to work with nestjs swagger plugin . Snippet of my controller:
Looks like the plugin won't override or recognize @apiquery(), if I select the optional "status" shown on the upper image, it will take no effect on my request. It works if I fill the required "status", but it is not meant to be required. Am I doing something wrong? |
Is it possible to add options object to Something along the lines of: getAll(@Query(`someQuery`, { optional: true }) someQuery?: string)
getAllWithTransform(@Query(`someQuery`, new Pipe(...), { optional: true }) someQuery?: number) That would reduce the amount of decorators needed, and while not perfect, at least should improve the overall experience. |
@brunodmn |
Thank you @DrRoot-github, adding the name makes it work (override default plugin behavior). This is not a required param by the ApiQuery though. |
This issue is still active.
This workaround helped in my situation. Would be more beautiful if just using |
+100 (please just make ? work!) |
Am I right in thinking that you could look for a
?
on the query function parameter to denote that it is optional, and pick this up to set therequired
value on the swagger document?It would be nicer than having to add decorators like
@ApiImplicitQuery({name:'offset',required:false})
to the method I think. What do you reckon?Thanks :)
The text was updated successfully, but these errors were encountered: