6

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"
            }
          }
        }
      }
    }
macm
  • 544
  • 7
  • 21

2 Answers2

4

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");
}
Derviş Kayımbaşıoğlu
  • 28,492
  • 4
  • 50
  • 72
  • ugghhh that seems like a terrible way to solve this issue. Can you point to any examples suggesting this? – macm Jan 03 '19 at 23:11
  • I think part of the issue is I am using the API only model. I don't have a wwwroot and when I goto UseStaticFiles it is resolving to C:\Program Files\IIS Express . This is an ASPNetCore API project. I can get the hostingEnvironment but the documentation asks for either a FQDN or relative path. var pathToSwagger = Path.Combine(_hostingEnvironment.ContentRootPath, "bin", "swagger.json"); but that is physical path and won't work – macm Jan 03 '19 at 23:44
  • 2
    Figured it out .. thanks .I ended up having to create a wwwroot directory and put the swagger.json file in there. After doing that it work exactly as expected. Thanks for putting me on the right track. – macm Jan 04 '19 at 00:01
0

.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"
        });
    }
Song
  • 593
  • 9
  • 21