简体   繁体   English

如何在 Swagger UI 中隐藏服务器描述?

[英]How to hide server descriptions in Swagger UI?

I have an OpenAPI 3.0 definition with multiple servers:我有一个包含多个服务器的 OpenAPI 3.0 定义:

servers:
- url: https://development.gigantic-server.com/v1
  description: Development server
- url: https://staging.gigantic-server.com/v1
  description: Staging server
- url: https://api.gigantic-server.com/v1
  description: Production server

When this definition is rendered in Swagger UI, the "Servers" dropdown shows the description of each server:当此定义在 Swagger UI 中呈现时,“服务器”下拉列表显示每个服务器的描述:

Swagger UI 中的服务器描述

Is it possible to hide the server descriptions from this dropdown?是否可以从此下拉列表中隐藏服务器描述?

Swagger UI always renders server description if it's provided, this is hard-coded: Swagger UI 总是呈现服务器description (如果提供),这是硬编码的:
https://github.com/swagger-api/swagger-ui/blob/master/src/core/plugins/oas3/components/servers.jsx#L114 https://github.com/swagger-api/swagger-ui/blob/master/src/core/plugins/oas3/components/servers.jsx#L114

As a workaround you can modify the API definition dynamically after it's loaded and remove server descriptions.作为一种解决方法,您可以在加载 API 定义后动态修改它并删除服务器描述。 To do that, edit your index.html page and add the following onComplete function to the Swagger UI initialization code:为此,请编辑您的index.html页面并将以下onComplete function 添加到 Swagger UI 初始化代码中:

  const ui = SwaggerUIBundle({
    url: "https://path/to/your/openapi.json",
    ...

    onComplete: function() {
      let spec = ui.specSelectors.specJson().toJS();
      let servers = spec.servers || [];

      for (let i in servers) {
        servers[i].description = ""
      }

      ui.specActions.updateJsonSpec(spec);
    }
  })

They haven't provided any option to replace this server's description in another place, but they have mentioned the description is optional in swagger specification of object representing a Server .他们没有提供任何选项来在其他地方替换此服务器的description ,但他们已经提到该description在 object 的 swagger 规范中是可选的,代表一个 Server

Swagger UI have not provided any rendering option for this. Swagger UI 没有为此提供任何渲染选项。

The best use of description is define in a single word, like production , development , api , staging , etc.. description的最佳用途是用一个词定义,例如productiondevelopmentapistaging等。

If you really don't want in dropdown then you can remove it from your server list.如果您真的不想在下拉列表中,则可以将其从服务器列表中删除。

servers:
- url: https://development.gigantic-server.com/v1
- url: https://staging.gigantic-server.com/v1
- url: https://api.gigantic-server.com/v1

This part i am writing for your information, about how to use oas-servers ,这部分我写给你的信息,关于如何使用oas-servers

I observed your server urls, these can be easily define in single url, how?我观察了您的服务器网址,这些可以在单个 url 中轻松定义,如何? using server variables .使用 服务器变量

servers:
- url: https://{environment}.gigantic-server.com/{version}
  variables: 
    environment:
      enum:
        - 'development'
        - 'staging'
        - 'api'
    version:
      enum:
        - 'v1'

Hope this help.希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM