Support optional path parameters with default value
See original GitHub issueAs of today, Open API spec asserts that a path parameter should always be required. This is a good assertion. However, we have some services in Azure that have optional path parameters with a default value. There has been an increasing number of requests from service teams to support this.
What do you think about the following assertion?
If the parameter is in “path”, required field must be specified. If required is false, it must be supported with a default. Else, required filed should be true.
This provides more flexibility apart from taking care of the current assertion.
For example:
- “/kevault/{KeyType}”
{
"name": "KeyType",
"in": "path",
"required": false,
"default": "symmetric",
"type": "string",
"description": "The type of key."
}
Can this be included in the 3.0 version of the Open API Spec?
Issue Analytics
- State:
- Created 7 years ago
- Comments:17 (6 by maintainers)
Top Results From Across the Web
Describing Parameters - Swagger
Default Parameter Values. Use the default keyword in the parameter schema to specify the default value for an optional parameter. The default value...
Read more >Spring Optional Path Variables | Baeldung
By default, Spring will try to bind all parameters annotated with @PathVariable in a handler method with the corresponding variables in the URI ......
Read more >React Router with optional path parameter - Stack Overflow
v6: React router seems to have dropped support for optional path parameters in v6, see github.com/remix-run/react-router/issues/…
Read more >Using parameters in routes - Drupal Wiki
Optional parameters. Parameters on routes can be omitted when a default value for the parameter is supplied. Imagine you have a form controller ......
Read more >Optional parameters in Web API Attribute Routing
In this article, I am going to discuss Optional Parameters in Web API Attribute Routing with some examples. You can make a URI...
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
@fehguy I disagree with your non-determinism comment. I argue that path resolution is non-deterministic by default. For example:
Is deterministic because it always processes the same logic to reliably retrieve the latest blog post. It’s also non-deterministic because there’s no guarantee that between calls the same blog post will be retrieved.
Are deterministic because they always return the exact same post respectively. However, you’re nuts if you think I’m going to specify a path for every post that exists (extreme example) <- and revise my spec every time a new blog post is created.
OAS is a specification for describing an API. It is not an example or reference for how an API should be implemented nor does it have anything to do with how path resolution is implemented.
I implement my API and I wish to document it. I wish to document it in a standard way to that doc generation tools can deterministically (reliably) generate the docs accurately. I have optional path (route) parameters. I cannot trust that any doc generator will correctly generate a doc for my API because if the generator is OAS-compliant, it MAY ignore or throw an error when it encounters
required: false
in a parameter definition that also includesin: 'path'
.This is an unreasonable restriction.
EDIT: (forgot to finish my thought) In all cases,
blog.domain/api/post/
irrespective of what follows reaches the same resource on my server which then decides how to handle what comes after the ‘/’ <- but that’s my implementation choice, and I currently have no way of documenting that. I may, for example, wish that for the case of no argument, return the latest by default or a collection of posts.Jumping in here late to say that I also wish for this to be implemented. My use case is the same as @bryan5989 above, in that I have paths:
and I also have
My server receives requests at the root
blog.domain/api/post/
synonymous with the requests above by provided POST values. My path looks like this:So a POST request to
blog.domain/api/post/
with the body{ post_id: "latest" }
receives the same response asblog.domain/api/post/latest
provides. And requests that come in at /api/post/ without the {post_id} parameter receive the same response as /api/post/latest (default: “latest”).This is how my application is structured and I would like for it to be documented properly. The reason I would like to be able to document this properly is so that my generated API requests are properly formatted. Right now, when using swagger-ui to connect using my given swagger.json, requests without filling in the optional path parameter look like this: