简体   繁体   English

使用 sanic-openapi 时找不到 swagger.json

[英]Can't find swagger.json when using sanic-openapi

I'm trying to use Sanic-OpenAPI module with sanic to auto-document my own RESTful APIs.我正在尝试使用带有sanic的 Sanic-OpenAPI模块来自动记录我自己的 RESTful API。 So, I installed them by running the following command:因此,我通过运行以下命令安装了它们:

pip install sanic sanic-openapi

# sanic version is 19.12.2
# sanic_openapi version is 0.6.1

And I created this simple project which is, by the way, provided in their official GitHub repository.我创建了这个简单的项目,顺便说一下,它在他们的官方 GitHub 存储库中提供。 And it looks like the following:它看起来像下面这样:

from sanic import Sanic, response
from sanic_openapi import swagger_blueprint


app = Sanic("app")
app.blueprint(swagger_blueprint)


@app.route("/")
async def test(request):
    return response.json({"hello": "world"})


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8000, debug=True)

This runs just fine!这运行得很好! But when I try to access the http://localhost:8000/swagger , it throws the following error:但是当我尝试访问http://localhost:8000/swagger时,它会抛出以下错误:

Traceback (most recent call last):
  File "/media/anwar/E/Chatbot/NLU_engine/py3.7/lib/python3.7/site-packages/sanic/app.py", line 974, in handle_request
    response = handler(request, *args, **kwargs)
  File "/media/anwar/E/Chatbot/NLU_engine/py3.7/lib/python3.7/site-packages/sanic_openapi/swagger.py", line 263, in spec
    return json(swagger_blueprint._spec)
  File "/media/anwar/E/Chatbot/NLU_engine/py3.7/lib/python3.7/site-packages/sanic/response.py", line 234, in json
    dumps(body, **kwargs),
TypeError: <sanic_openapi.spec.Spec object at 0x7effe3d2f910> is not JSON serializable
[2020-07-12 11:34:09 +0200] - (sanic.access)[INFO][127.0.0.1:37880]: GET http://localhost:8000/swagger/swagger.json  500 2649
[2020-07-12 11:34:09 +0200] [11014] [ERROR] Exception occurred while handling uri: 'http://localhost:8000/swagger/swagger.json'
Traceback (most recent call last):
  File "/media/anwar/E/Chatbot/NLU_engine/py3.7/lib/python3.7/site-packages/sanic/app.py", line 974, in handle_request
    response = handler(request, *args, **kwargs)
  File "/media/anwar/E/Chatbot/NLU_engine/py3.7/lib/python3.7/site-packages/sanic_openapi/swagger.py", line 263, in spec
    return json(swagger_blueprint._spec)
  File "/media/anwar/E/Chatbot/NLU_engine/py3.7/lib/python3.7/site-packages/sanic/response.py", line 234, in json
    dumps(body, **kwargs),
TypeError: <sanic_openapi.spec.Spec object at 0x7effe3d2f910> is not JSON serializable

After around two hours of searching, I've found out the solution in this issue on their official GitHub repository.经过大约两个小时的搜索,我在他们的官方 GitHub 存储库中找到了此问题的解决方案。 The reason, according to the issue, is that sanic-openapi is not compatible with the last two releases ( 2.x and 3.x ) of ujson .根据问题,原因是sanic-openapiujson的最后两个版本( 2.x3.x )不兼容。

So, to fix this issue, you need to downgrade your ujson package to 1.35 like so:因此,要解决此问题,您需要将ujson package 降级为1.35 ,如下所示:

pip install ujson==1.35

And it works perfectly now:它现在完美运行:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flask Swagger 用户界面找不到 swagger.json - Flask Swagger UI not able to find swagger.json Flask Swagger UI not able to find swagger.json Fetch error Not Found /static/swagger.json Flask API Docker - Flask Swagger UI not able to find swagger.json Fetch error Not Found /static/swagger.json Flask API Docker Sanic OpenAPI Swagger 文档生成抛出属性路径 operationId 重复 - Sanic OpenAPI Swagger docs generation throws attribute paths operationId is repeated swagger.json 示例 json 预测 model 似乎不返回预测 - swagger.json example json for forecast model doesn't seem to return predictions 无法在 ec2 上运行 python sanic - Can't run python sanic on ec2 错误:“您的部署没有关联的 swagger.json” - Stream 分析作业上的 ACI 部署 - Error: “Your deployment does not have an associated swagger.json” - ACI deployment on Stream Analytics Job 如何在通过带有蓝图的flask restplus生成的swagger.json中创建主机字段 - how to create a host field in the swagger.json that is generated via flask restplus with blueprint 使用 blueprint.copy 时如何覆盖 sanic 中的路由? - how to overwrite a route in sanic when using blueprint.copy? 使用Nginx作为Sanic + Gunicorn的反向代理时的性能下降 - Performance Decrease When Using Nginx as Reverse Proxy for Sanic + Gunicorn 使用 Sanic 和 Redis 出现问题 - Trouble using Sanic and Redis
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM