4
4
from pydantic import AnyUrl , BaseModel , Field
5
5
6
6
from aws_lambda_powertools .event_handler .openapi .compat import model_rebuild , parser_openapi_extension
7
+ from aws_lambda_powertools .event_handler .openapi .exceptions import SchemaValidationError
7
8
from aws_lambda_powertools .event_handler .openapi .pydantic_loader import PYDANTIC_V2
8
9
from aws_lambda_powertools .shared .types import Annotated , Literal
9
10
@@ -25,6 +26,7 @@ class OpenAPIExtensions(BaseModel):
25
26
26
27
# This rule is valid for Pydantic v1 and v2
27
28
# If the 'openapi_extensions' field is present in the 'values' dictionary,
29
+ # And if the extension starts with x-
28
30
# update the 'values' dictionary with the contents of 'openapi_extensions',
29
31
# and then remove the 'openapi_extensions' field from the 'values' dictionary
30
32
@@ -34,8 +36,15 @@ class OpenAPIExtensions(BaseModel):
34
36
35
37
@parser_openapi_extension (mode = "before" )
36
38
def serialize_openapi_extension_v2 (self ):
37
- if isinstance (self , dict ) and self .get ("openapi_extensions" ):
38
- self .update (self .get ("openapi_extensions" ))
39
+ openapi_extension_value = self .get ("openapi_extensions" )
40
+
41
+ if isinstance (self , dict ) and openapi_extension_value :
42
+
43
+ for extension_key in openapi_extension_value :
44
+ if not str (extension_key ).startswith ("x-" ):
45
+ raise SchemaValidationError ("An OpenAPI extension key must start with x-" )
46
+
47
+ self .update (openapi_extension_value )
39
48
self .pop ("openapi_extensions" , None )
40
49
41
50
return self
@@ -44,9 +53,17 @@ def serialize_openapi_extension_v2(self):
44
53
45
54
@parser_openapi_extension (pre = False , allow_reuse = True )
46
55
def serialize_openapi_extension_v1 (cls , values ):
47
- if values .get ("openapi_extensions" ):
56
+ openapi_extension_value = values .get ("openapi_extensions" )
57
+
58
+ if openapi_extension_value :
59
+
60
+ for extension_key in openapi_extension_value :
61
+ if not str (extension_key ).startswith ("x-" ):
62
+ raise SchemaValidationError ("An OpenAPI extension key must start with x-" )
63
+
48
64
values .update (values ["openapi_extensions" ])
49
65
del values ["openapi_extensions" ]
66
+
50
67
return values
51
68
52
69
class Config :
0 commit comments