swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
17.04k stars 6.03k forks source link

[Python2] Client encoding issues #7109

Open nelz9999 opened 6 years ago

nelz9999 commented 6 years ago
Description

When using the python client generated by the 2.3.0 branch, in Python2, it mishandles proper JSON unicode encodings. (I believe this is probably a corner case related to @kenjones-cisco's work on #6881.)

It doesn't blow up similarly when running the client under Python3.

Swagger-codegen version

swagger-codegen-cli-2.3.0-20171204.142945-311.jar

Swagger declaration file content or url
---
swagger: "2.0"
info:
  version: 0.0.1
  title: Example
  description: Example service
  contact:
    name: "@nelz9999"
basePath: "/"
schemes:
- http
produces:
- application/json
paths:
  /ex1.json:
    get:
      operationId: ex1
      responses:
        '200':
          description: Greetings
          schema:
            "$ref": "#/definitions/Greetings"
definitions:
  Greetings:
    type: object
    required:
    - en
    - zh
    properties:
      en:
        type: string
      zh:
        type: string
Command line used for generation

Use the following config to build, saved as config.json:

{
  "packageName":"encoding_example"
}
$ java -jar ./swagger-codegen-cli-2.3.0-20171204.142945-311.jar generate -i ./swagger.yml -l python -o ./generated/ -c ./config.json

Then, install the client via pip.

Steps to reproduce

Create the following file as ex1.json:

{
    "en": "hello",
    "zh": "\u4F60\u597D"
}

Serve that file by running this in the same directory:

$ python -m SimpleHTTPServer 8000
$ python
Python 2.7.13 (default, Jul 18 2017, 09:17:00)
>>> import encoding_example
>>> api_instance = encoding_example.DefaultApi()
>>> api_instance.api_client.configuration.host = "http://127.0.0.1:8000"
>>> api_instance.ex1()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/nelz/.venv/orion/lib/python2.7/site-packages/encoding_example/api/default_api.py", line 53, in ex1
    (data) = self.ex1_with_http_info(**kwargs)  # noqa: E501
  File "/Users/nelz/.venv/orion/lib/python2.7/site-packages/encoding_example/api/default_api.py", line 119, in ex1_with_http_info
    collection_formats=collection_formats)
  File "/Users/nelz/.venv/orion/lib/python2.7/site-packages/encoding_example/api_client.py", line 322, in call_api
    _preload_content, _request_timeout)
  File "/Users/nelz/.venv/orion/lib/python2.7/site-packages/encoding_example/api_client.py", line 161, in __call_api
    return_data = self.deserialize(response_data, response_type)
  File "/Users/nelz/.venv/orion/lib/python2.7/site-packages/encoding_example/api_client.py", line 233, in deserialize
    return self.__deserialize(data, response_type)
  File "/Users/nelz/.venv/orion/lib/python2.7/site-packages/encoding_example/api_client.py", line 272, in __deserialize
    return self.__deserialize_model(data, klass)
  File "/Users/nelz/.venv/orion/lib/python2.7/site-packages/encoding_example/api_client.py", line 613, in __deserialize_model
    kwargs[attr] = self.__deserialize(value, attr_type)
  File "/Users/nelz/.venv/orion/lib/python2.7/site-packages/encoding_example/api_client.py", line 264, in __deserialize
    return self.__deserialize_primitive(data, klass)
  File "/Users/nelz/.venv/orion/lib/python2.7/site-packages/encoding_example/api_client.py", line 544, in __deserialize_primitive
    return six.u(data)
  File "/Users/nelz/.venv/orion/lib/python2.7/site-packages/six-1.10.0-py2.7.egg/six.py", line 647, in u
    return unicode(s.replace(r'\\', r'\\\\'), "unicode_escape")
TypeError: decoding Unicode is not supported
cledoux commented 6 years ago

There's a bug filed in the six repo about this: https://github.com/benjaminp/six/issues/45

In this report, they explicitly state

[six.u] should only be used for string literals. It's not a make-unicode-out-of-anything magic function.

Documentation on six says the same thing.

Text should always be a normal string literal.