From de8e4b413eeba8d1cd194e7cc9ee91048a5e563d Mon Sep 17 00:00:00 2001 From: Quoc Duong Date: Thu, 12 Sep 2024 11:56:58 +0700 Subject: [PATCH] Check FastAPI version and apply appropriate method for function wrapping --- src/extendable_pydantic/_patch.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/extendable_pydantic/_patch.py b/src/extendable_pydantic/_patch.py index 561873c..8557b98 100644 --- a/src/extendable_pydantic/_patch.py +++ b/src/extendable_pydantic/_patch.py @@ -16,6 +16,8 @@ from typing_extensions import Annotated import wrapt +import pkg_resources +from packaging import version from extendable import context from pydantic import TypeAdapter @@ -66,12 +68,17 @@ def _create_response_field_wrapper(wrapped, instance, args, kwargs): _resolve_model_fields_annotation([field]) return field - # fastapi < 0.112.3 - wrapt.wrap_function_wrapper( - utils, "create_response_field", _create_response_field_wrapper - ) + # Get the installed version of FastAPI + fastapi_version = pkg_resources.get_distribution("fastapi").version - # fastapi >= 0.112.3 - wrapt.wrap_function_wrapper( - utils, "create_model_field", _create_response_field_wrapper - ) + # Compare the version and call the appropriate method + if version.parse(fastapi_version) < version.parse("0.112.3"): + # For fastapi < 0.112.3 + wrapt.wrap_function_wrapper( + utils, "create_response_field", _create_response_field_wrapper + ) + else: + # For fastapi >= 0.112.3 + wrapt.wrap_function_wrapper( + utils, "create_model_field", _create_response_field_wrapper + )