在运行时更改 swagger-ui 服务器变量 [英] Changing swagger-ui server variable at runtime

查看:58
本文介绍了在运行时更改 swagger-ui 服务器变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 openapi v3 配置,我有一个名为主机名"的服务器变量,用于构建 url,例如:

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"
....

在运行时,我希望能够更改主机名"服务器变量.我在页面上找到了 UI 元素,

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">

通过编辑输入字段更改变量工作正常,但通过 jQuery 更改输入字段不起作用,即使在设置值后触发更改"事件时,在扩展 api 部分之一时该值也会恢复.我还尝试触发 keyup/keydown 和 focusin/focusout 事件,以更好地模拟用户如何更改字段.

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.

是否有更 swagger-ui 的方法来通过公开调用更改此值?我已经浏览了 window.ui 但它有点神秘.

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.

推荐答案

我有一个 api.yaml 文件托管在不同的 IoT 设备上.每个设备将根据其配置具有不同的主机名.加载页面时,我尝试使用 javascript 将主机名"服务器变量设置为 window.location.hostname,例如通过 javascript.

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.

您可以简单地指定一个相对服务器 url - 它将相对于 OpenAPI 定义文件的位置进行解析.

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

例如,如果您有

servers:
  - url: /api

API 定义文件位于

http://foobar/spec/api.yaml

然后 API 调用的基本 url 将被解析为

then the base url for API calls will be resolved to

http://foobar/api

这篇关于在运行时更改 swagger-ui 服务器变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆