使用openapi v3配置,我有一个名为“主机名”的服务器变量,用于构建url,例如:
...
servers:
- url: http://{hostname}/api
variables:
hostname:
"default": "some default here"
....
在运行时,我希望能够更改“主机名”服务器变量。我在页面上找到了UI元素,
<input type="text" value="some default here" data-variable="hostname">
通过编辑输入字段来更改变量可以很好地工作,但是通过jQuery更改输入字段不起作用,即使在设置值后触发“ change”事件时,在扩展api部分之一时该值也将恢复。我还尝试触发keyup / keydown和focusin / focusout事件,以更好地模拟用户如何更改字段。
是否有更灵活的方法通过公开调用来更改此值?我看过window.ui,但它有点神秘。
答案 0 :(得分:2)
您可以通过编写插件来更改规范json,然后再呈现
manifest.json
答案 1 :(得分:1)
我在不同的IoT设备上托管了一个api.yaml文件。每个设备将根据其配置使用不同的主机名。加载页面时,我尝试使用javascript将“主机名”服务器变量设置为window.location.hostname,例如通过javascript。
您只需指定一个相对服务器url
-它将相对于OpenAPI定义文件的位置进行解析。
例如,如果您有
servers:
- url: /api
并且API定义文件位于
http://foobar/spec/api.yaml
然后将API调用的基础url
解析为
http://foobar/api
答案 2 :(得分:1)
调整 answer from nevermind 以与 index.html from swagger-ui 中使用的 SwaggerUIBundle 一起使用:
plugins: [
SwaggerUIBundle.plugins.DownloadUrl,
// Custom plugin that replaces the server list with the current url
function() {
return {
statePlugins: {
spec: {
wrapActions: {
updateJsonSpec: function(oriAction, system) {
return (spec) => {
spec.servers = [{url: window.location.origin}]
return oriAction(spec)
}
}
}
}
}
}
}
],
这样 index.html 就可以从后端服务本身提供,并且只会在自己的 url 上引用。