@@ -1484,7 +1484,7 @@ def get_openapi_schema(
1484
1484
get_compat_model_name_map ,
1485
1485
get_definitions ,
1486
1486
)
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
1488
1488
from aws_lambda_powertools .event_handler .openapi .types import (
1489
1489
COMPONENT_REF_TEMPLATE ,
1490
1490
)
@@ -1504,23 +1504,12 @@ def get_openapi_schema(
1504
1504
1505
1505
info .update ({field : value for field , value in optional_fields .items () if value })
1506
1506
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
+ }
1524
1513
1525
1514
components : Dict [str , Dict [str , Any ]] = {}
1526
1515
paths : Dict [str , Dict [str , Any ]] = {}
@@ -1569,6 +1558,31 @@ def get_openapi_schema(
1569
1558
1570
1559
return OpenAPI (** output )
1571
1560
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
+
1572
1586
@staticmethod
1573
1587
def _determine_openapi_version (openapi_version ):
1574
1588
from aws_lambda_powertools .event_handler .openapi .pydantic_loader import PYDANTIC_V2
0 commit comments