From 3e85d2cda27e48858a57400cea4dfb0ac12eae3c Mon Sep 17 00:00:00 2001 From: JONEMI19 Date: Fri, 7 Jul 2023 19:24:24 +0000 Subject: [PATCH] add pydantic v1 support --- fastapi/_compat.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/fastapi/_compat.py b/fastapi/_compat.py index 8c76a8011..1d9a3738e 100644 --- a/fastapi/_compat.py +++ b/fastapi/_compat.py @@ -331,6 +331,10 @@ else: SHAPE_SEQUENCE, SHAPE_TUPLE_ELLIPSIS, } + + mapping_shapes = { + SHAPE_MAPPING, + } sequence_shape_to_type = { SHAPE_LIST: list, SHAPE_SET: set, @@ -405,6 +409,32 @@ else: if _annotation_is_sequence(field.type_): return True return False + + def is_pv1_scalar_mapping_field(field: ModelField) -> bool: + if (field.shape in mapping_shapes) and not lenient_issubclass( + field.type_, BaseModel + ): + if field.sub_fields is None: + return False + for sub_field in field.sub_fields: + if not is_scalar_field(sub_field): + return False + return True + return False + + + def is_pv1_scalar_sequence_mapping_field(field: ModelField) -> bool: + if (field.shape in mapping_shapes) and not lenient_issubclass( + field.type_, BaseModel + ): + if field.sub_fields is None: + return False + for sub_field in field.sub_fields: + if not is_scalar_sequence_field(sub_field): + return False + return True + return False + def _normalize_errors(errors: Sequence[Any]) -> List[Dict[str, Any]]: use_errors: List[Any] = []