更改 swagger 包的根路径 javascript

Changing root path of swagger bundle javascript

我在将 Swagger 配置到我的 flask 应用程序时遇到问题,该应用程序部署为另一个父项目下的微服务。

如图,Swagger的静态文件是这样加载的 <script src="/swaggerui/swagger-ui-bundle.js"></script>

这是导致问题的原因,因为 .js 试图从我无法控制的父应用程序的根路径进行解析。

在哪里指定 Swagger 在加载这些 Javascript 时寻找自定义路径?只需在前面添加一个点 . 即可解决问题

我的代码是这样的

from flask import Flask, Blueprint
from flask_restplus import Api

app = Flask(__name__)
blueprint = Blueprint('api', __name__, url_prefix='/api')
api = Api(blueprint, doc='/doc/')
app.register_blueprint(blueprint)

通过覆盖默认模板的 swagger_static 并通过 spec_url 传递必要的路径,我们可以更改 swagger 寻找 html & [ 的路径=22=]。

完整的解决方案如下

@apidoc.apidoc.add_app_template_global
def swagger_static(filename):
    return "./swaggerui/{0}".format(filename)

api = Api(app)

@api.documentation
def custom_ui():
    return render_template("swagger-ui.html", title=api.title, specs_url="./swagger.json")

链接Github讨论https://github.com/noirbizarre/flask-restplus/issues/262#issuecomment-542490899