@@ -38,7 +38,6 @@ class Schema(object):
38
38
"""Represents an OpenAPI Schema."""
39
39
40
40
DEFAULT_CAST_CALLABLE_GETTER = {
41
- SchemaType .INTEGER : int ,
42
41
SchemaType .NUMBER : float ,
43
42
SchemaType .BOOLEAN : forcebool ,
44
43
}
@@ -148,27 +147,29 @@ def get_all_required_properties_names(self):
148
147
149
148
return set (required )
150
149
151
- def get_cast_mapping (self , custom_formatters = None ):
150
+ def get_cast_mapping (self , custom_formatters = None , strict = True ):
152
151
pass_defaults = lambda f : functools .partial (
153
- f , custom_formatters = custom_formatters )
152
+ f , custom_formatters = custom_formatters , strict = strict )
154
153
mapping = self .DEFAULT_CAST_CALLABLE_GETTER .copy ()
155
154
mapping .update ({
156
155
SchemaType .STRING : pass_defaults (self ._unmarshal_string ),
156
+ SchemaType .INTEGER : pass_defaults (self ._unmarshal_integer ),
157
157
SchemaType .ANY : pass_defaults (self ._unmarshal_any ),
158
158
SchemaType .ARRAY : pass_defaults (self ._unmarshal_collection ),
159
159
SchemaType .OBJECT : pass_defaults (self ._unmarshal_object ),
160
160
})
161
161
162
162
return defaultdict (lambda : lambda x : x , mapping )
163
163
164
- def cast (self , value , custom_formatters = None ):
164
+ def cast (self , value , custom_formatters = None , strict = True ):
165
165
"""Cast value to schema type"""
166
166
if value is None :
167
167
if not self .nullable :
168
168
raise InvalidSchemaValue ("Null value for non-nullable schema" , value , self .type )
169
169
return self .default
170
170
171
- cast_mapping = self .get_cast_mapping (custom_formatters = custom_formatters )
171
+ cast_mapping = self .get_cast_mapping (
172
+ custom_formatters = custom_formatters , strict = strict )
172
173
173
174
if self .type is not SchemaType .STRING and value == '' :
174
175
return None
@@ -180,12 +181,12 @@ def cast(self, value, custom_formatters=None):
180
181
raise InvalidSchemaValue (
181
182
"Failed to cast value {value} to type {type}" , value , self .type )
182
183
183
- def unmarshal (self , value , custom_formatters = None ):
184
+ def unmarshal (self , value , custom_formatters = None , strict = True ):
184
185
"""Unmarshal parameter from the value."""
185
186
if self .deprecated :
186
187
warnings .warn ("The schema is deprecated" , DeprecationWarning )
187
188
188
- casted = self .cast (value , custom_formatters = custom_formatters )
189
+ casted = self .cast (value , custom_formatters = custom_formatters , strict = strict )
189
190
190
191
if casted is None and not self .required :
191
192
return None
@@ -196,7 +197,10 @@ def unmarshal(self, value, custom_formatters=None):
196
197
197
198
return casted
198
199
199
- def _unmarshal_string (self , value , custom_formatters = None ):
200
+ def _unmarshal_string (self , value , custom_formatters = None , strict = True ):
201
+ if strict and not isinstance (value , (text_type , binary_type )):
202
+ raise InvalidSchemaValue ("Value {value} is not of type {type}" , value , self .type )
203
+
200
204
try :
201
205
schema_format = SchemaFormat (self .format )
202
206
except ValueError :
@@ -216,7 +220,13 @@ def _unmarshal_string(self, value, custom_formatters=None):
216
220
raise InvalidCustomFormatSchemaValue (
217
221
"Failed to format value {value} to format {type}: {exception}" , value , self .format , exc )
218
222
219
- def _unmarshal_any (self , value , custom_formatters = None ):
223
+ def _unmarshal_integer (self , value , custom_formatters = None , strict = True ):
224
+ if strict and not isinstance (value , (integer_types , )):
225
+ raise InvalidSchemaValue ("Value {value} is not of type {type}" , value , self .type )
226
+
227
+ return int (value )
228
+
229
+ def _unmarshal_any (self , value , custom_formatters = None , strict = True ):
220
230
types_resolve_order = [
221
231
SchemaType .OBJECT , SchemaType .ARRAY , SchemaType .BOOLEAN ,
222
232
SchemaType .INTEGER , SchemaType .NUMBER , SchemaType .STRING ,
@@ -232,16 +242,18 @@ def _unmarshal_any(self, value, custom_formatters=None):
232
242
233
243
raise NoValidSchema (value )
234
244
235
- def _unmarshal_collection (self , value , custom_formatters = None ):
245
+ def _unmarshal_collection (self , value , custom_formatters = None , strict = True ):
236
246
if self .items is None :
237
247
raise UndefinedItemsSchema (self .type )
238
248
239
- f = functools .partial (self .items .unmarshal ,
240
- custom_formatters = custom_formatters )
249
+ f = functools .partial (
250
+ self .items .unmarshal ,
251
+ custom_formatters = custom_formatters , strict = strict ,
252
+ )
241
253
return list (map (f , value ))
242
254
243
255
def _unmarshal_object (self , value , model_factory = None ,
244
- custom_formatters = None ):
256
+ custom_formatters = None , strict = True ):
245
257
if not isinstance (value , (dict , )):
246
258
raise InvalidSchemaValue ("Value {value} is not of type {type}" , value , self .type )
247
259
@@ -270,7 +282,7 @@ def _unmarshal_object(self, value, model_factory=None,
270
282
return model_factory .create (properties , name = self .model )
271
283
272
284
def _unmarshal_properties (self , value , one_of_schema = None ,
273
- custom_formatters = None ):
285
+ custom_formatters = None , strict = True ):
274
286
all_props = self .get_all_properties ()
275
287
all_props_names = self .get_all_properties_names ()
276
288
all_req_props_names = self .get_all_required_properties_names ()
0 commit comments