首页 > 解决方案 > how to create a host field in the swagger.json that is generated via flask restplus with blueprint

问题描述

I created a flask app as below. In local, I can see the Swagger UI; however, when deployed to a server, the Swagger UI is not available (kept getting an error "SCRIPT5009: SCRIPT5009: 'SwaggerUIBundle' is not defined"). It seems like the swagger is looking for files at the root URL level (local host or server, not at the url_prefix level) even though the Swagger.json is at the url_prefix level.

app = Flask(__name__)
blueprint = Blueprint('queue', __name__, url_prefix='/apipath/api/v1'
authorizations = {
'apikey': {
    'type': 'apiKey',
    'in': 'header',
    'name': 'X-Api-Key'
  }
}
api = Api(blueprint, version='1.0.0', title='title', description='description', contact='email', authorizations=authorizations, doc='/')
app.register_blueprint(blueprint)
ns = api.namespace('queue', description='description')

So, I decide to use the Swagger.json that is generated. However, the Swagger.json doesn't have the host field, so that the onboarded Swagger UI doesn't work (meaning I can't do 'Try it out') - shown below.

swagger "2.0"
basePath    "/apipath/api/v1"
paths   
    /queueprediction    
        post    
            responses   
                200 

标签: pythonflaskswaggerflask-restplus

解决方案


我想出了如何在 Swagger.json 中包含主机字段。

通过设置 app.config['SERVER_NAME'] = 'some host' 而不是在 app.run 语句中设置 'host' 或 'port' 就可以了。

谢谢!


推荐阅读