fix: Process union variant properties once for nullable top-level schemas by dmarin-pixel · Pull Request #1518 · openapi-generators/openapi-python-client · GitHub
A schema in components/schemas with type: ["object", "null"] that has a nested model, for example a property that is itself a nullable object, is dropped with Attempted to generate duplicate models with name "...Type0". Every schema that references it is dropped too. The generator exits 0 and only prints "Client was generated, but some pieces may be missing".
Cause: _create_schemas calls property_from_data with process_properties=False, so that model properties are built later in _process_models. UnionProperty.build splits a type list into one variant per type and calls property_from_data for each, but it did not forward process_properties or roots. The object variant was therefore built immediately, which registered InnerLabelsType0. The same variant is also in models_to_process, so building it again in _process_models registered the same name a second time and failed.
This forwards process_properties and roots from property_from_data through UnionProperty.build to its variants. anyOf/oneOf unions go through the same code path and get the same fix.
Changes:
union.py: UnionProperty.build takes process_properties and roots and passes them to the variants.
properties/__init__.py: property_from_data passes both to UnionProperty.build.
New functional test TestNullableTopLevelObjectWithNullableProperty in test_unions.py.
Patch changeset.
Testing:
The new test errors on main and passes with this change.
pytest tests end_to_end_tests: 468 passed, 4 skipped. The golden records are unchanged.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A schema in components/schemas with type: ["object", "null"] that has a nested model, for example a property that is itself a nullable object, is dropped with Attempted to generate duplicate models with name "...Type0". Every schema that references it is dropped too. The generator exits 0 and only prints "Client was generated, but some pieces may be missing".
Minimal spec:
components: schemas: Outer: type: object properties: inner: $ref: "#/components/schemas/Inner" Inner: type: ["object", "null"] properties: labels: type: ["object", "null"] additionalProperties: { type: "string" }Cause: _create_schemas calls property_from_data with process_properties=False, so that model properties are built later in _process_models. UnionProperty.build splits a type list into one variant per type and calls property_from_data for each, but it did not forward process_properties or roots. The object variant was therefore built immediately, which registered InnerLabelsType0. The same variant is also in models_to_process, so building it again in _process_models registered the same name a second time and failed.
This forwards process_properties and roots from property_from_data through UnionProperty.build to its variants. anyOf/oneOf unions go through the same code path and get the same fix.
Changes:
Testing: