Skip to content

Share Python legacy model helpers across generated models - #24730

Open
Abhinav0905 wants to merge 2 commits into
OpenAPITools:masterfrom
Abhinav0905:agent/share-python-legacy-model-helpers
Open

Share Python legacy model helpers across generated models#24730
Abhinav0905 wants to merge 2 commits into
OpenAPITools:masterfrom
Abhinav0905:agent/share-python-legacy-model-helpers

Conversation

@Abhinav0905

@Abhinav0905 Abhinav0905 commented Aug 18, 2026

Copy link
Copy Markdown

What changed

  • Generate the Python legacy model conversion helpers once in a private _legacy_model_helpers.py package module for full client generation.
  • Import the shared helpers from generic, anyOf, and oneOf models and from ApiClient.
  • Keep model-only generation self-contained by falling back to the existing inline helpers when supporting files are not generated.
  • Add focused generator assertions and regenerate the legacy model dictionaries sample.

Why

compatibleWithPythonLegacy currently expands the same helper partial into every generated model and ApiClient. In kubernetes-client/python#2677, this accounts for roughly 1,538 repeated model copies and about 3.4 MB of duplicated source across the synchronous and asynchronous clients.

This fixes the duplication at its source without adding a generator option or changing the generated public API. The small checked-in sample drops from 151,102 to 140,230 Python source bytes (7.20%) and from seven copies of _get_openapi_to_dict to one.

Closes #24729.

Downstream follow-up: kubernetes-client/python#2677

Validation

  • mvn -ntp -pl modules/openapi-generator -am -Dtest=PythonClientCodegenTest#testLegacyModelToDictRendering+testLegacyModelToDictSupportsModelOnlyGeneration -Dsurefire.failIfNoSpecifiedTests=false test — 2 passed
  • mvn -ntp -pl modules/openapi-generator-cli -am package -DskipTests=true -Dmaven.javadoc.skip=true -Djacoco.skip=true — build succeeded
  • ./bin/generate-samples.sh bin/configs/python-legacy-model-dictionaries.yaml
  • python -m pytest -q tests in the regenerated sample — 11 passed
  • python -m flake8 legacy_model_dict_client/_legacy_model_helpers.py
  • git diff --check

PR checklist

  • Read the contribution guidelines.
  • Run the full-project build, regenerate every sample, and export all generator docs. The focused Python sample and relevant modules were built and tested locally; the complete matrix is left to CI while this PR is in draft.
  • Python technical committee: @cbornet @tomplus @krjakbrjak @fa0311

Summary by cubic

Shares Python legacy model conversion helpers across generated models to remove duplication without changing the public API. Previously each model and ApiClient inlined the helpers; now full client generation emits a single private _legacy_model_helpers.py that models and ApiClient import, with a model-only fallback to inline helpers.

  • Generate _legacy_model_helpers.py when legacy compatibility is enabled and supporting files are generated or explicitly requested via CodegenConstants.SUPPORTING_FILES; expose CodegenConstants.GENERATE_SUPPORTING_FILES to templates.
  • Update PythonClientCodegen templates (model_generic, model_oneof, model_anyof, api_client) to import the shared helpers when useLegacyModelHelpersModule is set; add _legacy_model_helpers.mustache.
  • Fall back to inlined helpers when generating models only, keeping model-only outputs self-contained.
  • Add focused tests for import vs inline behavior and regenerate the python-legacy-model-dictionaries sample; source size drops by ~7%.

Written for commit 0c1bee9. Summary will update on new commits.

Review in cubic

@Abhinav0905
Abhinav0905 marked this pull request as ready for review August 19, 2026 17:08

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 18 files

Re-trigger cubic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: emit legacy-compatible model helpers once per package

1 participant