OpenAPITools / OpenAPITools/openapi-generator
Python: emit legacy-compatible model helpers once per package
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Description
The Python generator's compatibleWithPythonLegacy mode currently inlines the same legacy model conversion helpers into every generated generic, anyOf, and oneOf model. Kubernetes Python enables this mode for both its synchronous and asynchronous clients, resulting in the same helper implementation being emitted across roughly 1,538 model files (about 3.4 MB of duplicated source).
This was traced from kubernetes-client/python#2677, where a repository approver agreed with fixing the duplication upstream rather than editing generated Kubernetes client files.
Proposed change
- Emit the legacy model conversion helpers once in a private generated package module for full client generation when
compatibleWithPythonLegacy=true. - Import those helpers from
model_generic.mustache,model_anyof.mustache, andmodel_oneof.mustache. - Reuse the shared identity helper from
api_client.mustachewhere applicable. - Preserve the existing private helper names, function signatures, serialization behavior, and generated public API.
- Preserve self-contained model-only generation by retaining the current inline helpers when supporting files are not generated.
- Add focused generator tests and update the existing legacy-compatible Python sample.
This would not add another generator option. Projects that do not enable compatibleWithPythonLegacy would be unchanged.
Validation
The implementation verifies generated imports and model-only fallback behavior with focused generator tests. The regenerated legacy-compatible Python sample passes all 11 runtime tests and reduces its generated package from 151,102 to 140,230 bytes (7.20%) while reducing seven helper copies to one. After an upstream release/commit is available, Kubernetes Python can update its pinned generator and regenerate both clients in a separate PR.
Downstream context: https://github.com/kubernetes-client/python/issues/2677
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
Start with model_generic.mustache, model_anyof.mustache, model_oneof.mustache, and api_client.mustache to trace the current helper emission and imports. Then inspect the focused generator tests and the existing legacy-compatible Python sample. Done means shared helpers are emitted once for full generation, model-only generation retains its fallback, and the sample's runtime tests still pass.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Refactor
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100