OpenAPITools / OpenAPITools/openapi-generator

[BUG] [PYTHON] Inaccurate typing on API return values, query/path params, headers, etc.

Open
#16,401 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Python Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

The python client generated by the openapi-generator for python using the asyncio library (probably it's library agnostic) generates a valid client with poor and/or incorrect typing. For example, in the given openapi-spec.json below, I have a schema called 'Contact' and two endpoints, one GET /v1/contacts/{id} that takes in a required string and returns Contact, and one GET /v1/contacts that returns a List[Contact].

The GET /v1/contacts/{id} typing incorrectly says id should be an object, but validly type annotates the return as Contact, while the GET /v1/contacts says the int query params should be object and annotates the type return as object instead of List[Contact] as expected.
image

Additionally, I have one Header param String-Header which is typed as a required string in the spec, but the generated client and documentation say it's an object not a string.
image

Other OpenAPI generators such as Redocly, Swagger all correctly type annotate the parameters and the return values, and I see in the generator code that it should be annotating List[Contact], but it doesn't seem to want to and instead defaults back to object.

openapi-generator version

docker v7.0.0-beta

OpenAPI declaration file content or url

openapi-spec.json

Generation Details

openapi-config.json

{
  "library": "asyncio",
  "packageName": "MyAPI",
  "projectName": "MyAPI",
  "packageVersion": "v0.1.0"
}
docker run --rm --user $UID:$GID -v $PWD:/local openapitools/openapi-generator-cli:v7.0.0-beta generate -i /local/openapi-spec.json -g python -o /local/python-client -c /local/openapi-config.json
Steps to reproduce
mkdir test-client
cd test-client
touch openapi-spec.json # write provided openapi-spec.json
touch openapi-config.json # write provided openapi-config.json
docker run --rm --user $UID:$GID -v $PWD:/local openapitools/openapi-generator-cli:v7.0.0-beta generate -i /local/openapi-spec.json -g python -o /local/python-client -c /local/openapi-config.json

And observe the generated client and generated documentation.

Related issues/PRs

I couldn't find anything immediately obvious in terms of related issues/PRs. Some more buggy-bugs where the code wasn't functional, but this is more of an improvement.

Suggest a fix

I couldn't seem to figure out exactly which file generates the API specification, but I suspect either the Schema object making it's way into DefaultCodegen is wrong, or it fails to match isArraySchema, or...? Happy to fix if someone can help me understand where the codegen actually happens 🙂

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

Reproduce the generated Python asyncio client with the linked openapi-spec.json, openapi-config.json, and Docker command, then inspect DefaultCodegen.java and the Python generator path. Compare parameter and return annotations in the generated client and documentation with the OpenAPI schema; done means string, integer, Contact, and List[Contact] types are preserved instead of defaulting to object.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker, java, openapi, python
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.