FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix: Process union variant properties once for nullable top-level schemas by dmarin-pixel · Pull Request #1518 · openapi-generators/openapi-python-client · GitHub

Repository navigation

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .md  (1) .py  (3) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
default: patch
---

# Fix duplicate model error for nullable top-level object schemas

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.
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
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,32 @@ def test_type_hints(self, ModelWithUnion, ModelWithRequiredUnion, ModelWithUnion
)
assert_model_property_type_hint(ModelWithUnionOfOne, "thing", "ThingA | Unset")
assert_model_property_type_hint(ModelWithUnionOfOne, "required_thing", "ThingA")


@with_generated_client_fixture(
"""
components:
schemas:
Outer:
type: object
properties:
inner:
$ref: "#/components/schemas/Inner"
Inner:
type: ["object", "null"]
properties:
labels:
type: ["object", "null"]
additionalProperties: { type: "string" }
"""
)
@with_generated_code_imports(
".models.Outer",
".models.InnerType0",
)
class TestNullableTopLevelObjectWithNullableProperty:
def test_decode_encode(self, Outer, InnerType0):
data = {"inner": {"labels": {"a": "b"}}}
outer = Outer.from_dict(data)
assert isinstance(outer.inner, InnerType0)
assert outer.to_dict() == data
2 changes: 2 additions & 0 deletions openapi_python_client/parser/properties/__init__.py
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
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ def property_from_data( # noqa: PLR0911, PLR0912
schemas=schemas,
parent_name=parent_name,
config=config,
process_properties=process_properties,
roots=roots,
)
if data.const is not None:
return (
Expand Down
8 changes: 7 additions & 1 deletion openapi_python_client/parser/properties/union.py
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from ...strings import PythonIdentifier
from ..errors import ParseError, PropertyError
from .protocol import PropertyProtocol, Value, convert_example
from .schemas import Schemas
from .schemas import ReferencePath, Schemas


@define
Expand All @@ -37,6 +37,8 @@ def build(
schemas: Schemas,
parent_name: str,
config: Config,
process_properties: bool = True,
roots: set[ReferencePath | strings.ClassName] | None = None,
) -> tuple[UnionProperty | PropertyError, Schemas]:
"""
Create a `UnionProperty` the right way.
Expand All @@ -48,6 +50,8 @@ def build(
schemas: The `Schemas` so far describing existing classes / references.
parent_name: The name of the thing which holds this property (used for renaming inner classes).
config: User-defined config values for modifying inner properties.
process_properties: Passed to inner properties; False defers building inner model properties.
roots: Passed to inner properties; schema objects the new properties depend on.

Returns:
`(result, schemas)` where `schemas` is the updated version of the input `schemas` and `result` is the
Expand Down Expand Up @@ -81,6 +85,8 @@ def build(
schemas=schemas,
parent_name=parent_name,
config=config,
process_properties=process_properties,
roots=roots,
)
if isinstance(sub_prop, PropertyError):
return (
Expand Down

Back | FazBrowse Home | New Git URL