Skip to content

Unable to use byte type parameters with Python 3 #120

@olipratt

Description

@olipratt

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions