swagger-api / swagger-api/swagger-codegen

[Python] Allow Unicode Characters in Request Body.

Open
#10,831 0 comments 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

In order to send a request with a string body containing unicode characters (like '∆', for example), the body needs to be explicitly encoded using body_str.encode('utf-8') in order avoid the UnicodeEncodeError in https://github.com/python/cpython/blob/master/Lib/http/client.py#L1313

1312        if isinstance(body, str):
1313            # RFC 2616 Section 3.7.1 says that text default has a
1314            # default charset of iso-8859-1.
1315            body = _encode(body, 'body')

which is gonna try to explicitly encode it as latin-1 and fail.

I propose to fix it by updating this condition here
https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/resources/python/rest.mustache#L182
which should allow all string types, as well as bytes type

182                elif isinstance(body, (six.string_types, six.binary_type))

instead of checking just for isinstance of str

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 modules/swagger-codegen/src/main/resources/python/rest.mustache around line 182 and review how generated Python clients classify request bodies. Verify the generated client can send Unicode string and bytes bodies without the reported encoding failure; no test file is named in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.