@@ -371,9 +371,14 @@ def serialize_json(request: dataclass) -> str:
371
371
def dict_to_dataclass (orig : dict [str , any ], dataclass_type : str ):
372
372
cast_type = str (dataclass_type ).replace (
373
373
"typing.Optional[" , "" ).replace ("]" , "" )
374
- cast_module = cast_type .split ("." )[:- 1 ]
374
+
375
+ cast_modules = cast_type .split ("." )[:- 1 ]
376
+ if cast_modules [0 ] == "typing" :
377
+ # This is a built-in type, not a data_class
378
+ return orig
379
+
375
380
module = None
376
- for m in cast_module :
381
+ for m in cast_modules :
377
382
if not module :
378
383
module = __import__ (m )
379
384
else :
@@ -579,7 +584,7 @@ def serialize_form(data: dataclass, meta_string: str) -> dict[str, any]:
579
584
580
585
581
586
def _populate_form (field_name : str , explode : boolean , obj : any , get_field_name_func : Callable ) -> dict [str , list [str ]]:
582
- params : dict [str , list [str ]] = {}
587
+ params : dict [str , str | list [str ]] = {}
583
588
584
589
if is_dataclass (obj ):
585
590
items = []
@@ -602,12 +607,7 @@ def _populate_form(field_name: str, explode: boolean, obj: any, get_field_name_f
602
607
items = []
603
608
for key , value in obj .items ():
604
609
if explode :
605
- # Python uses True and False instead of true and false for booleans;
606
- # This json encodes the values _only_ if the value is a boolean.
607
- if value is True or value is False :
608
- params [key ] = json .dumps (value )
609
- else :
610
- params [key ] = value
610
+ _populate_simple_param (params , key , value )
611
611
else :
612
612
items .append (f'{ key } ,{ value } ' )
613
613
@@ -627,11 +627,20 @@ def _populate_form(field_name: str, explode: boolean, obj: any, get_field_name_f
627
627
if len (items ) > 0 :
628
628
params [field_name ] = [',' .join ([str (item ) for item in items ])]
629
629
else :
630
- params [ field_name ] = obj
630
+ _populate_simple_param ( params , field_name , obj )
631
631
632
632
return params
633
633
634
634
635
+ def _populate_simple_param (params : dict [str , str | list [str ]], field_name : str , value : any ):
636
+ # Python uses True and False instead of true and false for booleans;
637
+ # This json encodes the values _only_ if the value is a boolean.
638
+ if value is True or value is False :
639
+ params [field_name ] = json .dumps (value )
640
+ else :
641
+ params [field_name ] = value
642
+
643
+
635
644
def _serialize_header (explode : boolean , obj : any ) -> str :
636
645
if is_dataclass (obj ):
637
646
items = []
@@ -675,11 +684,8 @@ def _serialize_header(explode: boolean, obj: any) -> str:
675
684
676
685
677
686
def unmarshal_json (data , t ):
678
- Unmarhsal = make_dataclass ('Unmarhsal' , [('res' , t )],
679
- bases = (DataClassJsonMixin ,))
680
687
d = json .loads (data )
681
- out = Unmarhsal .from_dict ({"res" : d })
682
- return out .res
688
+ return dict_to_dataclass (d , t )
683
689
684
690
685
691
def marshal_json (c ):
0 commit comments