swagger-api / swagger-api/swagger-codegen

[Python2] Client encoding issues

Open
#7,109 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the generated api_client.py entry point, especially __deserialize_primitive, and reproduce the failure with the supplied Swagger definition, ex1.json, and Python 2 command. Done means the generated client can deserialize the JSON response containing the Chinese value without raising the reported TypeError.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.