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.

Automatically detect optional @Query

See original GitHub issue

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 the required 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 😃

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:29
  • Comments:22 (5 by maintainers)

github_iconTop GitHub Comments

29reactions
jaufgangcommented, Apr 18, 2018

Keep in mind that a param could also be optional if it has a default value rather than a ?. For example:

@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 Number, but this is also not reflected in the swagger docs if we omit the @ApiImplicitQuery({type: Number}). In fact, the try it out form field will currently not accept a number without the type specified in the ApiImplicitQuery decorator.

It would be ideal if all of these things could be inferred by reflection so that you would only need to use the @ApiImplicitQuery decorator to add the description.

23reactions
mjgp2commented, Jan 9, 2018

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 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Query Parameters - FastAPI
As query parameters are not a fixed part of a path, they can be optional and can have default values. In the example...
Read more >
Optional query string parameters in ASP.NET Web API
If I set all of the parameters the method gets called; furthermore it starts with Get so it is automatically bound with the...
Read more >
Using schema auto-detection | BigQuery - Google Cloud
Schema auto-detection enables BigQuery to infer the schema for CSV, JSON, or Sheets data. ... SSSSSS] (the fractional-second component is optional).
Read more >
Retrofit — Optional Query Parameters - Future Studio
Depending on the API design, the sort parameter might be optional. In case you don't want to pass it with the request, just...
Read more >
Testing Python Connexion Optional Query Parameter Names
It allows you to make the openAPI specification define input validation that is automatically enforced, maps API endpoints to a named function ...
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