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.

Optional parameters in the path generate invalid swagger

See original GitHub issue

Strange 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:closed
  • Created 8 years ago
  • Reactions:2
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
kwaazaarcommented, Oct 22, 2016

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.

3reactions
domaindrivendevcommented, Oct 21, 2016

Swagger is a specification (see http://swagger.io/specification/). Swashbuckle generates Swagger. The specification DOES NOT support optional parameters in “path”.

Read more comments on GitHub >

github_iconTop 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 >

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