Skip to content

Commit

Permalink
Check FastAPI version and apply appropriate method for function wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
QuocDuong1306 committed Sep 12, 2024
1 parent ffdc03a commit de8e4b4
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/extendable_pydantic/_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
)

0 comments on commit de8e4b4

Please sign in to comment.