【问题标题】:Swagger UI disable Try It Out buttonSwagger UI 禁用试用按钮
【发布时间】:2021-07-09 06:53:46
【问题描述】:

我想在 API 文档中禁用 Try it Out 按钮。我在参数和配置中尝试过"tryItOut": false。此外,我使用的 swagger 2.0 默认启用“试用”按钮,而在 3.0 中默认禁用。因此,应该有配置它的选项。如何从 Swagger UI 中禁用 Try it out 按钮?

【问题讨论】:

标签: swagger-ui


【解决方案1】:

在 Swagger UI 配置中将 supportedSubmitMethods 设置为空数组 []。 v. 3.10.0+ 支持此配置选项。

const ui = SwaggerUIBundle({
  "dom_id": "#swagger-ui",
  url: "https://path/to/your/api.yaml",
  ...
  supportedSubmitMethods: []    // <--------
})

此配置还可以针对特定 HTTP 方法选择性地禁用“试用”。例如,supportedSubmitMethods: ["get", "head"] 仅对 GET 和 HEAD 保留“试用”,但对 POST、PUT、PATCH、DELETE 和其他方法禁用它。

【讨论】:

    【解决方案2】:

    来自 GitHub 存储库 cmets,下面的代码适用于我

    const DisableTryItOutPlugin = function() {
      return {
        statePlugins: {
          spec: {
            wrapSelectors: {
              allowTryItOutFor: () => () => false
            }
          }
        }
      }
    }
    
    // elsewhere, when you call Swagger-UI...
    SwaggerUI({
      plugins: [
        DisableTryItOutPlugin
      ]
    })
    

    参考:https://github.com/swagger-api/swagger-ui/issues/3725#issuecomment-334899276

    【讨论】:

    • 虽然插件仍然有效,但现在有一个内置的 supportedSubmitMethods 配置。
    猜你喜欢
    • 2017-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-26
    • 2021-06-06
    相关资源
    最近更新 更多