3
3
from typing import TYPE_CHECKING
4
4
from typing import Any
5
5
from typing import Dict
6
+ from typing import Iterable
6
7
from typing import List
7
8
from typing import Optional
8
9
16
17
from openapi_schema_validator ._format import oas30_format_checker
17
18
from openapi_schema_validator ._types import is_string
18
19
19
- from openapi_core .extensions .models .factories import ModelFactory
20
+ from openapi_core .extensions .models .factories import ModelClassImporter
20
21
from openapi_core .schema .schemas import get_all_properties
21
22
from openapi_core .schema .schemas import get_all_properties_names
22
23
from openapi_core .spec import Spec
@@ -196,8 +197,8 @@ class ObjectUnmarshaller(ComplexUnmarshaller):
196
197
}
197
198
198
199
@property
199
- def model_factory (self ) -> ModelFactory :
200
- return ModelFactory ()
200
+ def object_class_factory (self ) -> ModelClassImporter :
201
+ return ModelClassImporter ()
201
202
202
203
def unmarshal (self , value : Any ) -> Any :
203
204
try :
@@ -230,11 +231,11 @@ def _unmarshal_object(self, value: Any) -> Any:
230
231
else :
231
232
properties = self ._unmarshal_properties (value )
232
233
233
- if "x- model" in self .schema :
234
- name = self . schema [ "x-model" ]
235
- return self .model_factory .create (properties , name = name )
234
+ model = self .schema . getkey ( "x-model" )
235
+ fields : Iterable [ str ] = properties and properties . keys () or [ ]
236
+ object_class = self .object_class_factory .create (fields , model = model )
236
237
237
- return properties
238
+ return object_class ( ** properties )
238
239
239
240
def _unmarshal_properties (
240
241
self , value : Any , one_of_schema : Optional [Spec ] = None
@@ -253,17 +254,18 @@ def _unmarshal_properties(
253
254
additional_properties = self .schema .getkey (
254
255
"additionalProperties" , True
255
256
)
256
- if isinstance (additional_properties , dict ):
257
- additional_prop_schema = self .schema / "additionalProperties"
257
+ if additional_properties is not False :
258
+ # free-form object
259
+ if additional_properties is True :
260
+ additional_prop_schema = Spec .from_dict ({})
261
+ # defined schema
262
+ else :
263
+ additional_prop_schema = self .schema / "additionalProperties"
258
264
for prop_name in extra_props :
259
265
prop_value = value [prop_name ]
260
266
properties [prop_name ] = self .unmarshallers_factory .create (
261
267
additional_prop_schema
262
268
)(prop_value )
263
- elif additional_properties is True :
264
- for prop_name in extra_props :
265
- prop_value = value [prop_name ]
266
- properties [prop_name ] = prop_value
267
269
268
270
for prop_name , prop in list (all_props .items ()):
269
271
read_only = prop .getkey ("readOnly" , False )
0 commit comments