From 74aa5581d3d297c4b741ac4ac038dc0e994498bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B5=E9=92=8A=E6=98=8E?= <40025941+0x587@users.noreply.github.com> Date: Thu, 17 Aug 2023 23:53:52 +0800 Subject: [PATCH 1/4] Fix bugs that are not compatible with pydantic2.x --- fastapi_crudrouter/core/_utils.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/fastapi_crudrouter/core/_utils.py b/fastapi_crudrouter/core/_utils.py index ef3562e4..4bdfa864 100644 --- a/fastapi_crudrouter/core/_utils.py +++ b/fastapi_crudrouter/core/_utils.py @@ -26,11 +26,23 @@ def schema_factory( Is used to create a CreateSchema which does not contain pk """ - fields = { - f.name: (f.type_, ...) - for f in schema_cls.__fields__.values() - if f.name != pk_field_name - } + # for handle pydantic 2.x migration + from pydantic import __version__ as pydantic_version + + if int(pydantic_version.split(".")[0]) >= 2: + # pydantic 2.x + fields = { + fk: (fv.annotation, ...) + for fk, fv in schema_cls.model_fields.items() + if fk != pk_field_name + } + else: + # pydantic 1.x + fields = { + f.name: (f.type_, ...) + for f in schema_cls.__fields__.values() + if f.name != pk_field_name + } name = schema_cls.__name__ + name schema: Type[T] = create_model(__model_name=name, **fields) # type: ignore From 6fbf927d2b9c3ab72486da3d10c94d45e7359c2f Mon Sep 17 00:00:00 2001 From: ChiuMing SIU <40025941+0x587@users.noreply.github.com> Date: Wed, 13 Sep 2023 18:23:21 +0800 Subject: [PATCH 2/4] Update _utils.py --- fastapi_crudrouter/core/_utils.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/fastapi_crudrouter/core/_utils.py b/fastapi_crudrouter/core/_utils.py index 4bdfa864..0c997eae 100644 --- a/fastapi_crudrouter/core/_utils.py +++ b/fastapi_crudrouter/core/_utils.py @@ -14,7 +14,10 @@ def __init__(self, *args, **kwargs) -> None: # type: ignore def get_pk_type(schema: Type[PYDANTIC_SCHEMA], pk_field: str) -> Any: try: - return schema.__fields__[pk_field].type_ + if int(pydantic_version.split(".")[0]) >= 2: + return schema.model_fields[pk_field].annotation + else: + return schema.__fields__[pk_field].type_ except KeyError: return int From 473f4011a73c7a430bee64d572330acf46895859 Mon Sep 17 00:00:00 2001 From: ChiuMing SIU <40025941+0x587@users.noreply.github.com> Date: Tue, 24 Oct 2023 13:26:30 +0000 Subject: [PATCH 3/4] Supplement the import of pydantic version located in get_pk_type --- fastapi_crudrouter/core/_utils.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/fastapi_crudrouter/core/_utils.py b/fastapi_crudrouter/core/_utils.py index 0c997eae..7088bcc6 100644 --- a/fastapi_crudrouter/core/_utils.py +++ b/fastapi_crudrouter/core/_utils.py @@ -14,6 +14,9 @@ def __init__(self, *args, **kwargs) -> None: # type: ignore def get_pk_type(schema: Type[PYDANTIC_SCHEMA], pk_field: str) -> Any: try: + # for handle pydantic 2.x migration + from pydantic import __version__ as pydantic_version + if int(pydantic_version.split(".")[0]) >= 2: return schema.model_fields[pk_field].annotation else: From df78b24ed397d04575d3f38b8b7e71ae35400a28 Mon Sep 17 00:00:00 2001 From: ChiuMing SIU <40025941+0x587@users.noreply.github.com> Date: Wed, 1 Nov 2023 12:10:29 +0000 Subject: [PATCH 4/4] import at top --- fastapi_crudrouter/core/_utils.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/fastapi_crudrouter/core/_utils.py b/fastapi_crudrouter/core/_utils.py index 7088bcc6..e7eb94a1 100644 --- a/fastapi_crudrouter/core/_utils.py +++ b/fastapi_crudrouter/core/_utils.py @@ -2,9 +2,11 @@ from fastapi import Depends, HTTPException from pydantic import create_model +from pydantic import __version__ as pydantic_version from ._types import T, PAGINATION, PYDANTIC_SCHEMA +PYDANTIC_MAJOR_VERSION = int(pydantic_version.split(".", maxsplit=1)[0]) class AttrDict(dict): # type: ignore def __init__(self, *args, **kwargs) -> None: # type: ignore @@ -14,10 +16,7 @@ def __init__(self, *args, **kwargs) -> None: # type: ignore def get_pk_type(schema: Type[PYDANTIC_SCHEMA], pk_field: str) -> Any: try: - # for handle pydantic 2.x migration - from pydantic import __version__ as pydantic_version - - if int(pydantic_version.split(".")[0]) >= 2: + if PYDANTIC_MAJOR_VERSION >= 2: return schema.model_fields[pk_field].annotation else: return schema.__fields__[pk_field].type_ @@ -32,10 +31,7 @@ def schema_factory( Is used to create a CreateSchema which does not contain pk """ - # for handle pydantic 2.x migration - from pydantic import __version__ as pydantic_version - - if int(pydantic_version.split(".")[0]) >= 2: + if PYDANTIC_MAJOR_VERSION >= 2: # pydantic 2.x fields = { fk: (fv.annotation, ...)