Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.

Commit b5a64a5

Browse files
committed
Samples updated
1 parent 293e9a2 commit b5a64a5

File tree

3 files changed

+73
-59
lines changed
  • samples/openapi3/client
    • 3_0_3_unit_test/python/unit_test_api
    • features/nonCompliantUseDiscriminatorIfCompositionFails/python/this_package
    • petstore/python/.openapi-generator

3 files changed

+73
-59
lines changed

samples/openapi3/client/3_0_3_unit_test/python/unit_test_api/api_client.py

+36-29
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ def __eq__(self, other):
5353

5454

5555
class JSONEncoder(json.JSONEncoder):
56+
compact_separators = (',', ':')
57+
5658
def default(self, obj):
5759
if isinstance(obj, str):
5860
return str(obj)
@@ -324,8 +326,25 @@ def _serialize_simple(
324326
)
325327

326328

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+
327346
@dataclass
328-
class ParameterBase:
347+
class ParameterBase(JSONDetector):
329348
name: str
330349
in_type: ParameterInType
331350
required: bool
@@ -352,7 +371,6 @@ class ParameterBase:
352371
}
353372
__disallowed_header_names = {'Accept', 'Content-Type', 'Authorization'}
354373
_json_encoder = JSONEncoder()
355-
_json_content_type = 'application/json'
356374

357375
@classmethod
358376
def __verify_style_to_in_type(cls, style: typing.Optional[ParameterStyle], in_type: ParameterInType):
@@ -399,8 +417,11 @@ def __init__(
399417

400418
def _serialize_json(
401419
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
403422
) -> str:
423+
if eliminate_whitespace:
424+
return json.dumps(in_data, separators=self._json_encoder.compact_separators)
404425
return json.dumps(in_data)
405426

406427

@@ -495,7 +516,7 @@ def serialize(
495516
for content_type, schema in self.content.items():
496517
cast_in_data = schema(in_data)
497518
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):
499520
value = self._serialize_json(cast_in_data)
500521
return self._to_dict(self.name, value)
501522
raise NotImplementedError('Serialization of {} has not yet been implemented'.format(content_type))
@@ -513,7 +534,7 @@ def __init__(
513534
schema: typing.Optional[typing.Type[Schema]] = None,
514535
content: typing.Optional[typing.Dict[str, typing.Type[Schema]]] = None
515536
):
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
517538
used_explode = self._get_default_explode(used_style) if explode is None else explode
518539

519540
super().__init__(
@@ -576,8 +597,6 @@ def __serialize_form(
576597
return self._to_dict(self.name, value)
577598

578599
def get_prefix_separator_iterator(self) -> typing.Optional[PrefixSeparatorIterator]:
579-
if not self.schema:
580-
return None
581600
if self.style is ParameterStyle.FORM:
582601
return PrefixSeparatorIterator('?', '&')
583602
elif self.style is ParameterStyle.SPACE_DELIMITED:
@@ -616,12 +635,17 @@ def serialize(
616635
elif self.style is ParameterStyle.PIPE_DELIMITED:
617636
return self.__serialize_pipe_delimited(cast_in_data, prefix_separator_iterator)
618637
# self.content will be length one
638+
if prefix_separator_iterator is None:
639+
prefix_separator_iterator = self.get_prefix_separator_iterator()
619640
for content_type, schema in self.content.items():
620641
cast_in_data = schema(in_data)
621642
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+
)
625649
raise NotImplementedError('Serialization of {} has not yet been implemented'.format(content_type))
626650

627651

@@ -680,7 +704,7 @@ def serialize(
680704
for content_type, schema in self.content.items():
681705
cast_in_data = schema(in_data)
682706
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):
684708
value = self._serialize_json(cast_in_data)
685709
return self._to_dict(self.name, value)
686710
raise NotImplementedError('Serialization of {} has not yet been implemented'.format(content_type))
@@ -737,7 +761,7 @@ def serialize(
737761
for content_type, schema in self.content.items():
738762
cast_in_data = schema(in_data)
739763
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):
741765
value = self._serialize_json(cast_in_data)
742766
return self.__to_headers(((self.name, value),))
743767
raise NotImplementedError('Serialization of {} has not yet been implemented'.format(content_type))
@@ -800,23 +824,6 @@ class ApiResponseWithoutDeserialization(ApiResponse):
800824
headers: typing.Union[Unset, typing.List[HeaderParameter]] = unset
801825

802826

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-
820827
class OpenApiResponse(JSONDetector):
821828
__filename_content_disposition_pattern = re.compile('filename="(.+?)"')
822829

samples/openapi3/client/features/nonCompliantUseDiscriminatorIfCompositionFails/python/this_package/api_client.py

+36-29
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ def __eq__(self, other):
5353

5454

5555
class JSONEncoder(json.JSONEncoder):
56+
compact_separators = (',', ':')
57+
5658
def default(self, obj):
5759
if isinstance(obj, str):
5860
return str(obj)
@@ -324,8 +326,25 @@ def _serialize_simple(
324326
)
325327

326328

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+
327346
@dataclass
328-
class ParameterBase:
347+
class ParameterBase(JSONDetector):
329348
name: str
330349
in_type: ParameterInType
331350
required: bool
@@ -352,7 +371,6 @@ class ParameterBase:
352371
}
353372
__disallowed_header_names = {'Accept', 'Content-Type', 'Authorization'}
354373
_json_encoder = JSONEncoder()
355-
_json_content_type = 'application/json'
356374

357375
@classmethod
358376
def __verify_style_to_in_type(cls, style: typing.Optional[ParameterStyle], in_type: ParameterInType):
@@ -399,8 +417,11 @@ def __init__(
399417

400418
def _serialize_json(
401419
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
403422
) -> str:
423+
if eliminate_whitespace:
424+
return json.dumps(in_data, separators=self._json_encoder.compact_separators)
404425
return json.dumps(in_data)
405426

406427

@@ -495,7 +516,7 @@ def serialize(
495516
for content_type, schema in self.content.items():
496517
cast_in_data = schema(in_data)
497518
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):
499520
value = self._serialize_json(cast_in_data)
500521
return self._to_dict(self.name, value)
501522
raise NotImplementedError('Serialization of {} has not yet been implemented'.format(content_type))
@@ -513,7 +534,7 @@ def __init__(
513534
schema: typing.Optional[typing.Type[Schema]] = None,
514535
content: typing.Optional[typing.Dict[str, typing.Type[Schema]]] = None
515536
):
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
517538
used_explode = self._get_default_explode(used_style) if explode is None else explode
518539

519540
super().__init__(
@@ -576,8 +597,6 @@ def __serialize_form(
576597
return self._to_dict(self.name, value)
577598

578599
def get_prefix_separator_iterator(self) -> typing.Optional[PrefixSeparatorIterator]:
579-
if not self.schema:
580-
return None
581600
if self.style is ParameterStyle.FORM:
582601
return PrefixSeparatorIterator('?', '&')
583602
elif self.style is ParameterStyle.SPACE_DELIMITED:
@@ -616,12 +635,17 @@ def serialize(
616635
elif self.style is ParameterStyle.PIPE_DELIMITED:
617636
return self.__serialize_pipe_delimited(cast_in_data, prefix_separator_iterator)
618637
# self.content will be length one
638+
if prefix_separator_iterator is None:
639+
prefix_separator_iterator = self.get_prefix_separator_iterator()
619640
for content_type, schema in self.content.items():
620641
cast_in_data = schema(in_data)
621642
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+
)
625649
raise NotImplementedError('Serialization of {} has not yet been implemented'.format(content_type))
626650

627651

@@ -680,7 +704,7 @@ def serialize(
680704
for content_type, schema in self.content.items():
681705
cast_in_data = schema(in_data)
682706
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):
684708
value = self._serialize_json(cast_in_data)
685709
return self._to_dict(self.name, value)
686710
raise NotImplementedError('Serialization of {} has not yet been implemented'.format(content_type))
@@ -737,7 +761,7 @@ def serialize(
737761
for content_type, schema in self.content.items():
738762
cast_in_data = schema(in_data)
739763
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):
741765
value = self._serialize_json(cast_in_data)
742766
return self.__to_headers(((self.name, value),))
743767
raise NotImplementedError('Serialization of {} has not yet been implemented'.format(content_type))
@@ -800,23 +824,6 @@ class ApiResponseWithoutDeserialization(ApiResponse):
800824
headers: typing.Union[Unset, typing.List[HeaderParameter]] = unset
801825

802826

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-
820827
class OpenApiResponse(JSONDetector):
821828
__filename_content_disposition_pattern = re.compile('filename="(.+?)"')
822829

Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
unset
1+
1.0.0

0 commit comments

Comments
 (0)