Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swagger Editor shows "Failed to fetch" error

I'm new to Swagger and using Swagger Editor running locally on my desktop to test an API. I'm not responsible for server configuration, and have no access to make changes. I have my security definitions set up and my authorization working. Now I'm trying to set up my first path schema, but when I execute it, I get an error message that says "TypeError: Failed to fetch" and the Response Headers field is empty.

enter image description here

However, when I copy the Curl request provided by Swagger Editor and run it in GitBash, it returns the value I expect. So I know that Swagger Editor has created a working request.

I know that my security authorization is working because I can see the token it returns in the Curl request.

The authentication schema and the path schema both hit different sub-domains. Authentication is handled by betaauthorize.myDomain.com while the path is handled by betaapi.myDoamin.com.

The errors I'm getting in the browser console don't mean much to me. enter image description here

Again, I'm running Swagger Editor from my desktop hard drive. Is there some kind of Swagger Editor config setting that I'm missing? Is there a server config issue that I need to make the server admins aware of? I've been trying to figure this out for two days and I'm all out of ideas. Any suggestions would be appreciated.

like image 810
M.Shute Avatar asked Apr 12 '17 17:04

M.Shute


4 Answers

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.

like image 187
Obakeng Balatseng Avatar answered Oct 19 '22 18:10

Obakeng Balatseng


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.

like image 40
M.Shute Avatar answered Oct 19 '22 17:10

M.Shute


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);
    });
}
like image 31
Antony Booth Avatar answered Oct 19 '22 17:10

Antony Booth


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.

like image 20
MaximeW Avatar answered Oct 19 '22 17:10

MaximeW