Skip to content

Commit 0b5d74e

Browse files
Ben MartineauBen Martineau
authored andcommitted
Fixes a regression where Enum fields would not propagate keyword arguments to the schema (#2109)
fix #2108 * Fix schema extra not being included when field type is Enum * Code format * More code format * Add changes file Co-authored-by: Ben Martineau <[email protected]>
1 parent c76a5b9 commit 0b5d74e

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

changes/2109-bm424.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a regression where Enum fields would not propagate keyword arguments to the schema

pydantic/schema.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,9 @@ def get_field_schema_validations(field: ModelField) -> Dict[str, Any]:
260260
f_schema: Dict[str, Any] = {}
261261

262262
if lenient_issubclass(field.type_, Enum):
263-
# schema is already updated by `enum_process_schema`
263+
# schema is already updated by `enum_process_schema`; just update with field extra
264+
if field.field_info.extra:
265+
f_schema.update(field.field_info.extra)
264266
return f_schema
265267

266268
if lenient_issubclass(field.type_, (str, bytes)):

tests/test_schema.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,33 @@ class Foo(BaseModel):
374374
}
375375

376376

377+
def test_list_enum_schema_extras():
378+
class FoodChoice(str, Enum):
379+
spam = 'spam'
380+
egg = 'egg'
381+
chips = 'chips'
382+
383+
class Model(BaseModel):
384+
foods: List[FoodChoice] = Field(examples=[['spam', 'egg']])
385+
386+
assert Model.schema() == {
387+
'definitions': {
388+
'FoodChoice': {
389+
'description': 'An enumeration.',
390+
'enum': ['spam', 'egg', 'chips'],
391+
'title': 'FoodChoice',
392+
'type': 'string',
393+
}
394+
},
395+
'properties': {
396+
'foods': {'type': 'array', 'items': {'$ref': '#/definitions/FoodChoice'}, 'examples': [['spam', 'egg']]},
397+
},
398+
'required': ['foods'],
399+
'title': 'Model',
400+
'type': 'object',
401+
}
402+
403+
377404
def test_json_schema():
378405
class Model(BaseModel):
379406
a = b'foobar'

0 commit comments

Comments
 (0)