22from six import iteritems
33
44from openapi_core .exceptions import (
5- OpenAPIMappingError , MissingParameterError , MissingBodyError ,
5+ OpenAPIMappingError , MissingParameter , MissingBody ,
66)
77
88
@@ -45,13 +45,6 @@ def __init__(self, errors, body=None, parameters=None):
4545
4646class RequestValidator (object ):
4747
48- SPEC_LOCATION_TO_REQUEST_LOCATION = {
49- 'path' : 'view_args' ,
50- 'query' : 'args' ,
51- 'headers' : 'headers' ,
52- 'cookies' : 'cookies' ,
53- }
54-
5548 def __init__ (self , spec ):
5649 self .spec = spec
5750
@@ -69,10 +62,10 @@ def validate(self, request):
6962
7063 operation_pattern = request .full_url_pattern .replace (
7164 server .default_url , '' )
72- method = request .method .lower ()
7365
7466 try :
75- operation = self .spec .get_operation (operation_pattern , method )
67+ operation = self .spec .get_operation (
68+ operation_pattern , request .method )
7669 # don't process if operation errors
7770 except OpenAPIMappingError as exc :
7871 errors .append (exc )
@@ -81,17 +74,20 @@ def validate(self, request):
8174 for param_name , param in iteritems (operation .parameters ):
8275 try :
8376 raw_value = self ._get_raw_value (request , param )
84- except MissingParameterError as exc :
77+ except MissingParameter as exc :
8578 if param .required :
8679 errors .append (exc )
8780
8881 if not param .schema or param .schema .default is None :
8982 continue
9083 raw_value = param .schema .default
9184
92- value = param .unmarshal (raw_value )
93-
94- parameters [param .location ][param_name ] = value
85+ try :
86+ value = param .unmarshal (raw_value )
87+ except OpenAPIMappingError as exc :
88+ errors .append (exc )
89+ else :
90+ parameters [param .location ][param_name ] = value
9591
9692 if operation .request_body is not None :
9793 try :
@@ -101,29 +97,26 @@ def validate(self, request):
10197 else :
10298 try :
10399 raw_body = self ._get_raw_body (request )
104- except MissingBodyError as exc :
100+ except MissingBody as exc :
105101 if operation .request_body .required :
106102 errors .append (exc )
107103 else :
108- body = media_type .unmarshal (raw_body )
104+ try :
105+ body = media_type .unmarshal (raw_body )
106+ except OpenAPIMappingError as exc :
107+ errors .append (exc )
109108
110109 return RequestValidationResult (errors , body , parameters )
111110
112- def _get_request_location (self , spec_location ):
113- return self .SPEC_LOCATION_TO_REQUEST_LOCATION [spec_location ]
114-
115111 def _get_raw_value (self , request , param ):
116- request_location = self ._get_request_location (param .location )
117- request_attr = getattr (request , request_location )
118-
119112 try :
120- return request_attr [param .name ]
113+ return request . parameters [ param . location ] [param .name ]
121114 except KeyError :
122- raise MissingParameterError (
115+ raise MissingParameter (
123116 "Missing required `{0}` parameter" .format (param .name ))
124117
125118 def _get_raw_body (self , request ):
126- if not request .data :
127- raise MissingBodyError ("Missing required request body" )
119+ if not request .body :
120+ raise MissingBody ("Missing required request body" )
128121
129- return request .data
122+ return request .body
0 commit comments