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

unicode type on a param causes Swagger documentation to crash under Python 2 #720

Open
Drarok opened this issue Sep 30, 2019 · 3 comments
Open
Labels

Comments

@Drarok
Copy link

Drarok commented Sep 30, 2019

Code

# Note this is Python 2 code
@api.route("/search")
class Search(Resource):
    @api.param(
        "search", type=unicode, default=None,
        description="etc etc etc",
    )
    def get(self):
        pass

Repro Steps (if applicable)

  1. Define a resource as above
  2. Attempt to access Swagger documentation
  3. Broken!

Expected Behavior

I would expect unicode to be a valid type.

Actual Behavior

Swagger documentation is unavailable, console shows the following error:

[start]   File "…/json/encoder.py", line 184, in default
[start]     raise TypeError(repr(o) + " is not JSON serializable")
[start] TypeError: <type 'unicode'> is not JSON serializable

Error Messages/Stack Trace

[start] Traceback (most recent call last):
[start]   File "…/my_project/app/lib/flask/app.py", line 1813, in full_dispatch_request
[start]     rv = self.dispatch_request()
[start]   File "…/my_project/app/lib/flask/app.py", line 1799, in dispatch_request
[start]     return self.view_functions[rule.endpoint](**req.view_args)
[start]   File "…/my_project/app/lib/flask_restplus/api.py", line 329, in wrapper
[start]     return self.make_response(data, code, headers=headers)
[start]   File "…/my_project/app/lib/flask_restplus/api.py", line 350, in make_response
[start]     resp = self.representations[mediatype](data, *args, **kwargs)
[start]   File "…/my_project/app/lib/flask_restplus/representations.py", line 27, in output_json
[start]     dumped = dumps(data, **settings) + "\n"
[start]   File "…/python2.7/json/__init__.py", line 251, in dumps
[start]     sort_keys=sort_keys, **kw).encode(obj)
[start]   File "…/python2.7/json/encoder.py", line 209, in encode
[start]     chunks = list(chunks)
[start]   File "…/python2.7/json/encoder.py", line 434, in _iterencode
[start]     for chunk in _iterencode_dict(o, _current_indent_level):
[start]   File "…/python2.7/json/encoder.py", line 408, in _iterencode_dict
[start]     for chunk in chunks:
[start]   File "…/python2.7/json/encoder.py", line 408, in _iterencode_dict
[start]     for chunk in chunks:
[start]   File "…/python2.7/json/encoder.py", line 408, in _iterencode_dict
[start]     for chunk in chunks:
[start]   File "…/python2.7/json/encoder.py", line 408, in _iterencode_dict
[start]     for chunk in chunks:
[start]   File "…/python2.7/json/encoder.py", line 332, in _iterencode_list
[start]     for chunk in chunks:
[start]   File "…/python2.7/json/encoder.py", line 408, in _iterencode_dict
[start]     for chunk in chunks:
[start]   File "…/python2.7/json/encoder.py", line 442, in _iterencode
[start]     o = _default(o)
[start]   File "…/python2.7/json/encoder.py", line 184, in default
[start]     raise TypeError(repr(o) + " is not JSON serializable")
[start] TypeError: <type 'unicode'> is not JSON serializable

Environment

  • Python 2.7.16
  • Flask 1.0.2
  • Flask-RESTPlus 0.12.1
@Drarok Drarok added the bug label Sep 30, 2019
@j5awry
Copy link
Collaborator

j5awry commented Sep 30, 2019

We'll chat about this, but my gut is to not check into Python2 bugs with the Py2 death date approaching quickly. We've got limited resources.

Does this work under Py3?

@Drarok
Copy link
Author

Drarok commented Sep 30, 2019

Python 3 removes the unicode type since its default str type supports unicode, so I don't think it's an issue under Py3.

@Drarok
Copy link
Author

Drarok commented Sep 30, 2019

I've found a way to fix this:

diff --git a/flask_restplus/swagger.py b/flask_restplus/swagger.py
index 77c364b..21f9555 100644
--- a/flask_restplus/swagger.py
+++ b/flask_restplus/swagger.py
@@ -40,6 +40,9 @@ PY_TYPES = {
     None: 'void'
 }
 
+if six.PY2:
+    PY_TYPES[unicode] = 'string'
+
 RE_URL = re.compile(r'<(?:[^:<>]+:)?([^<>]+)>')
 
 DEFAULT_RESPONSE_DESCRIPTION = 'Success'

That said, it might be pretty unusual to define a param as type=unicode – Flask-RESTPlus appears to use unicode types internally regardless. The issue which caused me to define my params as unicode instead of str was elsewhere.

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

No branches or pull requests

2 participants