Skip to content

Commit abe7cc5

Browse files
authored
Fix an intriduced bug in parsing json field with discriminated union (#312)
1 parent b5d4534 commit abe7cc5

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

pydantic_settings/sources.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,8 +1597,8 @@ def _annotation_is_complex(annotation: type[Any] | None, metadata: list[Any]) ->
15971597
# Check if annotation is of the form Annotated[type, metadata].
15981598
if isinstance(annotation, _AnnotatedAlias):
15991599
# Return result of recursive call on inner type.
1600-
inner, meta = get_args(annotation)
1601-
return _annotation_is_complex(inner, [meta])
1600+
inner, *meta = get_args(annotation)
1601+
return _annotation_is_complex(inner, meta)
16021602
origin = get_origin(annotation)
16031603
return (
16041604
_annotation_is_complex_inner(annotation)

tests/test_settings.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,6 +1767,26 @@ class Settings(BaseSettings):
17671767
assert s.a_or_b.y == 'foo'
17681768

17691769

1770+
def test_json_field_with_discriminated_union(env):
1771+
class A(BaseModel):
1772+
x: Literal['a'] = 'a'
1773+
1774+
class B(BaseModel):
1775+
x: Literal['b'] = 'b'
1776+
1777+
A_OR_B = Annotated[Union[A, B], Field(discriminator='x')]
1778+
1779+
class Settings(BaseSettings):
1780+
a_or_b: Optional[Json[A_OR_B]] = None
1781+
1782+
# Set up environment so that the discriminator is 'a'.
1783+
env.set('a_or_b', '{"x": "a"}')
1784+
1785+
s = Settings()
1786+
1787+
assert s.a_or_b.x == 'a'
1788+
1789+
17701790
def test_nested_model_case_insensitive(env):
17711791
class SubSubSub(BaseModel):
17721792
VaL3: str

0 commit comments

Comments
 (0)