Search code examples
yamlswagger-uiopenapiswagger-editoroas

How to hide server descriptions in Swagger UI?


I have an OpenAPI 3.0 definition with multiple servers:

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:

Server descriptions in Swagger UI

Is it possible to hide the server descriptions from this dropdown?


Solution

  • Swagger UI always displays the server description if it's provided, this is hard-coded:
    https://github.com/swagger-api/swagger-ui/blob/master/src/core/plugins/oas3/components/servers.jsx#L85

    As a workaround you can modify the API definition dynamically after it's loaded and remove server descriptions. To do that, edit your Swagger UI's swagger-initializer.js or index.html file and add the following onComplete function to the SwaggerUIBundle initialization code:

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