Share Python legacy model helpers across generated models
What changed
- Generate the Python legacy model conversion helpers once in a private
_legacy_model_helpers.pypackage module for full client generation. - Import the shared helpers from generic,
anyOf, andoneOfmodels and fromApiClient. - 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 passedmvn -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.yamlpython -m pytest -q testsin the regenerated sample — 11 passedpython -m flake8 legacy_model_dict_client/_legacy_model_helpers.pygit 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.pywhen legacy compatibility is enabled and supporting files are generated or explicitly requested viaCodegenConstants.SUPPORTING_FILES, honoring a custom source folder; exposeCodegenConstants.GENERATE_SUPPORTING_FILESto templates. - Update
PythonClientCodegentemplates (model_generic,model_oneof,model_anyof,api_client) to import the shared helpers whenuseLegacyModelHelpersModuleis set; add_legacy_model_helpers.mustache. - Fall back to inlined helpers when generating models only and treat whitespace-only
SUPPORTING_FILESvalues 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-dictionariessample and drop its source size by ~7%.