Skip to content

Commit

Permalink
adds test case for unexpected discriminated union behavior (#9236)
Browse files Browse the repository at this point in the history
  • Loading branch information
kf-novi committed Apr 23, 2024
1 parent c33b925 commit 73d1049
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions tests/test_discriminated_union.py
Expand Up @@ -2031,3 +2031,29 @@ class Cat(BaseModel, Generic[T]):
assert ta.validate_python(int_dog).id == 1
assert ta.validate_python(int_dog).friends[0].id == 2
assert ta.validate_python(int_dog).friends[1].id == 3


@pytest.mark.xfail(
reason='model_dump does not properly serialize the discriminator field to string if it is using an Enum. Issue: https://github.com/pydantic/pydantic/issues/9235'
)
def test_discriminated_union_model_dump_with_nested_class():
class SomeEnum(str, Enum):
CAT = 'cat'
DOG = 'dog'

class Dog(BaseModel):
type: Literal[SomeEnum.DOG] = SomeEnum.DOG
name: str

class Cat(BaseModel):
type: Literal[SomeEnum.CAT] = SomeEnum.CAT
name: str

class Yard(BaseModel):
pet: Union[Dog, Cat] = Field(discriminator='type')

yard = Yard(pet=Dog(name='Rex'))
yard_dict = yard.model_dump(mode='json')
assert isinstance(yard_dict['pet']['type'], str)
assert not isinstance(yard_dict['pet']['type'], SomeEnum)
assert str(yard_dict['pet']['type']) == 'dog'

0 comments on commit 73d1049

Please sign in to comment.