Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add `host`, `basePath` and `schemes` to swagger.json using Swashbuckle Aspnetcore

I am using official doc step by step method to configure Swagger UI and generate Swagger JSON file in my ASP.NET core API application.

Get started with Swashbuckle and ASP.NET Core

If I look at my generated swagger.json file - it is missing three important properties host, basePath and schemes

Please help me understand what piece of code can I add so the swagger.json that gets generated will have following mentioned properties/values.

Here is an ideal swagger.json - give attention to the host, basePath and schemes values which are missing if I follow the documentation code in my application

{
  "swagger": "2.0",
  "info": {
    "version": "v1",
    "title": "Demo API Title"
  },
  "host": "some-url-that-is-hosted-on-azure.azurewebsites.net",
  "basePath": "/api",
  "schemes": ["https"],
  "paths": {
    "/Account/Test": {
      "post": {
        "tags": [
          "Admin"
        ],
        "summary": "Account test method - POST",
        "operationId": "AccountTest",
        "consumes": [],
        "produces": [
          "text/plain",
          "application/json",
          "text/json"
        ],
        "parameters": [],
        "responses": {
          "200": {
            "description": "Success",
            "schema": {
              "type": "boolean"
            }
          }
        }
      }
    }
  },
  "definitions": {
    "NumberSearchResult": {
      "type": "object",
      "properties": {
        "number": {
          "type": "string"
        },
        "location": {
          "type": "string"
        }
      }
    }
  },
  "securityDefinitions": {
    "Bearer": {
      "name": "Authorization",
      "in": "header",
      "type": "apiKey",
      "description": "Authorization. Example: \"Authorization: Bearer {token}\""
    }
  },
  "security": [
    {
      "Bearer": []
    }
  ]
}
like image 897
Jsinh Avatar asked Dec 15 '18 13:12

Jsinh


People also ask

How do you use swashbuckle AspNetCore Swagger?

Add and configure Swagger middlewareLaunch the app and navigate to https://localhost:<port>/swagger/v1/swagger.json . 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 .

Where is Swagger v1 Swagger JSON?

Launch the app, and navigate to http://localhost:<port>/swagger/v1/swagger.json . The generated document describing the endpoints appears as shown in Swagger specification (swagger. json). The Swagger UI can be found at http://localhost:<port>/swagger .

What is the default Swagger UI URL?

By default, Swagger UI is accessible at /q/swagger-ui .

What is swashbuckle AspNetCore?

Swashbuckle is an open source project for generating Swagger documents for Web APIs that are built with ASP.NET Core. There are three core components: AspNetCore. SwaggerGen - provides the functionality to generate JSON Swagger documents that describe the objects, methods, return types, etc. AspNetCore.


1 Answers

There are some changes in latest version of Swashbuckle for .netcore

If you wish to change Request URL in Swashbuckle, maybe you are behind API gateway or have custom domain attached to your webapp. Do this.

  1. Create Document filter
public class BasePathDocumentFilter : IDocumentFilter
    {
        public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
        {
            swaggerDoc.Servers = new List<OpenApiServer>() { new OpenApiServer() { Url = "hxxt://yoursite" } };
        }
    }
  1. In your startup file.In services.AddSwaggerGen() method add document filter like this c.DocumentFilter<BasePathDocumentFilter>();
like image 71
Amey Vaidya Avatar answered Sep 20 '22 15:09

Amey Vaidya