Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i specify the default opening version of swagger?

I have a C# web API that is using Swagger as API documentation. I have used the Swashbuckle packages. The swagger environment is working with multiple versions that i specify in the controllers.

Today i introduced a new future version (1.2) that is still under development. I would like to open swagger on the version 1.1 version by default but still keep the correct sorting order in the dropdown in the top right (e.g. v1, v1.1, v1.2). Currently it always opens the top version in the drop down.

Does someone has an idea how to do this?

Dropdown swagger

like image 423
Joris Mathijssen Avatar asked Feb 12 '20 10:02

Joris Mathijssen


People also ask

What is the default Swagger URL?

Add and configure Swagger middleware The generated document describing the endpoints appears as shown in OpenAPI specification (openapi. json). The Swagger UI can be found at https://localhost:<port>/swagger .

What is Swagger version?

Metadata. Every Swagger specification starts with the Swagger version, 2.0 being the latest version. A Swagger version defines the overall structure of an API specification – what you can document and how you document it.

What is the latest version of Swagger UI?

4.14. 0 (2022-08-17)


1 Answers

The order in which you configure Swagger UI in Startup.cs - Configure method determines the dropdown list order. By default, the UI displays the specifications corresponding to the first option in the dropdown.

We can change the order of versions as shown below, but I am not sure if there is any property to override default version while retaining the dropdown order of versions in UI.

In the below example, it will open v1.1 by default.

app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("/swagger/v1.1/swagger.json", "V1.1");
    c.SwaggerEndpoint("/swagger/v1.0/swagger.json", "V1.0");
    c.SwaggerEndpoint("/swagger/v1.2/swagger.json", "V1.2");
}

Howerver, there's a workaround: You can pass the value of querystring parameter urls.primaryName so that it loads that version by default.

https://localhost:5001/swagger/index.html?urls.primaryName=v1.1

(Or) You can try customizing the Swagger UI by injecting custom javascript as follows:

app.UseSwaggerUI( 
            ....
            c => c.InjectJavascript(***pass custom javascript here***) )
like image 123
Bob Ash Avatar answered Sep 20 '22 19:09

Bob Ash