Skip to content

Commit a0d5449

Browse files
committed
fix: complexity
1 parent 0a0b776 commit a0d5449

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

+32-18
Original file line numberDiff line numberDiff line change
@@ -1484,7 +1484,7 @@ def get_openapi_schema(
14841484
get_compat_model_name_map,
14851485
get_definitions,
14861486
)
1487-
from aws_lambda_powertools.event_handler.openapi.models import OpenAPI, PathItem, Server, Tag
1487+
from aws_lambda_powertools.event_handler.openapi.models import OpenAPI, PathItem, Tag
14881488
from aws_lambda_powertools.event_handler.openapi.types import (
14891489
COMPONENT_REF_TEMPLATE,
14901490
)
@@ -1504,23 +1504,12 @@ def get_openapi_schema(
15041504

15051505
info.update({field: value for field, value in optional_fields.items() if value})
15061506

1507-
output: Dict[str, Any] = {"openapi": openapi_version, "info": info}
1508-
if servers:
1509-
output["servers"] = servers
1510-
else:
1511-
# If the servers property is not provided, or is an empty array, the default value would be a Server Object
1512-
# with an url value of /.
1513-
output["servers"] = [Server(url="/")]
1514-
1515-
if security:
1516-
if not security_schemes:
1517-
raise ValueError("security_schemes must be provided if security is provided")
1518-
1519-
# Check if all keys in security are present in the security_schemes
1520-
if not all(key in security_schemes for sec in security for key in sec):
1521-
raise ValueError("Some security schemes not found in security_schemes")
1522-
1523-
output["security"] = security
1507+
output: Dict[str, Any] = {
1508+
"openapi": openapi_version,
1509+
"info": info,
1510+
"servers": self._get_openapi_servers(servers),
1511+
"security": self._get_openapi_security(security, security_schemes),
1512+
}
15241513

15251514
components: Dict[str, Dict[str, Any]] = {}
15261515
paths: Dict[str, Dict[str, Any]] = {}
@@ -1569,6 +1558,31 @@ def get_openapi_schema(
15691558

15701559
return OpenAPI(**output)
15711560

1561+
@staticmethod
1562+
def _get_openapi_servers(servers: Optional[List["Server"]]) -> List["Server"]:
1563+
from aws_lambda_powertools.event_handler.openapi.models import Server
1564+
1565+
# If the 'servers' property is not provided or is an empty array,
1566+
# the default behavior is to return a Server Object with a URL value of "/".
1567+
return servers if servers else [Server(url="/")]
1568+
1569+
@staticmethod
1570+
def _get_openapi_security(
1571+
security: Optional[List[Dict[str, List[str]]]],
1572+
security_schemes: Optional[Dict[str, "SecurityScheme"]],
1573+
) -> Optional[List[Dict[str, List[str]]]]:
1574+
if security:
1575+
if not security_schemes:
1576+
raise ValueError("security_schemes must be provided if security is provided")
1577+
1578+
# Check if all keys in security are present in the security_schemes
1579+
if not all(key in security_schemes for sec in security for key in sec):
1580+
raise ValueError("Some security schemes not found in security_schemes")
1581+
1582+
return security
1583+
else:
1584+
return None
1585+
15721586
@staticmethod
15731587
def _determine_openapi_version(openapi_version):
15741588
from aws_lambda_powertools.event_handler.openapi.pydantic_loader import PYDANTIC_V2

0 commit comments

Comments
 (0)