swagger-api / swagger-api/swagger-codegen

[Python] rest_client.py does not accept byte types for body

Open
#11,079 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

Description

If I pre convert the body to be POST or PUT to an API into bytes from XML, like the following code based on an lxml parsed element:

from lxml import etree

# ... intervening code

el = # some lxml element
body = etree.tostring(el, encoding='utf-8')

I cannot then use this body with rest_client.py generated by the API client - it will only accept type str despite the fact that Python 2 is end-of-life.

Swagger-codegen version

3.0.24 or 3.0.25

Command line used for generation

I am working Ex Libris' Alma product which requires API keys. Not sure if this helps you but:

java -jar lib/swagger-codegen-cli.jar generate -l python -c api-config.json -i https://developers.exlibrisgroup.com/wp-content/uploads/alma/openapi/bibs.yaml --resolve-fully -o target

The api-config.json is below

{
  "packageName": "alma_api",
  "modelPackage": "models",
  "apiPackage": "apis"
}
Steps to reproduce
  1. Take any API that accepts a PUT or a POST
  2. Build a bytes type input for body
  3. Use it with the API
Suggest a fix/enhancement

See the code for encoding the body in modules/swagger-codegen/src/main/resources/python/rest.mustache.

You can see that the default type expected for an encoded body is str, but in Python 3 it should be bytes.

There are a number of options here:

  1. Drop support for Python 3 and simply accept either str or bytes
  2. Split the Python code generator from Python 3- support Python 2 as legacy.
  3. Use exception processing code to support both at the same time.

Because there are options around whether to support Python 2 going forward, it may be hard for a new contributor to craft a pull request until the need to support Python 2 going forward is solved.

I think Maven/Apache Ivy supplies the answer - if you need Python 2 support, use an older version.

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 with modules/swagger-codegen/src/main/resources/python/rest.mustache around the body-encoding code cited in the issue. Reproduce the failure using a Python bytes body with a generated client, then determine which Python 2 and Python 3 compatibility approach the project will accept. Done means the chosen behavior is implemented and bytes bodies work for POST or PUT requests without leaving the support policy unresolved.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api, tooling
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.