mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
Replacing encode_default instance check with strict type check (#3197)
* Replacing encode_default instance check with strict type check * Adding change notes * Changing to dictionary indexing in tests * Adding explicit Enum check and returning its value
This commit is contained in:
@@ -0,0 +1 @@
|
||||
Always use `Enum` value as default in generated JSON schema.
|
||||
+3
-1
@@ -922,7 +922,9 @@ def multitypes_literal_field_for_schema(values: Tuple[Any, ...], field: ModelFie
|
||||
|
||||
|
||||
def encode_default(dft: Any) -> Any:
|
||||
if isinstance(dft, (int, float, str)):
|
||||
if isinstance(dft, Enum):
|
||||
return dft.value
|
||||
elif isinstance(dft, (int, float, str)):
|
||||
return dft
|
||||
elif sequence_like(dft):
|
||||
t = dft.__class__
|
||||
|
||||
@@ -1435,6 +1435,26 @@ def test_list_default():
|
||||
}
|
||||
|
||||
|
||||
def test_enum_str_default():
|
||||
class MyEnum(str, Enum):
|
||||
FOO = 'foo'
|
||||
|
||||
class UserModel(BaseModel):
|
||||
friends: MyEnum = MyEnum.FOO
|
||||
|
||||
assert UserModel.schema()['properties']['friends']['default'] is MyEnum.FOO.value
|
||||
|
||||
|
||||
def test_enum_int_default():
|
||||
class MyEnum(IntEnum):
|
||||
FOO = 1
|
||||
|
||||
class UserModel(BaseModel):
|
||||
friends: MyEnum = MyEnum.FOO
|
||||
|
||||
assert UserModel.schema()['properties']['friends']['default'] is MyEnum.FOO.value
|
||||
|
||||
|
||||
def test_dict_default():
|
||||
class UserModel(BaseModel):
|
||||
friends: Dict[str, float] = {'a': 1.1, 'b': 2.2}
|
||||
|
||||
Reference in New Issue
Block a user