1+ from typing import TYPE_CHECKING
12from typing import Any
23from typing import Mapping
34from typing import Optional
2223from openapi_core .schema .protocols import SuportsGetAll
2324from openapi_core .schema .protocols import SuportsGetList
2425from openapi_core .schema .schemas import get_properties
26+ from openapi_core .validation .schemas .validators import SchemaValidator
27+
28+ if TYPE_CHECKING :
29+ from openapi_core .casting .schemas .casters import SchemaCaster
2530
2631
2732class MediaTypesDeserializer :
@@ -65,13 +70,17 @@ def __init__(
6570 media_types_deserializer : MediaTypesDeserializer ,
6671 mimetype : str ,
6772 schema : Optional [SchemaPath ] = None ,
73+ schema_validator : Optional [SchemaValidator ] = None ,
74+ schema_caster : Optional ["SchemaCaster" ] = None ,
6875 encoding : Optional [SchemaPath ] = None ,
6976 ** parameters : str ,
7077 ):
7178 self .style_deserializers_factory = style_deserializers_factory
7279 self .media_types_deserializer = media_types_deserializer
7380 self .mimetype = mimetype
7481 self .schema = schema
82+ self .schema_validator = schema_validator
83+ self .schema_caster = schema_caster
7584 self .encoding = encoding
7685 self .parameters = parameters
7786
@@ -86,25 +95,68 @@ def deserialize(self, value: bytes) -> Any:
8695 ):
8796 return deserialized
8897
89- # decode multipart request bodies
90- return self .decode (deserialized )
98+ # decode multipart request bodies if schema provided
99+ if self .schema is not None :
100+ return self .decode (deserialized )
101+
102+ return deserialized
91103
92104 def evolve (
93- self , mimetype : str , schema : Optional [SchemaPath ]
105+ self ,
106+ schema : SchemaPath ,
107+ mimetype : Optional [str ] = None ,
94108 ) -> "MediaTypeDeserializer" :
95109 cls = self .__class__
110+
111+ schema_validator = None
112+ if self .schema_validator is not None :
113+ schema_validator = self .schema_validator .evolve (schema )
114+
115+ schema_caster = None
116+ if self .schema_caster is not None :
117+ schema_caster = self .schema_caster .evolve (schema )
96118
97119 return cls (
98120 self .style_deserializers_factory ,
99121 self .media_types_deserializer ,
100- mimetype ,
122+ mimetype = mimetype or self . mimetype ,
101123 schema = schema ,
124+ schema_validator = schema_validator ,
125+ schema_caster = schema_caster ,
102126 )
103127
104- def decode (self , location : Mapping [str , Any ]) -> Mapping [str , Any ]:
128+ def decode (self , location : Mapping [str , Any ], schema_only : bool = False ) -> Mapping [str , Any ]:
105129 # schema is required for multipart
106130 assert self .schema is not None
107131 properties = {}
132+
133+ # For urlencoded/multipart, use caster for oneOf/anyOf detection if validator available
134+ if self .schema_validator is not None :
135+ one_of_schema = self .schema_validator .get_one_of_schema (
136+ location , caster = self .schema_caster
137+ )
138+ if one_of_schema is not None :
139+ one_of_properties = self .evolve (
140+ one_of_schema
141+ ).decode (location , schema_only = True )
142+ properties .update (one_of_properties )
143+
144+ any_of_schemas = self .schema_validator .iter_any_of_schemas (
145+ location , caster = self .schema_caster
146+ )
147+ for any_of_schema in any_of_schemas :
148+ any_of_properties = self .evolve (
149+ any_of_schema
150+ ).decode (location , schema_only = True )
151+ properties .update (any_of_properties )
152+
153+ all_of_schemas = self .schema_validator .iter_all_of_schemas (location )
154+ for all_of_schema in all_of_schemas :
155+ all_of_properties = self .evolve (
156+ all_of_schema
157+ ).decode (location , schema_only = True )
158+ properties .update (all_of_properties )
159+
108160 for prop_name , prop_schema in get_properties (self .schema ).items ():
109161 try :
110162 properties [prop_name ] = self .decode_property (
@@ -115,6 +167,9 @@ def decode(self, location: Mapping[str, Any]) -> Mapping[str, Any]:
115167 continue
116168 properties [prop_name ] = prop_schema ["default" ]
117169
170+ if schema_only :
171+ return properties
172+
118173 return properties
119174
120175 def decode_property (
@@ -175,8 +230,8 @@ def decode_property_content_type(
175230 ) -> Any :
176231 prop_content_type = get_content_type (prop_schema , prop_encoding )
177232 prop_deserializer = self .evolve (
178- prop_content_type ,
179233 prop_schema ,
234+ mimetype = prop_content_type ,
180235 )
181236 prop_schema_type = prop_schema .getkey ("type" , "" )
182237 if (
0 commit comments