Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] TypeError: Object of type Spec is not JSON serializable #155

Closed
oalexandere opened this issue Jan 8, 2020 · 16 comments
Closed

[Bug] TypeError: Object of type Spec is not JSON serializable #155

oalexandere opened this issue Jan 8, 2020 · 16 comments

Comments

@oalexandere
Copy link
Contributor

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

@AtomsForPeace
Copy link
Contributor

Thanks for the good bug report. I'll take a look at this later on.

@AtomsForPeace
Copy link
Contributor

I'm not able to reproduce this locally. What address are you going to?
Works for me at http://localhost:8000/swagger/#/default/test

@oalexandere
Copy link
Contributor Author

oalexandere commented Jan 9, 2020

Hello!
For reproducing I've create a new virtual env and installed only sanic-openapi (python --version 3.7.4):
image

Then run the app:
image

Goes http://127.0.0.1:8000/ and get {"hello":"world"}. But http://127.0.0.1:8000/swagger/ gets me an error:
image

Here is the app errors when I goes to http://127.0.0.1:8000/swagger/:
image

If I goes http://localhost:8000/swagger/#/default/test it also returns same errors:
image
image

@oalexandere
Copy link
Contributor Author

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

@oalexandere
Copy link
Contributor Author

Additional info, maybe it will be usefull.
Line 263 in swagger.py returns json(swagger_blueprint._spec), where swagger_blueprint._spec is Spec object, not dict as in version 0.6.0 sanic-openapi (rigth screen, line 289):
image

@chenjr0719
Copy link
Member

It is because that ujson can dump Spec class to JSON but json can not. And your OS is windows so you can only use json. That is the reason I and @AtomsForPeace can not reproducible the bug. Let me think about how to fix it and thanks for your report. It is very helpful to improve this project. 😀

@AtomsForPeace
Copy link
Contributor

@chenjr0719 Can we add CI for windows?

Maybe returning the json of the dict of the Spec class. I could try that but can't really test it... :/

@oalexandere
Copy link
Contributor Author

Thank you for your response, I created a pull request with trying to fix it, look it please.

@chenjr0719
Copy link
Member

@chenjr0719 Can we add CI for windows?

Maybe we can follow the same configuration from sanic to test without ujson. What do you think?

@chenjr0719
Copy link
Member

The #157 has been merged. Close this issue.

@xnuinside
Copy link

btw, same issue 1-to-1 with 0.6.1 on MacOS 10.14.6, not only on Windows. Also solved problem with downgrade to 0.6.0 version.

@Ronserruya
Copy link

^^ Same issue on MacOs 10.15.1 , works when downgrading to 0.6.0

@garcia-0E
Copy link

I'm facing this issue on a local Ubuntu 19.04 env. Tried to downgrade to 0.6.0 but still, have any clue??

@chenjr0719
Copy link
Member

@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.

@GitHubLeoYeh
Copy link

GitHubLeoYeh commented May 6, 2020

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

@dnutiu
Copy link

dnutiu commented Jul 21, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants