Skip to content

Commit

Permalink
Fix an intriduced bug in parsing json field with discriminated union (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
hramezani committed Jun 13, 2024
1 parent b5d4534 commit abe7cc5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pydantic_settings/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -1597,8 +1597,8 @@ def _annotation_is_complex(annotation: type[Any] | None, metadata: list[Any]) ->
# Check if annotation is of the form Annotated[type, metadata].
if isinstance(annotation, _AnnotatedAlias):
# Return result of recursive call on inner type.
inner, meta = get_args(annotation)
return _annotation_is_complex(inner, [meta])
inner, *meta = get_args(annotation)
return _annotation_is_complex(inner, meta)
origin = get_origin(annotation)
return (
_annotation_is_complex_inner(annotation)
Expand Down
20 changes: 20 additions & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -1767,6 +1767,26 @@ class Settings(BaseSettings):
assert s.a_or_b.y == 'foo'


def test_json_field_with_discriminated_union(env):
class A(BaseModel):
x: Literal['a'] = 'a'

class B(BaseModel):
x: Literal['b'] = 'b'

A_OR_B = Annotated[Union[A, B], Field(discriminator='x')]

class Settings(BaseSettings):
a_or_b: Optional[Json[A_OR_B]] = None

# Set up environment so that the discriminator is 'a'.
env.set('a_or_b', '{"x": "a"}')

s = Settings()

assert s.a_or_b.x == 'a'


def test_nested_model_case_insensitive(env):
class SubSubSub(BaseModel):
VaL3: str
Expand Down

0 comments on commit abe7cc5

Please sign in to comment.