Share Python legacy model helpers across generated models

Merged
AB
2d ago
+279-431

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: https://github.com/kubernetes-client/python/issues/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, honoring a custom source folder; 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 and treat whitespace-only SUPPORTING_FILES values as not requesting supporting files, keeping model-only outputs self-contained.
  • Add focused tests for import vs inline behavior, source-folder placement, and whitespace-only supporting files; regenerate the python-legacy-model-dictionaries sample and drop its source size by ~7%.