27
27
oas_path = os .path .join (os .path .dirname (__file__ ), ".." , ".." , "openapi.yaml" )
28
28
29
29
30
- class OpenAPIRequestValidator (Handler ):
30
+ class OpenAPIValidator (Handler ):
31
+ openapi : "OpenAPI"
32
+
33
+ def __init__ (self ) -> None :
34
+ path = Path (oas_path )
35
+ assert path .exists ()
36
+ self .openapi = OpenAPI .from_path (path )
37
+
38
+
39
+ class OpenAPIRequestValidator (OpenAPIValidator ):
31
40
"""
32
41
Validates the requests to the LocalStack public endpoints (the ones with a _localstack or _aws prefix) against
33
42
a OpenAPI specification.
34
43
"""
35
44
36
- def __init__ (self ):
37
- self .openapi = OpenAPI .from_path (Path (oas_path ))
38
-
39
45
def __call__ (self , chain : HandlerChain , context : RequestContext , response : Response ):
40
46
if not config .OPENAPI_VALIDATE_REQUEST :
41
47
return
@@ -50,25 +56,24 @@ def __call__(self, chain: HandlerChain, context: RequestContext, response: Respo
50
56
response .status_code = 400
51
57
response .set_json ({"error" : "Bad Request" , "message" : str (e )})
52
58
chain .stop ()
53
- except OpenAPIError as e :
59
+ except OpenAPIError :
54
60
# Other errors can be raised when validating a request against the OpenAPI specification.
55
61
# The most common are: ServerNotFound, OperationNotFound, or PathNotFound.
56
62
# We explicitly do not check any other error but RequestValidationError ones.
57
- LOG .debug ("OpenAPI validation exception: (%s): %s" , e .__class__ .__name__ , str (e ))
58
-
63
+ # We shallow the exception to avoid excessive logging (e.g., a lot of ServerNotFound), as the only
64
+ # purpose of this handler is to check for request validation errors.
65
+ pass
59
66
<
8000
td data-grid-cell-id="diff-a50e0798bccf43a6ee249f013e8f58d5990d6274b716d3905f9c9479c4d75c31-60-66-0" data-selected="false" role="gridcell" style="background-color:var(--diffBlob-deletionNum-bgColor, var(--diffBlob-deletion-bgColor-num));text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative left-side">60
- class OpenAPIResponseValidator (Handler ):
61
- def __init__ (self ):
62
- self .openapi = OpenAPI .from_path (Path (oas_path ))
63
67
68
+ class OpenAPIResponseValidator (OpenAPIValidator ):
64
69
def __call__ (self , chain : HandlerChain , context : RequestContext , response : Response ):
65
70
# We are more lenient in validating the responses. The use of this flag is intended for test.
66
71
if not config .OPENAPI_VALIDATE_RESPONSE :
67
72
return
68
73
69
74
path = context .request .path
70
75
71
- if path .startswith (INTERNAL_RESOURCE_PATH ) or path .startswith ("/_aws/" ):
76
+ if path .startswith (f" { INTERNAL_RESOURCE_PATH } /" ) or path .startswith ("/_aws/" ):
72
77
try :
73
78
self .openapi .validate_response (
74
79
WerkzeugOpenAPIRequest (context .request ),
0 commit comments