1414import logging as log
1515import json
1616
17- from openapi_core import (
18- Spec ,
19- unmarshal_request , unmarshal_response ,
20- )
17+ from openapi_core import OpenAPI
2118
2219from openapi_core .exceptions import OpenAPIError
2320from openapi_core .templating .paths .exceptions import PathNotFound , \
5653
5754with open (path .join (path .dirname (__file__ ), "openapi.json" ),
5855 "r" , encoding = "utf-8" ) as openapi :
59- app .openapi_spec = Spec .from_dict (json .load (openapi )) # type: ignore
56+ app .openapi_spec = OpenAPI .from_dict (json .load (openapi )) # type: ignore
6057
6158
6259@app .before_response ()
@@ -86,7 +83,8 @@ def before_each_response(req):
8683 """Checks the API before processing each response."""
8784 req .api = OpenAPIRequest (req )
8885 try :
89- unmarshal_request (req .api , app .openapi_spec )
86+ result = app .openapi_spec .unmarshal_request (req .api )
87+ result .raise_for_errors ()
9088 except (OperationNotFound , PathNotFound ) as error :
9189 log .debug ("%s" , error )
9290 return # not found
@@ -105,10 +103,10 @@ def after_each_response(req, res):
105103 if not hasattr (req , "api" ):
106104 req .api = OpenAPIRequest (req )
107105 try :
108- unmarshal_response (
106+ result = app . openapi_spec . unmarshal_response (
109107 req .api ,
110- OpenAPIResponse (res ),
111- app . openapi_spec )
108+ OpenAPIResponse (res ))
109+ result . raise_for_errors ( )
112110 except (OperationNotFound , PathNotFound ):
113111 return res
114112 except OpenAPIError as error :
@@ -170,6 +168,13 @@ def ajax_uuid(req, arg):
170168 return json .dumps ({"arg" : str (arg )}), "application/json"
171169
172170
171+ @app .route ("/arg/<arg>" )
172+ def ajax_arg_fallback (req , arg ):
173+ """Catch-all for /arg/{arg} - OpenAPI validation handles invalid types."""
174+ assert req
175+ return json .dumps ({"arg" : arg }), "application/json"
176+
177+
173178@app .route ('/internal-server-error' )
174179def method_raises_errror (req ):
175180 """Internal server error test."""
0 commit comments