sanic-openapi: [Bug] TypeError: Object of type Spec is not JSON serializable

Description Trying to reproduce simple example with Getting Started code: https://sanic-openapi.readthedocs.io/en/latest/ When opens swager page see an error: Failed to load API definition. With previous version sanic-openapi 0.6.0 everithing works fine.

Screenshot image

To Reproduce

from sanic import Sanic
from sanic.response import json
from sanic_openapi import swagger_blueprint

app = Sanic()
app.blueprint(swagger_blueprint)


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

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

Environment (please complete the following information):

  • OS: Windows 10 x64
  • Python: 3.7
  • Browser: chrome 79.0.3948.88
  • sanic-openapi: 0.6.1
  • sanic: 19.12.2

Additional context

Traceback (most recent call last): File “C:\Users\user.virtualenvs\bbscreener\lib\site-packages\sanic\app.py”, line 974, in handle_request response = handler(request, *args, **kwargs) File “C:\Users\user.virtualenvs\bbscreener\lib\site-packages\sanic_openapi\swagger.py”, line 263, in spec return json(swagger_blueprint.spec) File “C:\Users\user.virtualenvs\bbscreener\lib\site-packages\sanic\response.py”, line 234, in json dumps(body, **kwargs), File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\json_init.py", line 238, in dumps **kw).encode(obj) File “C:\Users\user\AppData\Local\Programs\Python\Python37\lib\json\encoder.py”, line 199, in encode chunks = self.iterencode(o, _one_shot=True) File “C:\Users\user\AppData\Local\Programs\Python\Python37\lib\json\encoder.py”, line 257, in iterencode return _iterencode(o, 0) File “C:\Users\user\AppData\Local\Programs\Python\Python37\lib\json\encoder.py”, line 179, in default raise TypeError(f’Object of type {o.class.name} ’ TypeError: Object of type Spec is not JSON serializable

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 1
  • Comments: 16 (11 by maintainers)

Most upvoted comments

After install ujson==1.35 Solve it!! (Ubuntu 18.04.4 LTS ) thanks @chenjr0719

I can confirm that this bug is still happening; Downgrading to usjon==1.35 is not a solution, make silent bugs and memory leaks can occur. usjon==3.0.0 make the de-serialization stricter that’s why exceptions occur, errors are no longer ignored.

@ray-t1 I think it is related to the new ujson release. Would you please try ujson==1.3.5 and check is this issue still exist? Somehow the new version of ujson changes its behavior and it affects the JSON serialization.

If downgrade sanic-openapi version to 0.6 everything works fine: image image