@@ -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
"""
@@ -383,6 +384,7 @@ def __init__(
383
384
self .tags = tags or []
384
385
self .include_in_schema = include_in_schema
385
386
self .security = security
387
+ self .openapi_extensions = openapi_extensions
386
388
self .middlewares = middlewares or []
387
389
self .operation_id = operation_id or self ._generate_operation_id ()
388
390
@@ -534,6 +536,10 @@ def _get_openapi_path(
534
536
if self .security :
535
537
operation ["security" ] = self .security
536
538
539
+ # Add OpenAPI extensions if present
540
+ if self .openapi_extensions :
541
+ operation .update (self .openapi_extensions )
542
+
537
543
# Add the parameters to the OpenAPI operation
538
544
if parameters :
539
545
all_parameters = {(param ["in" ], param ["name" ]): param for param in parameters }
@@ -939,6 +945,7 @@ def route(
939
945
operation_id : Optional [str ] = None ,
940
946
include_in_schema : bool = True ,
941
947
security : Optional [List [Dict [str , List [str ]]]] = None ,
948
+ openapi_extensions : Optional [Dict [str , Any ]] = None ,
942
949
middlewares : Optional [List [Callable [..., Any ]]] = None ,
943
950
):
944
951
raise NotImplementedError ()
@@ -998,6 +1005,7 @@ def get(
998
1005
operation_id : Optional [str ] = None ,
999
1006
include_in_schema : bool = True ,
1000
1007
security : Optional [List [Dict [str , List [str ]]]] = None ,
1008
+ openapi_extensions : Optional [Dict [str , Any ]] = None ,
1001
1009
middlewares : Optional [List [Callable [..., Any ]]] = None ,
1002
1010
):
1003
1011
"""Get route decorator with GET `method`
@@ -1036,6 +1044,7 @@ def lambda_handler(event, context):
1036
1044
operation_id ,
1037
1045
include_in_schema ,
1038
1046
security ,
1047
+ openapi_extensions ,
1039
1048
middlewares ,
1040
1049
)
1041
1050
@@ -1053,6 +1062,7 @@ def post(
1053
1062
operation_id : Optional [str ] = None ,
1054
1063
include_in_schema : bool = True ,
1055
1064
security : Optional [List [Dict [str , List [str ]]]] = None ,
1065
+ openapi_extensions : Optional [Dict [str , Any ]] = None ,
1056
1066
middlewares : Optional [List [Callable [..., Any ]]] = None ,
1057
1067
):
1058
1068
"""Post route decorator with POST `method`
@@ -1092,6 +1102,7 @@ def lambda_handler(event, context):
1092
1102
operation_id ,
1093
1103
include_in_schema ,
1094
1104
security ,
1105
+ openapi_extensions ,
1095
1106
middlewares ,
1096
1107
)
1097
1108
@@ -1109,6 +1120,7 @@ def put(
1109
1120
operation_id : Optional [str ] = None ,
1110
1121
include_in_schema : bool = True ,
1111
1122
security : Optional [List [Dict [str , List [str ]]]] = None ,
1123
+ openapi_extensions : Optional [Dict [str , Any ]] = None ,
1112
1124
middlewares : Optional [List [Callable [..., Any ]]] = None ,
1113
1125
):
1114
1126
"""Put route decorator with PUT `method`
@@ -1148,6 +1160,7 @@ def lambda_handler(event, context):
1148
1160
operation_id ,
1149
1161
include_in_schema ,
1150
1162
security ,
1163
+ openapi_extensions ,
1151
1164
middlewares ,
1152
1165
)
1153
1166
@@ -1165,6 +1178,7 @@ def delete(
1165
1178
operation_id : Optional [str ] = None ,
1166
1179
include_in_schema : bool = True ,
1167
1180
security : Optional [List [Dict [str , List [str ]]]] = None ,
1181
+ openapi_extensions : Optional [Dict [str , Any ]] = None ,
1168
1182
middlewares : Optional [List [Callable [..., Any ]]] = None ,
1169
1183
):
1170
1184
"""Delete route decorator with DELETE `method`
@@ -1203,6 +1217,7 @@ def lambda_handler(event, context):
1203
1217
operation_id ,
1204
1218
include_in_schema ,
1205
1219
security ,
1220
+ openapi_extensions ,
1206
1221
middlewares ,
1207
1222
)
1208
1223
@@ -1220,6 +1235,7 @@ def patch(
1220
1235
operation_id : Optional [str ] = None ,
1221
1236
include_in_schema : bool = True ,
1222
1237
security : Optional [List [Dict [str , List [str ]]]] = None ,
1238
+ openapi_extensions : Optional [Dict [str , Any ]] = None ,
1223
1239
middlewares : Optional [List [Callable ]] = None ,
1224
1240
):
1225
1241
"""Patch route decorator with PATCH `method`
@@ -1261,6 +1277,7 @@ def lambda_handler(event, context):
1261
1277
operation_id ,
1262
1278
include_in_schema ,
1263
1279
security ,
1280
+ openapi_extensions ,
1264
1281
middlewares ,
1265
1282
)
1266
1283
@@ -1278,6 +1295,7 @@ def head(
1278
1295
operation_id : Optional [str ] = None ,
1279
1296
include_in_schema : bool = True ,
1280
1297
security : Optional [List [Dict [str , List [str ]]]] = None ,
1298
+ openapi_extensions : Optional [Dict [str , Any ]] = None ,
1281
1299
middlewares : Optional [List [Callable ]] = None ,
1282
1300
):
1283
1301
"""Head route decorator with HEAD `method`
@@ -1318,6 +1336,7 @@ def lambda_handler(event, context):
1318
1336
operation_id ,
1319
1337
include_in_schema ,
1320
1338
security ,
1339
+ openapi_extensions ,
1321
1340
middlewares ,
1322
1341
)
1323
1342
@@ -1541,6 +1560,7 @@ def get_openapi_schema(
1541
1560
license_info : Optional ["License" ] = None ,
1542
1561
security_schemes : Optional [Dict [str , "SecurityScheme" ]] = None ,
1543
1562
security : Optional [List [Dict [str , List [str ]]]] = None ,
1563
+ openapi_extensions : Optional [Dict [str , Any ]] = None ,
1544
1564
) -> "OpenAPI" :
1545
1565
"""
1546
1566
Returns the OpenAPI schema as a pydantic model.
@@ -1603,11 +1623,15 @@ def get_openapi_schema(
1603
1623
1604
1624
info .update ({field : value for field , value in optional_fields .items () if value })
1605
1625
1626
+ if not openapi_extensions :
1627
+ openapi_extensions = {}
1628
+
1606
1629
output : Dict [str , Any ] = {
1607
1630
"openapi" : openapi_version ,
1608
1631
"info" : info ,
1609
1632
"servers" : self ._get_openapi_servers (servers ),
1610
1633
"security" : self ._get_openapi_security (security , security_schemes ),
1634
+ ** openapi_extensions ,
1611
1635
}
1612
1636
1613
1637
components : Dict [str , Dict [str , Any ]] = {}
@@ -1726,6 +1750,7 @@ def get_openapi_json_schema(
1726
1750
license_info : Optional ["License" ] = None ,
1727
1751
security_schemes : Optional [Dict [str , "SecurityScheme" ]] = None ,
1728
1752
security : Optional [List [Dict [str , List [str ]]]] = None ,
1753
+ openapi_extensions : Optional [Dict [str , Any ]] = None ,
1729
1754
) -> str :
1730
1755
"""
1731
1756
Returns the OpenAPI schema as a JSON serializable dict
@@ -1778,6 +1803,7 @@ def get_openapi_json_schema(
1778
1803
license_info = license_info ,
1779
1804
security_schemes = security_schemes ,
1780
1805
security = security ,
1806
+ openapi_extensions = openapi_extensions ,
1781
1807
),
1782
1808
by_alias = True ,
1783
1809
exclude_none = True ,
@@ -1805,6 +1831,7 @@ def enable_swagger(
1805
1831
security : Optional [List [Dict [str , List [str ]]]] = None ,
1806
1832
oauth2_config : Optional ["OAuth2Config" ] = None ,
1807
1833
persist_authorization : bool = False ,
1834
+ openapi_extensions : Optional [Dict [str , Any ]] = None ,
1808
1835
):
1809
1836
"""
1810
1837
Returns the OpenAPI schema as a JSON serializable dict
@@ -1896,6 +1923,7 @@ def swagger_handler():
1896
1923
license_info = license_info ,
1897
1924
security_schemes = security_schemes ,
1898
1925
security = security ,
1926
+ openapi_extensions = openapi_extensions ,
1899
1927
)
1900
1928
1901
1929
# The .replace('</', '<\\/') part is necessary to prevent a potential issue where the JSON string contains
@@ -1949,6 +1977,7 @@ def route(
1949
1977
operation_id : Optional [str ] = None ,
1950
1978
include_in_schema : bool = True ,
1951
1979
security : Optional [List [Dict [str , List [str ]]]] = None ,
1980
+ openapi_extensions : Optional [Dict [str , Any ]] = None ,
1952
1981
middlewares : Optional [List [Callable [..., Any ]]] = None ,
1953
1982
):
1954
1983
"""Route decorator includes parameter `method`"""
@@ -1976,6 +2005,7 @@ def register_resolver(func: Callable):
1976
2005
operation_id ,
1977
2006
include_in_schema ,
1978
2007
security ,
2008
+ openapi_extensions ,
1979
2009
middlewares ,
1980
2010
)
1981
2011
@@ -2489,6 +2519,7 @@ def route(
2489
2519
operation_id : Optional [str ] = None ,
2490
2520
include_in_schema : bool = True ,
2491
2521
security : Optional [List [Dict [str , List [str ]]]] = None ,
2522
+ openapi_extensions : Optional [Dict [str , Any ]] = None ,
2492
2523
middlewares : Optional [List [Callable [..., Any ]]] = None ,
2493
2524
):
2494
2525
def register_route (func : Callable ):
@@ -2497,6 +2528,7 @@ def register_route(func: Callable):
2497
2528
frozen_responses = _FrozenDict (responses ) if responses else None
2498
2529
frozen_tags = frozenset (tags ) if tags else None
2499
2530
frozen_security = _FrozenListDict (security ) if security else None
2531
+ fronzen_openapi_extensions = _FrozenDict (openapi_extensions ) if openapi_extensions else None
2500
2532
2501
2533
route_key = (
2502
2534
rule ,
@@ -2512,6 +2544,7 @@ def register_route(func: Callable):
2512
2544
operation_id ,
2513
2545
include_in_schema ,
2514
2546
frozen_security ,
2547
+ fronzen_openapi_extensions ,
2515
2548
)
2516
2549
2517
2550
# Collate Middleware for routes
@@ -2592,6 +2625,7 @@ def route(
2592
2625
operation_id : Optional [str ] = None ,
2593
2626
include_in_schema : bool = True ,
2594
2627
security : Optional [List [Dict [str , List [str ]]]] = None ,
2628
+ openapi_extensions : Optional [Dict [str , Any ]] = None ,
2595
2629
middlewares : Optional [List [Callable [..., Any ]]] = None ,
2596
2630
):
2597
2631
# NOTE: see #1552 for more context.
@@ -2609,6 +2643,7 @@ def route(
2609
2643
operation_id ,
2610
2644
include_in_schema ,
2611
2645
security ,
2646
+ openapi_extensions ,
2612
2647
middlewares ,
2613
2648
)
2614
2649
0 commit comments