Swagger Editor shows "Failed to fetch" error
I've encountered the same problem. The problem arises when swagger-editor ist hosted on a different domain than the API.
You will either have to change that or set the appropriate CORS headers.
Here is a link to the swagger documentation with additional information.
Normally you get this error when you use the wrong HTTP/HTTPS scheme. On your swagger page, there is a schemes dropdown. Ensure that you have not selected https if you are running http. Https is the default.
Also, make sure the SwaggerUI is accessed using the correct HTTPS.
It turns out that my code was requiring an authorization key for all requests, including OPTIONS requests. After some research and consultation, I determined that it's best practice to respond to OPTIONS requests without any authorization - authorization should be required on the subsequent requests. Once I modified the code to proceed without authorization for OPTIONS requests, I was able to get Swagger working for my project.
That message is eating the actual error. The problem is either an API misconfiguration, such as CORS not allowing the json file to be returned, or Swagger config itself.
Here's a few things to try. With Swagger running and the message in the browser
Browse directly to the JSON url itself (E.g: http://myserver.domain:port/swagger/v1/swagger.json). If you receive a 404 error, the SwaggerEndpoint value is incorrect.
SwaggerEndpoint("incorrect/v1/swagger.json", "My incorrect Application Version 1");
SwaggerEndpoint("v1/swagger.json", "My correct Application Version 1");
Once you have attempted to retrieve the swagger.json file from the correct path, either you'll see JSON in your browser, or an exception from a Swagger method call. You can see the stack trace from Swagger to determine the cause.
One reason this occurs is because you have a public method in a controller that isn't an API endpoint, but Swagger thinks it is, until it can't read the HTTPAttribute to determine what verb the endpoint is using (I.e: GET, POST ...) or route (/controller/action/{parameter:dataType}/somethingElse)
// This should be private, not public!
public ReturnType MyHelperMethod(object parameter){
//Do something to parameter
return InstanceOfReturnType;
}
Another reason is if the different data models being used are not unique schemas and you haven't configured swagger to fully qualify schema models to guarantee uniqueness.
Example: -
[HttpGet, Route("something", Name = "Do Something")]
public IActionResult DoSomething([FromBody] Datamodel.Something something)
{
var returnValue = Service.DoSomething(something);
return returnValue;
}
[HttpGet, Route("somethingElse", Name = "Do Something Else")]
public IActionResult DoSomethingElse([FromBody] IdenticalDatamodel.Something somethingElse)
{
var returnValue = Service.DoSomethingElse(somethingElse);
return returnValue;
}
namespace IdenticalDatamodel {
public class Something {
public string SomeProperty{ get; set;}
}
}
namespace Datamodel {
public class Something {
public string SomeProperty{ get; set;}
}
}
In this case, Class 'Something' from 2 different namespaces has the same schema, so Swagger chokes as they're identical. One fix for this is to configure Swagger to fully qualify Schema Ids, so Something in the DoSomething() method and Something in the DoSomethingElse() method would instead be identified as Datamodel.Something and IdenticalDatamodel.Something by Swagger when generating the .json file
To do this, you can use the following code in Startup.cs
public void ConfigureServices(IServiceCollection services)
{
//Add a bunch of service configurations here
// ...
// It's probably better to externalize the Swagger config to it's own private helper method
services.AddSwaggerGen(swagger =>
{
// Setup your Swagger doc, security etc here
});
// Customize the Swagger generator here
services.ConfigureSwaggerGen(options =>
{
// Use fully qualified schema object names to ensure uniqueness
options.CustomSchemaIds(configuration => configuration.FullName);
});
}