Optional parameters in the path generate invalid swagger
See original GitHub issueStrange as it may be, if you have a controller method
[Route("{id}")]
public HttpResponseMessage Get(long? id = null)
{
...
}
This generates invalid swagger json since the parameter comes out with “required”:false
{
"swagger": "2.0",
"info": {
"version": "v1",
"title": "WebApplication3"
},
"host": "localhost:58728",
"schemes": [
"http"
],
"paths": {
"/api/books/{id}": {
"get": {
"tags": [
"Books"
],
"operationId": "Books_Get",
"consumes": [],
"produces": [
"application/json",
"text/json",
"application/xml",
"text/xml"
],
"parameters": [
{
"name": "id",
"in": "path",
"required": false,
"type": "integer",
"format": "int64"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/Object"
}
}
},
"deprecated": false
}
}
},
"definitions": {
"Object": {
"type": "object",
"properties": {}
}
}
}
According to http://swagger.io/v2/schema.json a “pathParameterSubSchema” is only allowed to have “required”:true
I realize that the method is a little strange as its not actually callable without specifying the {id} as part of the URL, however the actual code was a bit more complicated. This was a simplified example to demonstrate the bug.
Issue Analytics
- State:
- Created 8 years ago
- Reactions:2
- Comments:8 (2 by maintainers)
Top Results From Across the Web
Optional parameters in the path generate invalid swagger
Strange as it may be, if you have a controller method [Route("{id}")] public HttpResponseMessage Get(long? id = null) { ... } This generates...
Read more >How to define an optional parameter in path using swagger
I tried to define it in Swagger but I encountered an error, Not a valid parameter definition, after I set the required as...
Read more >Describing Parameters
In OpenAPI, a path parameter is defined using in: path . The parameter name must be the same as specified ... Query parameters...
Read more >Optional Route Parameters with Swagger and ASP.NET Core
I'm here to show you how to make optional route parameters with Swagger and ASP.NET Core. Before we begin let's evaluate the scenario....
Read more >ASP.NET Core nullable route params in Swagger
In this post, I described how to generate an invalid Open API specification for the optional route parameter, but arrived at a better...
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 Free
Top 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
Ok, thats a pity. Yet you could also generate multiple routes/endpoints: one for each variant. In my example that would be three of them. Or I could do that myself by specifying all variants in the webapi controller, which I will do for now.
Swagger is a specification (see http://swagger.io/specification/). Swashbuckle generates Swagger. The specification DOES NOT support optional parameters in “path”.