[英]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-openapi
与ujson
的最后两个版本( 2.x
和3.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.