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

Unable to use byte type parameters with Python 3 #120

Closed
olipratt opened this issue Apr 18, 2017 · 2 comments
Closed

Unable to use byte type parameters with Python 3 #120

olipratt opened this issue Apr 18, 2017 · 2 comments
Milestone

Comments

@olipratt
Copy link
Contributor

Hi,

Whenever I use a byte format field with pyswagger and Python 3 and pass in a Python bytes object, I hit an exception like the one below.

Looking at the code to convert the bytes to JSON here, this actually returns bytes and not a str, matching the docs here:

>>> import base64
>>> base64.urlsafe_b64encode(b'abcdef')
b'YWJjZGVm'

And passing that to the JSONEncoder causes a TypeError as below.

 File "C:\Program Files (x86)\Python 3.5\lib\site-packages\pyswagger\contrib\client\requests.py", line 40, in request
    req.prepare(scheme=self.prepare_schemes(req), handle_files=False)
  File "C:\Program Files (x86)\Python 3.5\lib\site-packages\pyswagger\io.py", line 217, in prepare
    content_type, self.__data = self._prepare_body()
  File "C:\Program Files (x86)\Python 3.5\lib\site-packages\pyswagger\io.py", line 83, in _prepare_body
    return content_type, self.__op._mime_codec.marshal(content_type, body, _type=_type, _format=_format, name=name)
  File "C:\Program Files (x86)\Python 3.5\lib\site-packages\pyswagger\primitives\codec.py", line 29, in marshal
    return codec.marshal(value, **kwargs)
  File "C:\Program Files (x86)\Python 3.5\lib\site-packages\pyswagger\primitives\codec.py", line 48, in marshal
    return json.dumps(value, cls=PrimJSONEncoder)
  File "C:\Program Files (x86)\Python 3.5\lib\json\__init__.py", line 237, in dumps
    **kw).encode(obj)
  File "C:\Program Files (x86)\Python 3.5\lib\json\encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "C:\Program Files (x86)\Python 3.5\lib\json\encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "C:\Program Files (x86)\Python 3.5\lib\site-packages\pyswagger\primitives\comm.py", line 12, in default
    return json.JSONEncoder.default(self, obj)
  File "C:\Program Files (x86)\Python 3.5\lib\json\encoder.py", line 180, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: b'AA==' is not JSON serializable

I think Byte.to_json() should return a str - I'll try and create a pull request with a fix.

Thanks!

@mission-liao mission-liao added this to the v0.8.29 milestone Apr 20, 2017
mission-liao added a commit that referenced this issue Apr 23, 2017
[fix] Unable to use byte type parameters with Python 3
#120 (by Oliver Pratt)
@mission-liao
Copy link
Member

@olipratt Hi, thanks for your PR, and your fix is included in v0.8.29, which is just released.

@olipratt
Copy link
Contributor Author

Thanks for the heads-up!

mission-liao added a commit to pyopenapi/pyopenapi that referenced this issue Aug 12, 2017
[fix] Unable to use byte type parameters with Python 3
pyopenapi/pyswagger#120 (by Oliver Pratt)
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

2 participants