15
15
from openapi_core .unmarshalling .schemas .datatypes import (
16
16
FormatUnmarshallersDict ,
17
17
)
18
- from openapi_core .unmarshalling .schemas .exceptions import FormatUnmarshalError
19
- from openapi_core .unmarshalling .schemas .exceptions import UnmarshallerError
20
18
from openapi_core .validation .schemas .validators import SchemaValidator
21
19
22
20
log = logging .getLogger (__name__ )
@@ -138,34 +136,15 @@ def _unmarshal_properties(
138
136
139
137
class MultiTypeUnmarshaller (PrimitiveUnmarshaller ):
140
138
def __call__ (self , value : Any ) -> Any :
141
- unmarshaller = self ._get_best_unmarshaller (value )
139
+ primitive_type = self .schema_validator .get_primitive_type (value )
140
+ unmarshaller = self .schema_unmarshaller .get_type_unmarshaller (
141
+ primitive_type
142
+ )
142
143
return unmarshaller (value )
143
144
144
- @property
145
- def type (self ) -> List [str ]:
146
- types = self .schema .getkey ("type" , ["any" ])
147
- assert isinstance (types , list )
148
- return types
149
-
150
- def _get_best_unmarshaller (self , value : Any ) -> "PrimitiveUnmarshaller" :
151
- for schema_type in self .type :
152
- result = self .schema_validator .type_validator (
153
- value , type_override = schema_type
154
- )
155
- if not result :
156
- continue
157
- result = self .schema_validator .format_validator (value )
158
- if not result :
159
- continue
160
- return self .schema_unmarshaller .get_type_unmarshaller (schema_type )
161
-
162
- raise UnmarshallerError ("Unmarshaller not found for type(s)" )
163
-
164
145
165
146
class AnyUnmarshaller (MultiTypeUnmarshaller ):
166
- @property
167
- def type (self ) -> List [str ]:
168
- return self .schema_unmarshaller .types_unmarshaller .get_types ()
147
+ pass
169
148
170
149
171
150
class TypesUnmarshaller :
@@ -185,7 +164,7 @@ def __init__(
185
164
def get_types (self ) -> List [str ]:
186
165
return list (self .unmarshallers .keys ())
187
166
188
- def get_unmarshaller (
167
+ def get_unmarshaller_cls (
189
168
self ,
190
169
schema_type : Optional [Union [Iterable [str ], str ]],
191
170
) -> Type ["PrimitiveUnmarshaller" ]:
@@ -220,8 +199,8 @@ def unmarshal(self, schema_format: str, value: Any) -> Any:
220
199
return value
221
200
try :
222
201
return format_unmarshaller (value )
223
- except (ValueError , TypeError ) as exc :
224
- raise FormatUnmarshalError ( value , schema_format , exc )
202
+ except (AttributeError , ValueError , TypeError ):
203
+ return value
225
204
226
205
def get_unmarshaller (
227
206
self , schema_format : str
@@ -279,19 +258,32 @@ def unmarshal(self, value: Any) -> Any:
279
258
(isinstance (value , bytes ) and schema_format in ["binary" , "byte" ])
280
259
):
281
260
return typed
282
- return self .formats_unmarshaller .unmarshal (schema_format , typed )
261
+
262
+ format_unmarshaller = self .get_format_unmarshaller (schema_format )
263
+ if format_unmarshaller is None :
264
+ return typed
265
+ try :
266
+ return format_unmarshaller (typed )
267
+ except (AttributeError , ValueError , TypeError ):
268
+ return typed
283
269
284
270
def get_type_unmarshaller (
285
271
self ,
286
272
schema_type : Optional [Union [Iterable [str ], str ]],
287
273
) -> PrimitiveUnmarshaller :
288
- klass = self .types_unmarshaller .get_unmarshaller (schema_type )
274
+ klass = self .types_unmarshaller .get_unmarshaller_cls (schema_type )
289
275
return klass (
290
276
self .schema ,
291
277
self .schema_validator ,
292
278
self ,
293
279
)
294
280
281
+ def get_format_unmarshaller (
282
+ self ,
283
+ schema_format : str ,
284
+ ) -> Optional [FormatUnmarshaller ]:
285
+ return self .formats_unmarshaller .get_unmarshaller (schema_format )
286
+
295
287
def evolve (self , schema : SchemaPath ) -> "SchemaUnmarshaller" :
296
288
cls = self .__class__
297
289
@@ -304,6 +296,10 @@ def evolve(self, schema: SchemaPath) -> "SchemaUnmarshaller":
304
296
305
297
def find_format (self , value : Any ) -> Optional [str ]:
306
298
for schema in self .schema_validator .iter_valid_schemas (value ):
299
+ schema_validator = self .schema_validator .evolve (schema )
300
+ primitive_type = schema_validator .get_primitive_type (value )
301
+ if primitive_type != "string" :
302
+ continue
307
303
if "format" in schema :
308
304
return str (schema .getkey ("format" ))
309
305
return None
0 commit comments