mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
Fix missing enum extra (#2818)
* Allow passing schema_overrides to get_field_info_schema This function was resetting schema_overrides which prevents **extras on Enum type Fields from being saved to their json schema * Add changes file * Update changes/2697-sammchardy.md Co-authored-by: Eric Jolibois <em.jolibois@gmail.com> Co-authored-by: Sam McHardy <sam.mchardy@psma.com.au> Co-authored-by: Eric Jolibois <em.jolibois@gmail.com>
This commit is contained in:
@@ -0,0 +1 @@
|
||||
`Enum` fields now properly support extra kwargs in schema generation
|
||||
+2
-3
@@ -178,8 +178,7 @@ def model_schema(
|
||||
return m_schema
|
||||
|
||||
|
||||
def get_field_info_schema(field: ModelField) -> Tuple[Dict[str, Any], bool]:
|
||||
schema_overrides = False
|
||||
def get_field_info_schema(field: ModelField, schema_overrides: bool = False) -> Tuple[Dict[str, Any], bool]:
|
||||
|
||||
# If no title is explicitly set, we don't set title in the schema for enums.
|
||||
# The behaviour is the same as `BaseModel` reference, where the default title
|
||||
@@ -836,7 +835,7 @@ def field_singleton_schema( # noqa: C901 (ignore complexity)
|
||||
add_field_type_to_schema(field_type, f_schema)
|
||||
elif lenient_issubclass(field_type, Enum):
|
||||
enum_name = model_name_map[field_type]
|
||||
f_schema, schema_overrides = get_field_info_schema(field)
|
||||
f_schema, schema_overrides = get_field_info_schema(field, schema_overrides)
|
||||
f_schema.update(get_schema_ref(enum_name, ref_prefix, ref_template, schema_overrides))
|
||||
definitions[enum_name] = enum_process_schema(field_type)
|
||||
elif is_namedtuple(field_type):
|
||||
|
||||
@@ -398,6 +398,35 @@ def test_enum_and_model_have_same_behaviour():
|
||||
}
|
||||
|
||||
|
||||
def test_enum_includes_extra_without_other_params():
|
||||
class Names(str, Enum):
|
||||
rick = 'Rick'
|
||||
morty = 'Morty'
|
||||
summer = 'Summer'
|
||||
|
||||
class Foo(BaseModel):
|
||||
enum: Names
|
||||
extra_enum: Names = Field(..., extra='Extra field')
|
||||
|
||||
assert Foo.schema() == {
|
||||
'definitions': {
|
||||
'Names': {
|
||||
'description': 'An enumeration.',
|
||||
'enum': ['Rick', 'Morty', 'Summer'],
|
||||
'title': 'Names',
|
||||
'type': 'string',
|
||||
},
|
||||
},
|
||||
'properties': {
|
||||
'enum': {'$ref': '#/definitions/Names'},
|
||||
'extra_enum': {'allOf': [{'$ref': '#/definitions/Names'}], 'extra': 'Extra field'},
|
||||
},
|
||||
'required': ['enum', 'extra_enum'],
|
||||
'title': 'Foo',
|
||||
'type': 'object',
|
||||
}
|
||||
|
||||
|
||||
def test_list_enum_schema_extras():
|
||||
class FoodChoice(str, Enum):
|
||||
spam = 'spam'
|
||||
|
||||
Reference in New Issue
Block a user