mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
fix: access discriminator field on BaseModel instance using key (#3847)
When validating a discriminated union where the union value has been passed as a Pydantic model instance, we should access the discriminator field value using the field name and not the field alias (whether one is set or not).
This commit is contained in:
@@ -267,6 +267,24 @@ def test_discriminated_union_basemodel_instance_value():
|
||||
assert isinstance(t, Top)
|
||||
|
||||
|
||||
def test_discriminated_union_basemodel_instance_value_with_alias():
|
||||
class A(BaseModel):
|
||||
literal: Literal['a'] = Field(alias='lit')
|
||||
|
||||
class B(BaseModel):
|
||||
literal: Literal['b'] = Field(alias='lit')
|
||||
|
||||
class Config:
|
||||
allow_population_by_field_name = True
|
||||
|
||||
class Top(BaseModel):
|
||||
sub: Union[A, B] = Field(..., discriminator='literal')
|
||||
|
||||
assert Top(sub=A(lit='a')).sub.literal == 'a'
|
||||
assert Top(sub=B(lit='b')).sub.literal == 'b'
|
||||
assert Top(sub=B(literal='b')).sub.literal == 'b'
|
||||
|
||||
|
||||
def test_discriminated_union_int():
|
||||
class A(BaseModel):
|
||||
l: Literal[1]
|
||||
|
||||
Reference in New Issue
Block a user