无法从Swagger UI中的文件问题中读取

时间:2015-08-24 17:40:48

标签: swagger-ui swashbuckle

我在我的应用程序中加入了swagger UI。

当我尝试看到swagger UI时,我很好地获得了API的文档,但是一段时间后它在按钮上显示了一些错误图标。

错误信息如下:

  

[{“level”:“error”,“message”:“无法从文件中读取   http://MYIP/swagger/docs/v1“}]

我不确定是什么导致它。如果我刷新它可以工作并在几秒钟后显示错误。

6 个答案:

答案 0 :(得分:85)

我在猜测" http://MYIP/swagger/docs/v1"无法公开访问。

默认情况下,swagger ui使用在线验证器:online.swagger.io。如果它无法访问您的swagger网址,那么您将看到该错误消息。

可能的解决方案:

  1. 禁用验证:

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

  2. 让您的网站可公开访问

  3. 在本地托管验证器:

  4. 您可以从https://github.com/swagger-api/validator-badge#running-locally

    获取验证码

    您还需要告诉swaggerui验证器的位置

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

答案 1 :(得分:17)

补充接受的答案......我只是在SwaggerConfig.cs中取消注释了一行。我只想通过禁用验证器来摆脱主要招摇页面上的红色错误。

// 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();

答案 2 :(得分:5)

如果您使用的是swagger-ui github存储库中的文件,则可以通过将index.html设置为validatorUrl,从null文件中禁用模式验证:

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"
  })

答案 3 :(得分:1)

如果您使用PHP Laravel frameworkL5-Swagger取消注释

'validatorUrl' => null,

来自配置文件/config/l5-swagger.php

答案 4 :(得分:1)

this.model.validatorUrl = null;设置dist/swagger-ui.js为我工作..

// 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;
}

答案 5 :(得分:0)

使用Swashbuckle.OData时遇到类似问题的任何人:

我在将Swagger与我们的OData端点集成时遇到问题(使用 ODataController 用于API和 Swashbuckle.OData NuGet包)。我必须为此编写自己的文档过滤器并添加它:

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();
            });

显然,为了避免验证错误,我不得不注释掉设置 ODataSwaggerProvider 的行,并关闭了以上文章中提到的验证器。这使得Swashbuckle.OData的实用性值得怀疑,但我没有测试它与香草Swashbuckle一起起作用的情况。

注意:我使用了GitHub页面上描述的Swashbuckle.OData方法,但该方法无效:根本没有显示任何端点。也许有人知道更好的解决方案。