Swagger use a custom swagger.json file aspnet core

风流意气都作罢 提交于 2020-02-24 11:48:39

问题


Pretty sure I am missing something clearly obvious but not seeing it.

How can I use my updated swagger.json file?

I took my boilerplate swagger/v1/swagger.json code and pasted it into the editor.swagger.io system. I then updated the descriptions etc, added examples to my models and then saved the contents as swagger.json. Moved the file into the root of my api application, set the file to copy always.

 public void ConfigureServices(IServiceCollection services)
    {...
        services.AddSwaggerGen(c => { c.SwaggerDoc("V1", new Info {Title = "Decrypto", Version = "0.0"}); });

    }


 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
      ...
        app.UseSwagger();
      //--the default works fine
     //  app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/V1/swagger.json", "Decrypto v1"); });
        app.UseSwaggerUI(c => { c.SwaggerEndpoint("swagger.json", "Decrypto v1"); });

        app.UseMvc();
    }

I have tried a few different variation but none seem to be the trick. I don't really want to rewrite the work in SwaggerDoc as it seems dirty to me put documentation in the runtime.

the custom swagger.json file I want to use looks like this:

        {
      "swagger": "2.0",
      "info": {
        "version": "0.0",
        "title": "My Title"
      },
      "paths": {
        "/api/Decryption": {
          "post": {
            "tags": [
              "API for taking encrypted values and getting the decrypted values back"
            ],
            "summary": "",
            "description": "",
            "operationId": "Post",
            "consumes": [
              "application/json-patch+json",
              "application/json",
              "text/json",
              "application/*+json"
            ],
            "produces": [
              "text/plain",
              "application/json",
              "text/json"
            ],
            "parameters": [
              {
                "name": "units",
                "in": "body",
                "required": true,
                "schema": {
                  "uniqueItems": false,
                  "type": "array",
                  "items": {
                    "$ref": "#/definitions/EncryptedUnit"
                  }
                }
              }
            ],
            "responses": {
              "200": {
                "description": "Success",
                "schema": {
                  "uniqueItems": false,
                  "type": "array",
                  "items": {
                    "$ref": "#/definitions/DecryptedUnit"
                  }
                }
              }
            }
          }
        }
      },
      "definitions": {
        "EncryptedUnit": {
          "type": "object",
          "properties": {
            "value": {
              "type": "string",
              "example": "7OjLFw=="
            },
            "initializeVector": {
              "type": "string",
              "example": "5YVg="
            },
            "cipherText": {
              "type": "string",
              "example": "596F5AA48A882"
            }
          }
        },
        "DecryptedUnit": {
          "type": "object",
          "properties": {
            "encrypted": {
              "type": "string",
              "example": "7OjLV="
            },
            "decrypted": {
              "type": "string",
              "example": "555-55-5555"
            }
          }
        }
      }
    }

回答1:


you need to configure PhysicalFileProvider and put your swagger.json into wwwroot or anywhere accessible by PhysicalFileProvider. After that you can access it using IFileProvider

Reference: https://www.c-sharpcorner.com/article/file-providers-in-asp-net-core/


Edit If you just add app.UseStaticFiles(); into your StartUp, you can access wwwroot without hastle.

Reference


Completely Different Approach

you may also consider to serve your file using Controller/Action

public IActionResult GetSwaggerDoc()
{
    var file = Path.Combine(Directory.GetCurrentDirectory(), 
                            "MyStaticFiles", "swagger.json");

    return PhysicalFile(file, "application/json");
}



回答2:


.NET Core 2.2 could server physical file to url resource like below.
But if you use custom swagger json, your api is fixed except you change it every time.

    public void Configure(IApplicationBuilder app, IHostingEnvironment env,
        ILoggerFactory loggerFactory)
    {   
        ...
        app.UseStaticFiles(new StaticFileOptions
        {
            FileProvider = new PhysicalFileProvider(
                Path.Combine(Directory.GetCurrentDirectory(),
                    "swagger/v1/swagger.json")),
            RequestPath = "swagger/v1/swagger.json"
        });
    }


来源:https://stackoverflow.com/questions/54030633/swagger-use-a-custom-swagger-json-file-aspnet-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!