Debugging Swashbuckle Error - Failed to load API Definition
Asked Answered
M

4

24

Is there any way to get a stack trace or inner exceptions on Swashbuckle/Swagger errors? At some point, it stopped working. I'm not sure if it was when I upgraded from .Net Core 2.0 to 2.1, but I'm pretty sure it was still working after that. When I navigate to myapidomain/swagger/index.html I get this error:

enter image description here

Which is not very helpful. It was working 2 or so weeks ago... I didn't change any Swagger configuration. It's the same as it's always been:

public void ConfigureServices(IServiceCollection services)
{
    ...
     services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new Info
            {
                Version = "v1",
                Title = "My.API",
                Description = "Test"
            });
        });   
}


public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
        app.UseDeveloperExceptionPage();
    else
        app.UseHsts();

    app.UseSwagger();
    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "PropWorx API V1");
    });
    ...
}

I'm using Swashbuckle.AspNetCore 3.0.0 on an ASP.Net Core 2.1 Web API.

Memorandum answered 3/8, 2018 at 9:0 Comment(3)
What do you get when you open swagger/v1/swagger.jsonFong
Thanks Helder, sorry just saw your comment now. Yes, opening that file helped me figure out the issue.Memorandum
I'm having a similar problem, but with ASP.Net Swashbuckle 5.6.0 (latest). The failure looks quite different and I don't get any useful debug information from the request. I wonder why your Swashbuckle behaves so differently.Varicella
H
44

Agree that information on the UI isn't useful for debugging (Failed to Load API Definition / Fetch Error, etc), however the full exception trace can be scraped by opening up your browser's debugging tools (e.g. F12 on Chrome), refreshing your /swagger endpoint, and then examining the swagger.json payload - this is an XHR request which will fail with a 500 status code.

(I would suggest on a big project that you bookmark the link, so that in future you can just go straight to the json file, e.g. https://MyHost/swagger/v1/swagger.json)

e.g. in the below contrived error, I've duplicated the same route between two methods:

[HttpGet]
[Route("parseenum")]
public ActionResult CheckEnum([FromQuery] MyEnum someEnum)
...

[HttpGet]
[Route("parseenum")]
public ActionResult CheckEnum2([FromQuery] MyEnum someEnum)
...

Which produces the error:

SwaggerError

Which you should now be able to track down and fix.

Hemstitch answered 3/8, 2018 at 10:15 Comment(3)
Thanks Stuart! I followed your instructions and got to the root of the problem: "System.InvalidOperationException: Conflicting schemaIds: Identical schemaIds detected for types PropWorx.API.ModelsShared.User and PropWorx.API.Models.User. See config settings - "CustomSchemaIds" for a workaround"Memorandum
This isn't working on Swagger 8.3.20.403. There is no swagger.json anymore. There is a V1 entry that is downloaded but gives no information in the preview tab.Oliveolivegreen
This works like a charm, I've been looking for a quick and simple way to debug non-type safe issues for a while.Forklift
S
1

If your api have same two or more [HttpGet] its not working swagger. You should be specify [HttpGet] , [HttpGet ("{id}")]

  [HttpGet]
`[HttpGet ("{id}")]`
Simonsen answered 12/4, 2020 at 18:3 Comment(0)
B
0

i have this problem today and try so much to resolve it , you should must remove all [Route] before [http] tag`s in C# controller code like this code for example:

[Route("~/api/getAll")]
[HttpGet]
public ActionResult<List<asteriksModel>>GetAll()
{
    return _context.asterList.ToList();
}

and your route code mustbe like this

[HttpGet]
public ActionResult<List<asteriksModel>>GetAll()
{
    return _context.asterList.ToList();
}

in my project it works fine

Bimanous answered 23/10, 2018 at 10:38 Comment(2)
Yes , the swagger use default route that Controller makes , all the Programmer route the API by them selfs , so the swagger can not find our route , and use default , to use them , so we must to remove all the [Route] from top of [http..] or beside them like [httpget,[Route("...")]] it`s work fine without themBimanous
and unfortunately i forget to say you this point , you must set your API route like this : [Route("api/[controller]")]Bimanous
T
0

I found that the SwaggerFunctions need to by Anonymous for the Swagger/UI to complete.

public static class SwaggerFunctions
{
    [SwaggerIgnore]
    [FunctionName("Swagger")]
    public static Task<HttpResponseMessage> Swagger(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "swagger/json")]
    HttpRequestMessage req,
        [SwashBuckleClient] ISwashBuckleClient swashBuckleClient)
    {
        return Task.FromResult(swashBuckleClient.CreateSwaggerDocumentResponse(req));
    }

    [SwaggerIgnore]
    [FunctionName("SwaggerUi")]
    public static Task<HttpResponseMessage> SwaggerUi(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "swagger/ui")]
    HttpRequestMessage req,
        [SwashBuckleClient] ISwashBuckleClient swashBuckleClient)
    {
        return Task.FromResult(swashBuckleClient.CreateSwaggerUIResponse(req, "swagger/json"));
    }
}
Trunnion answered 29/6, 2020 at 23:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.