@@ -53,6 +53,8 @@ def __eq__(self, other):
53
53
54
54
55
55
class JSONEncoder (json .JSONEncoder ):
56
+ compact_separators = (',' , ':' )
57
+
56
58
def default (self , obj ):
57
59
if isinstance (obj , str ):
58
60
return str (obj )
@@ -324,8 +326,25 @@ def _serialize_simple(
324
326
)
325
327
326
328
329
+ class JSONDetector :
330
+ """
331
+ Works for:
332
+ application/json
333
+ application/json; charset=UTF-8
334
+ application/json-patch+json
335
+ application/geo+json
336
+ """
337
+ __json_content_type_pattern = re .compile ("application/[^+]*[+]?(json);?.*" )
338
+
339
+ @classmethod
340
+ def _content_type_is_json (cls , content_type : str ) -> bool :
341
+ if cls .__json_content_type_pattern .match (content_type ):
342
+ return True
343
+ return False
344
+
345
+
327
346
@dataclass
328
- class ParameterBase :
347
+ class ParameterBase ( JSONDetector ) :
329
348
name : str
330
349
in_type : ParameterInType
331
350
required : bool
@@ -352,7 +371,6 @@ class ParameterBase:
352
371
}
353
372
__disallowed_header_names = {'Accept' , 'Content-Type' , 'Authorization' }
354
373
_json_encoder = JSONEncoder ()
355
- _json_content_type = 'application/json'
356
374
357
375
@classmethod
358
376
def __verify_style_to_in_type (cls , style : typing .Optional [ParameterStyle ], in_type : ParameterInType ):
@@ -399,8 +417,11 @@ def __init__(
399
417
400
418
def _serialize_json (
401
419
self ,
402
- in_data : typing .Union [None , int , float , str , bool , dict , list ]
420
+ in_data : typing .Union [None , int , float , str , bool , dict , list ],
421
+ eliminate_whitespace : bool = False
403
422
) -> str :
423
+ if eliminate_whitespace :
424
+ return json .dumps (in_data , separators = self ._json_encoder .compact_separators )
404
425
return json .dumps (in_data )
405
426
406
427
@@ -495,7 +516,7 @@ def serialize(
495
516
for content_type , schema in self .content .items ():
496
517
cast_in_data = schema (in_data )
497
518
cast_in_data = self ._json_encoder .default (cast_in_data )
498
- if content_type == self ._json_content_type :
519
+ if self ._content_type_is_json ( content_type ) :
499
520
value = self ._serialize_json (cast_in_data )
500
521
return self ._to_dict (self .name , value )
501
522
raise NotImplementedError ('Serialization of {} has not yet been implemented' .format (content_type ))
@@ -513,7 +534,7 @@ def __init__(
513
534
schema : typing .Optional [typing .Type [Schema ]] = None ,
514
535
content : typing .Optional [typing .Dict [str , typing .Type [Schema ]]] = None
515
536
):
516
- used_style = ParameterStyle .FORM if style is None and content is None and schema else style
537
+ used_style = ParameterStyle .FORM if style is None else style
517
538
used_explode = self ._get_default_explode (used_style ) if explode is None else explode
518
539
519
540
super ().__init__ (
@@ -576,8 +597,6 @@ def __serialize_form(
576
597
return self ._to_dict (self .name , value )
577
598
578
599
def get_prefix_separator_iterator (self ) -> typing .Optional [PrefixSeparatorIterator ]:
579
- if not self .schema :
580
- return None
581
600
if self .style is ParameterStyle .FORM :
582
601
return PrefixSeparatorIterator ('?' , '&' )
583
602
elif self .style is ParameterStyle .SPACE_DELIMITED :
@@ -616,12 +635,17 @@ def serialize(
616
635
elif self .style is ParameterStyle .PIPE_DELIMITED :
617
636
return self .__serialize_pipe_delimited (cast_in_data , prefix_separator_iterator )
618
637
# self.content will be length one
638
+ if prefix_separator_iterator is None :
639
+ prefix_separator_iterator = self .get_prefix_separator_iterator ()
619
640
for content_type , schema in self .content .items ():
620
641
cast_in_data = schema (in_data )
621
642
cast_in_data = self ._json_encoder .default (cast_in_data )
622
- if content_type == self ._json_content_type :
623
- value = self ._serialize_json (cast_in_data )
624
- return self ._to_dict (self .name , value )
643
+ if self ._content_type_is_json (content_type ):
644
+ value = self ._serialize_json (cast_in_data , eliminate_whitespace = True )
645
+ return self ._to_dict (
646
+ self .name ,
647
+ next (prefix_separator_iterator ) + self .name + '=' + quote (value )
648
+ )
625
649
raise NotImplementedError ('Serialization of {} has not yet been implemented' .format (content_type ))
626
650
627
651
@@ -680,7 +704,7 @@ def serialize(
680
704
for content_type , schema in self .content .items ():
681
705
cast_in_data = schema (in_data )
682
706
cast_in_data = self ._json_encoder .default (cast_in_data )
683
- if content_type == self ._json_content_type :
707
+ if self ._content_type_is_json ( content_type ) :
684
708
value = self ._serialize_json (cast_in_data )
685
709
return self ._to_dict (self .name , value )
686
710
raise NotImplementedError ('Serialization of {} has not yet been implemented' .format (content_type ))
@@ -737,7 +761,7 @@ def serialize(
737
761
for content_type , schema in self .content .items ():
738
762
cast_in_data = schema (in_data )
739
763
cast_in_data = self ._json_encoder .default (cast_in_data )
740
- if content_type == self ._json_content_type :
764
+ if self ._content_type_is_json ( content_type ) :
741
765
value = self ._serialize_json (cast_in_data )
742
766
return self .__to_headers (((self .name , value ),))
743
767
raise NotImplementedError ('Serialization of {} has not yet been implemented' .format (content_type ))
@@ -800,23 +824,6 @@ class ApiResponseWithoutDeserialization(ApiResponse):
800
824
headers : typing .Union [Unset , typing .List [HeaderParameter ]] = unset
801
825
802
826
803
- class JSONDetector :
804
- """
805
- Works for:
806
- application/json
807
- application/json; charset=UTF-8
808
- application/json-patch+json
809
- application/geo+json
810
- """
811
- __json_content_type_pattern = re .compile ("application/[^+]*[+]?(json);?.*" )
812
-
813
- @classmethod
814
- def _content_type_is_json (cls , content_type : str ) -> bool :
815
- if cls .__json_content_type_pattern .match (content_type ):
816
- return True
817
- return False
818
-
819
-
820
827
class OpenApiResponse (JSONDetector ):
821
828
__filename_content_disposition_pattern = re .compile ('filename="(.+?)"' )
822
829
0 commit comments