Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing swagger-ui server variable at runtime

Tags:

swagger-ui

Using an openapi v3 configuration I have a server variable called 'hostname' that is used to build the url, like:

...
servers:
- url: http://{hostname}/api
 variables:
  hostname:
   "default": "some default here"
....

At runtime I'd like to be able to change the 'hostname' server variable. I've found the UI element on the page,

<input type="text" value="some default here" data-variable="hostname">

Changing the variable by editing the input field works fine, but changing the input field via jQuery isn't working, even when triggering the "change" event after setting the value, the value reverts when expanding one of the api sections. I also tried triggering the keyup/keydown and focusin/focusout events to better simulate how a user would change the field.

Is there a more swagger-ui approach to changing this value through an exposed call? I've looked through window.ui but its kind of cryptic.

like image 588
Chris Morgan Avatar asked Jun 30 '18 13:06

Chris Morgan


People also ask

How do I change my Swagger UI?

You can change default swagger-ui path programmatically using ApplicationListener<ApplicationPreparedEvent> . The idea is simple - override springdoc. swagger-ui. path=/custom/path before your Spring Boot application starts.

How do you set the base path in Swagger?

In OpenAPI 3.0, you can use an array of server elements to specify one or more base URLs for your API. You can register the servers using the annotation @Server: in @OpenAPIDefinition (for all operations) in @Operation (for a single operation)

Where is my Swagger UI URL?

Once your application is started, you can go to http://localhost:8080/q/swagger-ui and play with your API. You can visualize your API's operations and schemas. You can also interact with your API in order to quickly test it.

What is the difference between Swagger and Swagger UI?

Swagger Editor: Swagger Editor lets you edit OpenAPI specifications in YAML inside your browser and to preview documentations in real time. Swagger UI: Swagger UI is a collection of HTML, Javascript, and CSS assets that dynamically generate beautiful documentation from an OAS-compliant API.


2 Answers

I have an api.yaml file hosted on different IoT devices. Each device will have a different hostname based on its configuration. When the page is loaded I'm trying to use javascript to set the 'hostname' server variable to be window.location.hostname, for example via javascript.

You can simply specify a relative server url – it will be resolved relative to the location of the OpenAPI definition file.

For example, if you have

servers:
  - url: /api

and the API definition file is at

http://foobar/spec/api.yaml

then the base url for API calls will be resolved to

http://foobar/api
like image 91
Helen Avatar answered Sep 28 '22 01:09

Helen


You can alter the spec json before it render by writing a plugin

const ui = SwaggerUI({
    // ...
    plugins: [
        // add a plugin to alter spec before it rendered
        {
            statePlugins: {
                spec: {
                    wrapActions: {
                        updateJsonSpec: function(oriAction, system) {
                            return (spec) => {
                                // change spec.servers here to add new entry, use concat to put it as the first & default one
                                spec.servers = [{url: someDynamicUrlInRuntime}].concat(spec.servers || [])
                                return oriAction(spec)
                            }
                        }
                    }
                }
            }
        }
    ]
    // ...
})
like image 27
nevermind Avatar answered Sep 28 '22 01:09

nevermind