Skip to content

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

Merged
wing328 merged 1 commit into
OpenAPITools:masterfrom
TanbirRamim:fix-python-example-sibling-cycle
Oct 4, 2026
Merged

wing328 merged 1 commit into
OpenAPITools:masterfrom
TanbirRamim:fix-python-example-sibling-cycle

Conversation

@TanbirRamim

@TanbirRamim TanbirRamim commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

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.

Written for commit bd1eca8. Summary will update on new commits.

Review in cubic

toExampleValueRecursive tracks the ancestors of the current schema in
includedSchemas to stop on cycles, but the same list was passed to every
property of an object. When three or more sibling properties referenced
the same model (e.g. inline objects the generator deduplicates into one
model), the third one was mistaken for a cycle and got an empty value,
producing 'prop4 = ,' in the generated tests.

Give each property its own copy of the ancestor list.

Fixes OpenAPITools#25047
Copilot AI balanced review requested due to automatic review settings October 2, 2026 19:58

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@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 2 files

Re-trigger cubic

@wing328

wing328 commented Oct 3, 2026

Copy link
Copy Markdown
Member

thanks for the PR. i did a test with the spec provided in the issue but it seems like the test file generated by this fix still has syntax errors:

--- a/test/test_a_post_request.py
+++ b/test/test_a_post_request.py
@@ -42,7 +42,8 @@ class TestAPostRequest(unittest.TestCase):
                         prop = '', ),
                     prop3 = openapi_client.models._a_post_request_object1_prop2._a_post_request_object1_prop2(
                         prop = '', ),
-                    prop4 = , )
+                    prop4 = openapi_client.models._a_post_request_object1_prop2._a_post_request_object1_prop2(
+                        prop = '', ), )
             )
         else:
             return APostRequest(

Are you see the same when you tested it locally with your fix?

@wing328 wing328 removed this from the 7.26.0 milestone Oct 3, 2026
@TanbirRamim

Copy link
Copy Markdown
Contributor Author

Thanks for testing it! I think the diff you posted is the fix working: the - line is the old prop4 = , ) and the + line is the filled-in value.

I re-ran it just now with the spec from #25047 (needs --skip-validate-spec because of type: null) on this branch. Every generated test and model file passes py_compile, and since the example sits inside a docstring I also pulled the make_instance example out of all four test files and ran it through ast.parse. All four parse, and prop4 comes out as:

                    prop4 = openapi_client.models._a_post_request_object1_prop2._a_post_request_object1_prop2(
                        prop = '', ), )

Did you see a syntax error on a different line? One thing that does look odd is the openapi_client.models._a_post_request_object1_prop2._a_post_request_object1_prop2( path itself: the actual module is a_post_request_object1_prop2 and the class is APostRequestObject1Prop2. That path is valid syntax, though, and comes from how inline models are named in examples, not from this change. It's the same on master for prop1 to prop3. I'm happy to look at that in a separate PR if you'd like.

@wing328

wing328 commented Oct 4, 2026

Copy link
Copy Markdown
Member

Thanks for the detailed reply. I think I misread the change a bit. Sorry for the false alarm.

Let's give it a try.

@wing328
wing328 merged commit 4882687 into OpenAPITools:master Oct 4, 2026
15 checks passed
@wing328 wing328 added this to the 7.26.0 milestone Oct 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: make_instance generates syntax error for optional nullable fields with default null in nested objects

3 participants