From d560c57dbcfe91e9f67782ede3e871ce19b88373 Mon Sep 17 00:00:00 2001 From: PrettyWood Date: Tue, 26 May 2020 11:05:49 +0200 Subject: [PATCH] (fix) use right type check when modifying schema fix #1552 --- changes/1552-PrettyWood.md | 1 + pydantic/schema.py | 2 +- tests/test_schema.py | 4 +++- 3 files changed, 5 insertions(+), 2 deletions(-) create mode 100644 changes/1552-PrettyWood.md diff --git a/changes/1552-PrettyWood.md b/changes/1552-PrettyWood.md new file mode 100644 index 0000000000..e8427696a6 --- /dev/null +++ b/changes/1552-PrettyWood.md @@ -0,0 +1 @@ +Call `__modify_schema__` only for the field schema diff --git a/pydantic/schema.py b/pydantic/schema.py index 3ca516a906..f4dfaeef89 100644 --- a/pydantic/schema.py +++ b/pydantic/schema.py @@ -239,7 +239,7 @@ def get_field_schema_validations(field: ModelField) -> Dict[str, Any]: f_schema['const'] = field.default if field.field_info.extra: f_schema.update(field.field_info.extra) - modify_schema = getattr(field.type_, '__modify_schema__', None) + modify_schema = getattr(field.outer_type_, '__modify_schema__', None) if modify_schema: modify_schema(f_schema) return f_schema diff --git a/tests/test_schema.py b/tests/test_schema.py index ce447d5ec7..50708adeed 100644 --- a/tests/test_schema.py +++ b/tests/test_schema.py @@ -1822,6 +1822,7 @@ def __modify_schema__(cls, schema): class Model(BaseModel): path1: Path path2: MyPath + path3: List[MyPath] assert Model.schema() == { 'title': 'Model', @@ -1829,8 +1830,9 @@ class Model(BaseModel): 'properties': { 'path1': {'title': 'Path1', 'type': 'string', 'format': 'path'}, 'path2': {'title': 'Path2', 'type': 'string', 'format': 'path', 'foobar': 123}, + 'path3': {'title': 'Path3', 'type': 'array', 'items': {'type': 'string', 'format': 'path', 'foobar': 123}}, }, - 'required': ['path1', 'path2'], + 'required': ['path1', 'path2', 'path3'], }