Skip to content

Commit 23af685

Browse files
committed
fix: reduce complexity and branches
1 parent b143a10 commit 23af685

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

+20-14
Original file line numberDiff line numberDiff line change
@@ -1485,24 +1485,11 @@ def get_openapi_schema(
14851485
get_definitions,
14861486
)
14871487
from aws_lambda_powertools.event_handler.openapi.models import OpenAPI, PathItem, Server, Tag
1488-
from aws_lambda_powertools.event_handler.openapi.pydantic_loader import PYDANTIC_V2
14891488
from aws_lambda_powertools.event_handler.openapi.types import (
14901489
COMPONENT_REF_TEMPLATE,
14911490
)
14921491

1493-
# Pydantic V2 has no support for OpenAPI schema 3.0
1494-
if PYDANTIC_V2 and not openapi_version.startswith("3.1"):
1495-
warnings.warn(
1496-
"You are using Pydantic v2, which is incompatible with OpenAPI schema 3.0. Forcing OpenAPI 3.1",
1497-
stacklevel=2,
1498-
)
1499-
openapi_version = "3.1.0"
1500-
elif not PYDANTIC_V2 and not openapi_version.startswith("3.0"):
1501-
warnings.warn(
1502-
"You are using Pydantic v1, which is incompatible with OpenAPI schema 3.1. Forcing OpenAPI 3.0",
1503-
stacklevel=2,
1504-
)
1505-
openapi_version = "3.0.3"
1492+
openapi_version = self._determine_openapi_version(openapi_version)
15061493

15071494
# Start with the bare minimum required for a valid OpenAPI schema
15081495
info: Dict[str, Any] = {"title": title, "version": version}
@@ -1582,6 +1569,25 @@ def get_openapi_schema(
15821569

15831570
return OpenAPI(**output)
15841571

1572+
@staticmethod
1573+
def _determine_openapi_version(openapi_version):
1574+
from aws_lambda_powertools.event_handler.openapi.pydantic_loader import PYDANTIC_V2
1575+
1576+
# Pydantic V2 has no support for OpenAPI schema 3.0
1577+
if PYDANTIC_V2 and not openapi_version.startswith("3.1"):
1578+
warnings.warn(
1579+
"You are using Pydantic v2, which is incompatible with OpenAPI schema 3.0. Forcing OpenAPI 3.1",
1580+
stacklevel=2,
1581+
)
1582+
openapi_version = "3.1.0"
1583+
elif not PYDANTIC_V2 and not openapi_version.startswith("3.0"):
1584+
warnings.warn(
1585+
"You are using Pydantic v1, which is incompatible with OpenAPI schema 3.1. Forcing OpenAPI 3.0",
1586+
stacklevel=2,
1587+
)
1588+
openapi_version = "3.0.3"
1589+
return openapi_version
1590+
15851591
def get_openapi_json_schema(
15861592
self,
15871593
*,

0 commit comments

Comments
 (0)