Repository navigation
[python] Fix empty example value for sibling properties that share a model - #25106
Conversation
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
|
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? |
|
Thanks for testing it! I think the diff you posted is the fix working: the I re-ran it just now with the spec from #25047 (needs 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 |
|
Thanks for the detailed reply. I think I misread the change a bit. Sorry for the false alarm. Let's give it a try. |
Fixes #25047.
Cause.
toExampleValueRecursiveinAbstractPythonCodegenkeeps the ancestors of the current schema inincludedSchemasand 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,prop3andprop4all end up referencing one deduplicated model, so the third reference was treated as a cycle and the generated test containedprop4 = ,(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.
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.prop4now gets a model example and every generated test file passespython -m py_compile.bin/configs/python*.yamlone by one: no sample changes (none of the sample specs has siblings sharing a model three times).PR checklist
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
Written for commit bd1eca8. Summary will update on new commits.