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

Consider coercing response codes to strings #422

Closed
zachmullen opened this issue Apr 5, 2019 · 4 comments

Comments

@zachmullen
Copy link

commented Apr 5, 2019

The OpenAPI specification allows responses of the form 2XX, 4XX, etc. Using this library, it's possible in a docstring to write something like this:

responses:
   200:
       content:
           application/json:
               schema: GoodResponseSchema
   4XX:
       content:
           application/json:
               schema: RequestErrorSchema

This causes the following error:

File "/Users/zach/.pyenv/versions/3.6.5/lib/python3.6/json/encoder.py", line 353, in _iterencode_dict
    items = sorted(dct.items(), key=lambda kv: kv[0])
TypeError: '<' not supported between instances of 'str' and 'int'

Which happens because the 200 key is stored as an int, and the 4XX key is stored as a string. A preprocessing step to make them all strings would fix that problem.

@zachmullen

This comment has been minimized.

Copy link
Author

commented Apr 5, 2019

Note that just calling to_dict on the spec is not enough to trigger this error, this is a result of passing that return value into Flask's jsonify, which sorts dict keys when running in development mode.

@lafrech

This comment has been minimized.

Copy link
Member

commented Apr 5, 2019

This makes total sense. In fact, in flask-rest-api, I explicitly cast to string (Nobatek/flask-rest-api@7d688f4).

Would you like to submit a PR for that?

@zachmullen

This comment has been minimized.

Copy link
Author

commented Apr 5, 2019

Sure, can you point me to where in the code this ought to be done?

@zachmullen

This comment has been minimized.

Copy link
Author

commented Apr 7, 2019

Thanks!!! 🎉 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.