80

I have incorporated swagger-ui in my application.

When I try and see the swagger-ui I get the documentation of the API nicely but after some time it shows some error icon at the button.

The Error message is like below:

[{"level":"error","message":"Can't read from file http://MYIP/swagger/docs/v1"}]

I am not sure what is causing it. If I refresh it works and shows error after few seconds.

2240
  • 1,547
  • 2
  • 12
  • 30
Abi P
  • 1,410
  • 3
  • 22
  • 36

6 Answers6

108

I am guessing "http://MYIP/swagger/docs/v1" is not publicly accessible.

By default swagger ui uses an online validator: online.swagger.io. If it cannot access your swagger url then you will see that error message.

Possible solutions:

  1. Disable validation:

    config.EnableSwagger().EnableSwaggerUi(c => c.DisableValidator());

  2. Make your site publicly accessible

  3. Host the validator locally:

You can get the validator from: https://github.com/swagger-api/validator-badge#running-locally

You will also need to tell swaggerui the location of the validator

config.EnableSwagger().EnableSwaggerUi(c => c.SetValidatorUrl(<validator_url>));

Jon R
  • 1,207
  • 1
  • 9
  • 8
  • 3
    Can you please tell in which file we could configure the validator to be disabled? – shashi009 Oct 15 '15 at 09:21
  • 3
    That depends on which framework you are using. For example with Web Api 2 in Startup.cs: `public void Configuration(IAppBuilder app) { ... HttpConfiguration config = new HttpConfiguration(); SwaggerConfig.Register(config); }` Then the validator could be configured in SwaggerConfig.cs – Jon R Nov 16 '15 at 06:57
  • For the next person - when I downloaded and got the validator-badge running locally, I found out that my swagger json was valid. The error was because my swagger.json isn't available remotely (which is what I [and probably you] want). – Ryan Shillington May 16 '17 at 16:59
  • 1
    Config file is available in .\App_Start\SwaggerConfig.cs – Vijai Jan 30 '18 at 18:43
  • Any chance to fix this if I use the publicly available docker image of swagger ui? – Gunslinger Mar 07 '18 at 15:28
  • Is there any security risk or other kind of risk for disabling validation? – Trevor de Koekkoek Nov 08 '18 at 19:35
20

To supplement the accepted answer...I just uncommented one line in the SwaggerConfig.cs. I only wanted to get rid of the red error on the main swagger page by disabling the validator.

// By default, swagger-ui will validate specs against swagger.io's online validator and display the result
// in a badge at the bottom of the page. Use these options to set a different validator URL or to disable the
// feature entirely.
//c.SetValidatorUrl("http://localhost/validator");
c.DisableValidator();
Justin Pavatte
  • 1,198
  • 2
  • 12
  • 18
13

If you are using files from swagger-ui github repo, then you can disable schema validation from your index.html file by setting validatorUrl to null in it:

window.onload = function() {

  // Build a system
  const ui = SwaggerUIBundle({
    url: "/docs/open_api.json",
    dom_id: '#swagger-ui',

    validatorUrl : null,   # <----- Add this line

    deepLinking: true,
    presets: [
      SwaggerUIBundle.presets.apis,
      SwaggerUIStandalonePreset
    ],
    plugins: [
      SwaggerUIBundle.plugins.DownloadUrl
    ],
    layout: "StandaloneLayout"
  })
Carlos Mermingas
  • 3,822
  • 2
  • 21
  • 40
vaibhavatul47
  • 2,766
  • 4
  • 29
  • 42
3

If you using PHP Laravel framework with L5-Swagger just uncomment

'validatorUrl' => null,

from the config file /config/l5-swagger.php

Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
Seva Kalashnikov
  • 4,262
  • 2
  • 21
  • 36
1

Setting this.model.validatorUrl = null; in dist/swagger-ui.js worked for me ..

// Default validator
if(window.location.protocol === 'https:') {
  //this.model.validatorUrl = 'https://online.swagger.io/validator';
  this.model.validatorUrl = null;
} else {
  //this.model.validatorUrl = 'http://online.swagger.io/validator';
  this.model.validatorUrl = null;
}
bobbyrne01
  • 6,295
  • 19
  • 80
  • 150
0

To anynoe having similar issue when using Swashbuckle.OData:

I was having issues to integrated Swagger with our OData endpoints (using ODataController for API and Swashbuckle.OData NuGet package). I had to write our own document filter for it and add it:

GlobalConfiguration.Configuration
            .EnableSwagger(c =>
                {
                    c.SingleApiVersion("v1", "OurSolution.API");
                    c.DocumentFilter<SwaggerDocumentFilter>();
                    //c.CustomProvider((defaultProvider) => new ODataSwaggerProvider(defaultProvider, c, GlobalConfiguration.Configuration));
                    c.IncludeXmlComments(GetXmlCommentsPath());
                    c.UseFullTypeNameInSchemaIds();
                    c.RootUrl(req => ConfigurationManager.AppSettings["AppUrl"]);
                })
            .EnableSwaggerUi(c =>
            {
                c.DisableValidator();
            });

Apparently in order to avoid validation error I had to comment out line which is setting ODataSwaggerProvider along with turning off validator as mentioned in posts above. This makes usefulness of Swashbuckle.OData questionable yet I didn't test whatever it works with vanilla Swashbuckle.

Note: I used approach described on GitHub page for Swashbuckle.OData but it was not working: showing no possible endpoints at all. Maybe somebody knows better solution.

Diomos
  • 420
  • 5
  • 15