gpt4 book ai didi

swagger-ui - SwaggerUIBundle : Specify base url

转载 作者:行者123 更新时间:2023-12-05 09:28:37 32 4
gpt4 key购买 nike

我需要在以下位置测试 api:

<表类="s-表"><头><日> <日> <正文>API 网址http://api.mydomain.loc:20109/v1/API 定义网址http://api.mydomain.loc:20109/v1/swagger/swagger.json

API 定义是:

{
"openapi": "3.0.1",
"info": {
"title": "***",
"description": "***",
"version": "v1"
},
"servers": [
{
"url": "/v1/path1/path2"
}
],
"/ressource1": {
"get": {
"responses": {
"200": {
"description": "Success"
}
}
}
},
...
}

我在 documentation 中遵循这部分 unpkg启动本地 Swagger UI。我使用以下内容创建文件“swagger-ui.html”:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta
name="description"
content="SwaggerIU"
/>
<title>SwaggerUI</title>
<link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist@4.5.0/swagger-ui.css" />
</head>
<body>
<div id="swagger-ui"></div>
<script src="https://unpkg.com/swagger-ui-dist@4.5.0/swagger-ui-bundle.js" crossorigin></script>
<script>
window.onload = () => {
window.ui = SwaggerUIBundle({
url: 'http://api.mydomain.loc:20109/v1/swagger/swagger.json',
dom_id: '#swagger-ui',
});
};
</script>
</body>
</html>

当我打开页面时,会正确加载 API 定义并显示 Swagger UI。但是当我尝试端点“ressource1”时,Swagger UI 调用“http://api.mydomain.loc:20109/v1/path1/path2/ressource1”。就我而言,我想调用“http://api.mydomain.loc:20109/v1/ressource1”。

如何使用 unpkg 覆盖 Swagger UI 中的基本路径?

最佳答案

这是另一个使用 plugin 的解决方案与 rootInjects .主要思想与@vernou's answer相同。 - 动态更新 OpenAPI 定义。

<!-- index.html -->

<script>
const UrlMutatorPlugin = (system) => ({
rootInjects: {
setServer: (server) => {
const jsonSpec = system.getState().toJSON().spec.json;
const servers = [{url: server}];
const newJsonSpec = Object.assign({}, jsonSpec, { servers });

return system.specActions.updateJsonSpec(newJsonSpec);
}
}
});

window.onload = function() {
const ui = SwaggerUIBundle({
url: "http://api.mydomain.loc:20109/v1/swagger/swagger.json",
...

// Add UrlMutatorPlugin to the plugins list
plugins: [
SwaggerUIBundle.plugins.DownloadUrl,
UrlMutatorPlugin
],

// This will set appropriate data when Swagger UI is ready
onComplete: () => {
window.ui.setServer("http://api.mydomain.loc:20109/v1")
}
});

window.ui = ui;
};
</script>

关于swagger-ui - SwaggerUIBundle : Specify base url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71206117/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com