首页 文章

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

提问于
浏览
58

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

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

错误消息如下所示:

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

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

6 回答

  • 0

    我猜“http://MYIP/swagger/docs/v1”不公开 .

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

    可能的解决方案:

    • 禁用验证:

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

    • 使您的网站可公开访问

    • 在本地托管验证器:

    您可以从以下位置获取验证码:https://github.com/swagger-api/validator-badge#running-locally

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

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

  • 1

    为了补充已接受的答案......我只是在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();
    
  • 83

    如果您使用的是swagger-ui github repo中的文件,则可以通过将 validatorUrl 设置为 null 来禁用 index.html 文件中的架构验证:

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

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

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

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

    'validatorUrl' => null,
    

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

  • 4

    To anynoe having similar issue when using Swashbuckle.OData:

    我有问题将Swagger与我们的OData endpoints 集成(使用ODataController for 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的用处有问题,但我没有测试它与vanilla Swashbuckle有什么用 .

    注意:我使用GitHub页面上描述的Swashbuckle.OData方法,但它没有工作:根本没有显示可能的 endpoints . 也许有人知道更好的解决方案 .

相关问题