@@ -323,6 +323,7 @@ def __init__(
323
323
operation_id : Optional [str ] = None ,
324
324
include_in_schema : bool = True ,
325
325
security : Optional [List [Dict [str , List [str ]]]] = None ,
326
+ openapi_extensions : Optional [Dict [str , Any ]] = None ,
326
327
middlewares : Optional [List [Callable [..., Response ]]] = None ,
327
328
):
328
329
"""
@@ -360,6 +361,8 @@ def __init__(
360
361
Whether or not to include this route in the OpenAPI schema
361
362
security: List[Dict[str, List[str]]], optional
362
363
The OpenAPI security for this route
364
+ openapi_extensions: Dict[str, Any], optional
365
+ Additional OpenAPI extensions as a dictionary.
363
366
middlewares: Optional[List[Callable[..., Response]]]
364
367
The list of route middlewares to be called in order.
365
368
"""
@@ -383,6 +386,7 @@ def __init__(
383
386
self .tags = tags or []
384
387
self .include_in_schema = include_in_schema
385
388
self .security = security
389
+ self .openapi_extensions = openapi_extensions
386
390
self .middlewares = middlewares or []
387
391
self .operation_id = operation_id or self ._generate_operation_id ()
388
392
@@ -534,6 +538,10 @@ def _get_openapi_path(
534
538
if self .security :
535
539
operation ["security" ] = self .security
536
540
541
+ # Add OpenAPI extensions if present
542
+ if self .openapi_extensions :
543
+ operation .update (self .openapi_extensions )
544
+
537
545
# Add the parameters to the OpenAPI operation
538
546
if parameters :
539
547
all_parameters = {(param ["in" ], param ["name" ]): param for param in parameters }
@@ -939,6 +947,7 @@ def route(
939
947
operation_id : Optional [str ] = None ,
940
948
include_in_schema : bool = True ,
941
949
security : Optional [List [Dict [str , List [str ]]]] = None ,
950
+ openapi_extensions : Optional [Dict [str , Any ]] = None ,
942
951
middlewares : Optional [List [Callable [..., Any ]]] = None ,
943
952
):
944
953
raise NotImplementedError ()
@@ -998,6 +1007,7 @@ def get(
998
1007
operation_id : Optional [str ] = None ,
999
1008
include_in_schema : bool = True ,
1000
1009
security : Optional [List [Dict [str , List [str ]]]] = None ,
1010
+ openapi_extensions : Optional [Dict [str , Any ]] = None ,
1001
1011
middlewares : Optional [List [Callable [..., Any ]]] = None ,
1002
1012
):
1003
1013
"""Get route decorator with GET `method`
@@ -1036,6 +1046,7 @@ def lambda_handler(event, context):
1036
1046
operation_id ,
1037
1047
include_in_schema ,
1038
1048
security ,
1049
+ openapi_extensions ,
1039
1050
middlewares ,
1040
1051
)
1041
1052
@@ -1053,6 +1064,7 @@ def post(
1053
1064
operation_id : Optional [str ] = None ,
1054
1065
include_in_schema : bool = True ,
1055
1066
security : Optional [List [Dict [str , List [str ]]]] = None ,
1067
+ openapi_extensions : Optional [Dict [str , Any ]] = None ,
1056
1068
middlewares : Optional [List [Callable [..., Any ]]] = None ,
1057
1069
):
1058
1070
"""Post route decorator with POST `method`
@@ -1092,6 +1104,7 @@ def lambda_handler(event, context):
1092
1104
operation_id ,
1093
1105
include_in_schema ,
1094
1106
security ,
1107
+ openapi_extensions ,
1095
1108
middlewares ,
1096
1109
)
1097
1110
@@ -1109,6 +1122,7 @@ def put(
1109
1122
operation_id : Optional [str ] = None ,
1110
1123
include_in_schema : bool = True ,
1111
1124
security : Optional [List [Dict [str , List [str ]]]] = None ,
1125
+ openapi_extensions : Optional [Dict [str , Any ]] = None ,
1112
1126
middlewares : Optional [List [Callable [..., Any ]]] = None ,
1113
1127
):
1114
1128
"""Put route decorator with PUT `method`
@@ -1148,6 +1162,7 @@ def lambda_handler(event, context):
1148
1162
operation_id ,
1149
1163
include_in_schema ,
1150
1164
security ,
1165
+ openapi_extensions ,
1151
1166
middlewares ,
1152
1167
)
1153
1168
@@ -1165,6 +1180,7 @@ def delete(
1165
1180
operation_id : Optional [str ] = None ,
1166
1181
include_in_schema : bool = True ,
1167
1182
security : Optional [List [Dict [str , List [str ]]]] = None ,
1183
+ openapi_extensions : Optional [Dict [str , Any ]] = None ,
1168
1184
middlewares : Optional [List [Callable [..., Any ]]] = None ,
1169
1185
):
1170
1186
"""Delete route decorator with DELETE `method`
@@ -1203,6 +1219,7 @@ def lambda_handler(event, context):
1203
1219
operation_id ,
1204
1220
include_in_schema ,
1205
1221
security ,
1222
+ openapi_extensions ,
1206
1223
middlewares ,
1207
1224
)
1208
1225
@@ -1220,6 +1237,7 @@ def patch(
1220
1237
operation_id : Optional [str ] = None ,
1221
1238
include_in_schema : bool = True ,
1222
1239
security : Optional [List [Dict [str , List [str ]]]] = None ,
1240
+ openapi_extensions : Optional [Dict [str , Any ]] = None ,
1223
1241
middlewares : Optional [List [Callable ]] = None ,
1224
1242
):
1225
1243
"""Patch route decorator with PATCH `method`
@@ -1261,6 +1279,7 @@ def lambda_handler(event, context):
1261
1279
operation_id ,
1262
1280
include_in_schema ,
1263
1281
security ,
1282
+ openapi_extensions ,
1264
1283
middlewares ,
1265
1284
)
1266
1285
@@ -1278,6 +1297,7 @@ def head(
1278
1297
operation_id : Optional [str ] = None ,
1279
1298
include_in_schema : bool = True ,
1280
1299
security : Optional [List [Dict [str , List [str ]]]] = None ,
1300
+ openapi_extensions : Optional [Dict [str , Any ]] = None ,
1281
1301
middlewares : Optional [List [Callable ]] = None ,
1282
1302
):
1283
1303
"""Head route decorator with HEAD `method`
@@ -1318,6 +1338,7 @@ def lambda_handler(event, context):
1318
1338
operation_id ,
1319
1339
include_in_schema ,
1320
1340
security ,
1341
+ openapi_extensions ,
1321
1342
middlewares ,
1322
1343
)
1323
1344
@@ -1541,6 +1562,7 @@ def get_openapi_schema(
1541
1562
license_info : Optional ["License" ] = None ,
1542
1563
security_schemes : Optional [Dict [str , "SecurityScheme" ]] = None ,
1543
1564
security : Optional [List [Dict [str , List [str ]]]] = None ,
1565
+ openapi_extensions : Optional [Dict [str , Any ]] = None ,
1544
1566
) -> "OpenAPI" :
1545
1567
"""
1546
1568
Returns the OpenAPI schema as a pydantic model.
@@ -1571,6 +1593,8 @@ def get_openapi_schema(
1571
1593
A declaration of the security schemes available to be used in the specification.
1572
1594
security: List[Dict[str, List[str]]], optional
1573
1595
A declaration of which security mechanisms are applied globally across the API.
1596
+ openapi_extensions: Dict[str, Any], optional
1597
+ Additional OpenAPI extensions as a dictionary.
1574
1598
1575
1599
Returns
1576
1600
-------
@@ -1603,11 +1627,15 @@ def get_openapi_schema(
1603
1627
1604
1628
info .update ({field : value for field , value in optional_fields .items () if value })
1605
1629
1630
+ if not isinstance (openapi_extensions , Dict ):
1631
+ openapi_extensions = {}
1632
+
1606
1633
output : Dict [str , Any ] = {
1607
1634
"openapi" : openapi_version ,
1608
1635
"info" : info ,
1609
1636
"servers" : self ._get_openapi_servers (servers ),
1610
1637
"security" : self ._get_openapi_security (security , security_schemes ),
1638
+ ** openapi_extensions ,
1611
1639
}
1612
1640
1613
1641
components : Dict [str , Dict [str , Any ]] = {}
@@ -1726,6 +1754,7 @@ def get_openapi_json_schema(
1726
1754
license_info : Optional ["License" ] = None ,
1727
1755
security_schemes : Optional [Dict [str , "SecurityScheme" ]] = None ,
1728
1756
security : Optional [List [Dict [str , List [str ]]]] = None ,
1757
+ openapi_extensions : Optional [Dict [str , Any ]] = None ,
1729
1758
) -> str :
1730
1759
"""
1731
1760
Returns the OpenAPI schema as a JSON serializable dict
@@ -1756,6 +1785,8 @@ def get_openapi_json_schema(
1756
1785
A declaration of the security schemes available to be used in the specification.
1757
1786
security: List[Dict[str, List[str]]], optional
1758
1787
A declaration of which security mechanisms are applied globally across the API.
1788
+ openapi_extensions: Dict[str, Any], optional
1789
+ Additional OpenAPI extensions as a dictionary.
1759
1790
1760
1791
Returns
1761
1792
-------
@@ -1778,6 +1809,7 @@ def get_openapi_json_schema(
1778
1809
license_info = license_info ,
1779
1810
security_schemes = security_schemes ,
1780
1811
security = security ,
1812
+ openapi_extensions = openapi_extensions ,
1781
1813
),
1782
1814
by_alias = True ,
1783
1815
exclude_none = True ,
@@ -1805,6 +1837,7 @@ def enable_swagger(
1805
1837
security : Optional [List [Dict [str , List [str ]]]] = None ,
1806
1838
oauth2_config : Optional ["OAuth2Config" ] = None ,
1807
1839
persist_authorization : bool = False ,
1840
+ openapi_extensions : Optional [Dict [str , Any ]] = None ,
1808
1841
):
1809
1842
"""
1810
1843
Returns the OpenAPI schema as a JSON serializable dict
@@ -1847,6 +1880,8 @@ def enable_swagger(
1847
1880
The OAuth2 configuration for the Swagger UI.
1848
1881
persist_authorization: bool, optional
1849
1882
Whether to persist authorization data on browser close/refresh.
1883
+ openapi_extensions: Dict[str, Any], optional
1884
+ Additional OpenAPI extensions as a dictionary.
1850
1885
"""
1851
1886
from aws_lambda_powertools .event_handler .openapi .compat import model_json
1852
1887
from aws_lambda_powertools .event_handler .openapi .models import Server
@@ -1896,6 +1931,7 @@ def swagger_handler():
1896
1931
license_info = license_info ,
1897
1932
security_schemes = security_schemes ,
1898
1933
security = security ,
1934
+ openapi_extensions = openapi_extensions ,
1899
1935
)
1900
1936
1901
1937
# The .replace('</', '<\\/') part is necessary to prevent a potential issue where the JSON string contains
@@ -1949,6 +1985,7 @@ def route(
1949
1985
operation_id : Optional [str ] = None ,
1950
1986
include_in_schema : bool = True ,
1951
1987
security : Optional [List [Dict [str , List [str ]]]] = None ,
1988
+ openapi_extensions : Optional [Dict [str , Any ]] = None ,
1952
1989
middlewares : Optional [List [Callable [..., Any ]]] = None ,
1953
1990
):
1954
1991
"""Route decorator includes parameter `method`"""
@@ -1976,6 +2013,7 @@ def register_resolver(func: Callable):
1976
2013
operation_id ,
1977
2014
include_in_schema ,
1978
2015
security ,
2016
+ openapi_extensions ,
1979
2017
middlewares ,
1980
2018
)
1981
2019
@@ -2489,6 +2527,7 @@ def route(
2489
2527
operation_id : Optional [str ] = None ,
2490
2528
include_in_schema : bool = True ,
2491
2529
security : Optional [List [Dict [str , List [str ]]]] = None ,
2530
+ openapi_extensions : Optional [Dict [str , Any ]] = None ,
2492
2531
middlewares : Optional [List [Callable [..., Any ]]] = None ,
2493
2532
):
2494
2533
def register_route (func : Callable ):
@@ -2497,6 +2536,7 @@ def register_route(func: Callable):
2497
2536
frozen_responses = _FrozenDict (responses ) if responses else None
2498
2537
frozen_tags = frozenset (tags ) if tags else None
2499
2538
frozen_security = _FrozenListDict (security ) if security else None
2539
+ fronzen_openapi_extensions = _FrozenDict (openapi_extensions ) if openapi_extensions else None
2500
2540
2501
2541
route_key = (
2502
2542
rule ,
@@ -2512,6 +2552,7 @@ def register_route(func: Callable):
2512
2552
operation_id ,
2513
2553
include_in_schema ,
2514
2554
frozen_security ,
2555
+ fronzen_openapi_extensions ,
2515
2556
)
2516
2557
2517
2558
# Collate Middleware for routes
@@ -2592,6 +2633,7 @@ def route(
2592
2633
operation_id : Optional [str ] = None ,
2593
2634
include_in_schema : bool = True ,
2594
2635
security : Optional [List [Dict [str , List [str ]]]] = None ,
2636
+ openapi_extensions : Optional [Dict [str , Any ]] = None ,
2595
2637
middlewares : Optional [List [Callable [..., Any ]]] = None ,
2596
2638
):
2597
2639
# NOTE: see #1552 for more context.
@@ -2609,6 +2651,7 @@ def route(
2609
2651
operation_id ,
2610
2652
include_in_schema ,
2611
2653
security ,
2654
+ openapi_extensions ,
2612
2655
middlewares ,
2613
2656
)
2614
2657
0 commit comments