[python] Fix empty example value for sibling properties that share a model

Merged
TA
12d ago
+27-1

Fixes #25047.

Cause. toExampleValueRecursive in AbstractPythonCodegen keeps the ancestors of the current schema in includedSchemas and returns "" when a schema shows up twice, to stop on cycles. The same list was passed to every property of an object, so siblings polluted each other's path. In the issue's spec, prop2, prop3 and prop4 all end up referencing one deduplicated model, so the third reference was treated as a cycle and the generated test contained prop4 = , (a Python syntax error). That is why only the 4th field was affected.

Fix. Each property gets its own copy of the ancestor list, so real cycles (a model reaching itself through its own descendants) are still cut, and siblings no longer count.

Tests.

  • New PythonClientCodegenTest.testExampleValueForSiblingsSharingAModel: three properties referencing one model. It fails on master with the empty value and passes with the change.
  • PythonClientCodegenTest, PythonPydanticV1ClientCodegenTest, PythonFastAPIServerCodegenTest, PythonFlaskConnexionServerCodegenTest: 143 tests pass.
  • Generated the issue's spec with the patched CLI: prop4 now gets a model example and every generated test file passes python -m py_compile.
  • Regenerated every bin/configs/python*.yaml one by one: no sample changes (none of the sample specs has siblings sharing a model three times).

PR checklist

  • Read the contribution guidelines.
  • Built the project and regenerated the Python samples (no changes).
  • @mention the technical committee: @cbornet @tomplus @krjakbrjak @fa0311

Summary by cubic

Fixes empty example values in generated Python tests when three or more sibling properties reference the same model. The third reference was previously mistaken for a cycle and produced invalid Python like prop4 = ,.

Bug Fixes

  • Each property now gets its own copy of the ancestor list used for cycle detection, so shared sibling models are no longer treated as cycles while real cycles are still cut.
  • Adds a regression test with three sibling properties referencing one model.