1515from openapi_core .unmarshalling .schemas .datatypes import (
1616 FormatUnmarshallersDict ,
1717)
18- from openapi_core .unmarshalling .schemas .exceptions import FormatUnmarshalError
19- from openapi_core .unmarshalling .schemas .exceptions import UnmarshallerError
2018from openapi_core .validation .schemas .validators import SchemaValidator
2119
2220log = logging .getLogger (__name__ )
@@ -138,34 +136,15 @@ def _unmarshal_properties(
138136
139137class MultiTypeUnmarshaller (PrimitiveUnmarshaller ):
140138 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+ )
142143 return unmarshaller (value )
143144
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-
164145
165146class AnyUnmarshaller (MultiTypeUnmarshaller ):
166- @property
167- def type (self ) -> List [str ]:
168- return self .schema_unmarshaller .types_unmarshaller .get_types ()
147+ pass
169148
170149
171150class TypesUnmarshaller :
@@ -185,7 +164,7 @@ def __init__(
185164 def get_types (self ) -> List [str ]:
186165 return list (self .unmarshallers .keys ())
187166
188- def get_unmarshaller (
167+ def get_unmarshaller_cls (
189168 self ,
190169 schema_type : Optional [Union [Iterable [str ], str ]],
191170 ) -> Type ["PrimitiveUnmarshaller" ]:
@@ -220,8 +199,8 @@ def unmarshal(self, schema_format: str, value: Any) -> Any:
220199 return value
221200 try :
222201 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
225204
226205 def get_unmarshaller (
227206 self , schema_format : str
@@ -279,19 +258,32 @@ def unmarshal(self, value: Any) -> Any:
279258 (isinstance (value , bytes ) and schema_format in ["binary" , "byte" ])
280259 ):
281260 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
283269
284270 def get_type_unmarshaller (
285271 self ,
286272 schema_type : Optional [Union [Iterable [str ], str ]],
287273 ) -> PrimitiveUnmarshaller :
288- klass = self .types_unmarshaller .get_unmarshaller (schema_type )
274+ klass = self .types_unmarshaller .get_unmarshaller_cls (schema_type )
289275 return klass (
290276 self .schema ,
291277 self .schema_validator ,
292278 self ,
293279 )
294280
281+ def get_format_unmarshaller (
282+ self ,
283+ schema_format : str ,
284+ ) -> Optional [FormatUnmarshaller ]:
285+ return self .formats_unmarshaller .get_unmarshaller (schema_format )
286+
295287 def evolve (self , schema : SchemaPath ) -> "SchemaUnmarshaller" :
296288 cls = self .__class__
297289
@@ -304,6 +296,10 @@ def evolve(self, schema: SchemaPath) -> "SchemaUnmarshaller":
304296
305297 def find_format (self , value : Any ) -> Optional [str ]:
306298 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
307303 if "format" in schema :
308304 return str (schema .getkey ("format" ))
309305 return None
0 commit comments