From eef4ac505ed1c2356315b59eb31b19b7ab9d4e34 Mon Sep 17 00:00:00 2001 From: Sam McHardy Date: Wed, 8 Dec 2021 01:10:53 +1100 Subject: [PATCH] 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 Co-authored-by: Sam McHardy Co-authored-by: Eric Jolibois --- changes/2697-sammchardy.md | 1 + pydantic/schema.py | 5 ++--- tests/test_schema.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 changes/2697-sammchardy.md diff --git a/changes/2697-sammchardy.md b/changes/2697-sammchardy.md new file mode 100644 index 0000000..72ce6e0 --- /dev/null +++ b/changes/2697-sammchardy.md @@ -0,0 +1 @@ +`Enum` fields now properly support extra kwargs in schema generation \ No newline at end of file diff --git a/pydantic/schema.py b/pydantic/schema.py index b5d357a..4f6a343 100644 --- a/pydantic/schema.py +++ b/pydantic/schema.py @@ -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): diff --git a/tests/test_schema.py b/tests/test_schema.py index 2b8f831..e7f7e6d 100644 --- a/tests/test_schema.py +++ b/tests/test_schema.py @@ -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'