OpenAPITools / OpenAPITools/openapi-generator
[BUG] [PYTHON] Inaccurate typing on API return values, query/path params, headers, etc.
Nobody has claimed this yet.
- 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.
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.
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
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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