mirror of
https://github.com/anomalyco/opencode-sdk-python.git
synced 2026-08-16 12:04:48 +00:00
fix(parsing): correctly handle nested discriminated unions
This commit is contained in:
parent
88921d3d34
commit
693c53b857
2 changed files with 53 additions and 5 deletions
|
|
@ -889,3 +889,48 @@ def test_discriminated_union_case() -> None:
|
|||
)
|
||||
|
||||
assert isinstance(m, ModelB)
|
||||
|
||||
|
||||
def test_nested_discriminated_union() -> None:
|
||||
class InnerType1(BaseModel):
|
||||
type: Literal["type_1"]
|
||||
|
||||
class InnerModel(BaseModel):
|
||||
inner_value: str
|
||||
|
||||
class InnerType2(BaseModel):
|
||||
type: Literal["type_2"]
|
||||
some_inner_model: InnerModel
|
||||
|
||||
class Type1(BaseModel):
|
||||
base_type: Literal["base_type_1"]
|
||||
value: Annotated[
|
||||
Union[
|
||||
InnerType1,
|
||||
InnerType2,
|
||||
],
|
||||
PropertyInfo(discriminator="type"),
|
||||
]
|
||||
|
||||
class Type2(BaseModel):
|
||||
base_type: Literal["base_type_2"]
|
||||
|
||||
T = Annotated[
|
||||
Union[
|
||||
Type1,
|
||||
Type2,
|
||||
],
|
||||
PropertyInfo(discriminator="base_type"),
|
||||
]
|
||||
|
||||
model = construct_type(
|
||||
type_=T,
|
||||
value={
|
||||
"base_type": "base_type_1",
|
||||
"value": {
|
||||
"type": "type_2",
|
||||
},
|
||||
},
|
||||
)
|
||||
assert isinstance(model, Type1)
|
||||
assert isinstance(model.value, InnerType2)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue