Skip to content

Commit 2526d84

Browse files
committed
fix: Process union variant properties once for nullable top-level schemas
1 parent 46a4340 commit 2526d84

4 files changed

Lines changed: 45 additions & 1 deletion

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
default: patch
3+
---
4+
5+
# Fix duplicate model error for nullable top-level object schemas
6+
7+
A schema in `components/schemas` with `type: ["object", "null"]` and a nested model, such as a property that is itself a nullable object, was dropped with "Attempted to generate duplicate models". The nested model was built twice because union variants ignored the deferred processing used while schemas are created. Variants are now built once.

‎end_to_end_tests/functional_tests/generated_code_execution/test_unions.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,32 @@ def test_type_hints(self, ModelWithUnion, ModelWithRequiredUnion, ModelWithUnion
141141
)
142142
assert_model_property_type_hint(ModelWithUnionOfOne, "thing", "ThingA | Unset")
143143
assert_model_property_type_hint(ModelWithUnionOfOne, "required_thing", "ThingA")
144+
145+
146+
@with_generated_client_fixture(
147+
"""
148+
components:
149+
schemas:
150+
Outer:
151+
type: object
152+
properties:
153+
inner:
154+
$ref: "#/components/schemas/Inner"
155+
Inner:
156+
type: ["object", "null"]
157+
properties:
158+
labels:
159+
type: ["object", "null"]
160+
additionalProperties: { type: "string" }
161+
"""
162+
)
163+
@with_generated_code_imports(
164+
".models.Outer",
165+
".models.InnerType0",
166+
)
167+
class TestNullableTopLevelObjectWithNullableProperty:
168+
def test_decode_encode(self, Outer, InnerType0):
169+
data = {"inner": {"labels": {"a": "b"}}}
170+
outer = Outer.from_dict(data)
171+
assert isinstance(outer.inner, InnerType0)
172+
assert outer.to_dict() == data

‎openapi_python_client/parser/properties/__init__.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ def property_from_data( # noqa: PLR0911, PLR0912
225225
schemas=schemas,
226226
parent_name=parent_name,
227227
config=config,
228+
process_properties=process_properties,
229+
roots=roots,
228230
)
229231
if data.const is not None:
230232
return (

‎openapi_python_client/parser/properties/union.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from ...strings import PythonIdentifier
1212
from ..errors import ParseError, PropertyError
1313
from .protocol import PropertyProtocol, Value, convert_example
14-
from .schemas import Schemas
14+
from .schemas import ReferencePath, Schemas
1515

1616

1717
@define
@@ -37,6 +37,8 @@ def build(
3737
schemas: Schemas,
3838
parent_name: str,
3939
config: Config,
40+
process_properties: bool = True,
41+
roots: set[ReferencePath | strings.ClassName] | None = None,
4042
) -> tuple[UnionProperty | PropertyError, Schemas]:
4143
"""
4244
Create a `UnionProperty` the right way.
@@ -48,6 +50,8 @@ def build(
4850
schemas: The `Schemas` so far describing existing classes / references.
4951
parent_name: The name of the thing which holds this property (used for renaming inner classes).
5052
config: User-defined config values for modifying inner properties.
53+
process_properties: Passed to inner properties; False defers building inner model properties.
54+
roots: Passed to inner properties; schema objects the new properties depend on.
5155
5256
Returns:
5357
`(result, schemas)` where `schemas` is the updated version of the input `schemas` and `result` is the
@@ -81,6 +85,8 @@ def build(
8185
schemas=schemas,
8286
parent_name=parent_name,
8387
config=config,
88+
process_properties=process_properties,
89+
roots=roots,
8490
)
8591
if isinstance(sub_prop, PropertyError):
8692
return (

0 commit comments

Comments
 (0)