Skip to content

Commit 6bcc28d

Browse files
Python: Correctly serialize enum with its value (#18327) (#18328)
1 parent 9351217 commit 6bcc28d

19 files changed

Lines changed: 233 additions & 30 deletions

File tree

modules/openapi-generator/src/main/resources/python/api_client.mustache

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,8 @@ class ApiClient:
358358
"""
359359
if obj is None:
360360
return None
361+
elif isinstance(obj, Enum):
362+
return obj.value
361363
elif isinstance(obj, SecretStr):
362364
return obj.get_secret_value()
363365
elif isinstance(obj, self.PRIMITIVE_TYPES):

modules/openapi-generator/src/test/resources/3_0/python/petstore-with-fake-endpoints-models-for-testing.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,13 @@ paths:
894894
'*/*':
895895
schema:
896896
$ref: '#/components/schemas/OuterObjectWithEnumProperty'
897+
parameters:
898+
- in: query
899+
name: param
900+
schema:
901+
type: array
902+
items:
903+
$ref: '#/components/schemas/OuterEnumInteger'
897904
requestBody:
898905
required: true
899906
content:

samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent/openapi_client/api_client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,8 @@ def sanitize_for_serialization(self, obj):
351351
"""
352352
if obj is None:
353353
return None
354+
elif isinstance(obj, Enum):
355+
return obj.value
354356
elif isinstance(obj, SecretStr):
355357
return obj.get_secret_value()
356358
elif isinstance(obj, self.PRIMITIVE_TYPES):

samples/client/echo_api/python/openapi_client/api_client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,8 @@ def sanitize_for_serialization(self, obj):
351351
"""
352352
if obj is None:
353353
return None
354+
elif isinstance(obj, Enum):
355+
return obj.value
354356
elif isinstance(obj, SecretStr):
355357
return obj.get_secret_value()
356358
elif isinstance(obj, self.PRIMITIVE_TYPES):

samples/openapi3/client/petstore/python-aiohttp/docs/FakeApi.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ No authorization required
632632
[[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md)
633633

634634
# **fake_property_enum_integer_serialize**
635-
> OuterObjectWithEnumProperty fake_property_enum_integer_serialize(outer_object_with_enum_property)
635+
> OuterObjectWithEnumProperty fake_property_enum_integer_serialize(outer_object_with_enum_property, param=param)
636636
637637

638638

@@ -643,6 +643,7 @@ Test serialization of enum (int) properties with examples
643643

644644
```python
645645
import petstore_api
646+
from petstore_api.models.outer_enum_integer import OuterEnumInteger
646647
from petstore_api.models.outer_object_with_enum_property import OuterObjectWithEnumProperty
647648
from petstore_api.rest import ApiException
648649
from pprint import pprint
@@ -659,9 +660,10 @@ async with petstore_api.ApiClient(configuration) as api_client:
659660
# Create an instance of the API class
660661
api_instance = petstore_api.FakeApi(api_client)
661662
outer_object_with_enum_property = petstore_api.OuterObjectWithEnumProperty() # OuterObjectWithEnumProperty | Input enum (int) as post body
663+
param = [petstore_api.OuterEnumInteger()] # List[OuterEnumInteger] | (optional)
662664

663665
try:
664-
api_response = await api_instance.fake_property_enum_integer_serialize(outer_object_with_enum_property)
666+
api_response = await api_instance.fake_property_enum_integer_serialize(outer_object_with_enum_property, param=param)
665667
print("The response of FakeApi->fake_property_enum_integer_serialize:\n")
666668
pprint(api_response)
667669
except Exception as e:
@@ -676,6 +678,7 @@ async with petstore_api.ApiClient(configuration) as api_client:
676678
Name | Type | Description | Notes
677679
------------- | ------------- | ------------- | -------------
678680
**outer_object_with_enum_property** | [**OuterObjectWithEnumProperty**](OuterObjectWithEnumProperty.md)| Input enum (int) as post body |
681+
**param** | [**List[OuterEnumInteger]**](OuterEnumInteger.md)| | [optional]
679682

680683
### Return type
681684

samples/openapi3/client/petstore/python-aiohttp/petstore_api/api/fake_api.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from petstore_api.models.file_schema_test_class import FileSchemaTestClass
2626
from petstore_api.models.health_check_result import HealthCheckResult
2727
from petstore_api.models.outer_composite import OuterComposite
28+
from petstore_api.models.outer_enum_integer import OuterEnumInteger
2829
from petstore_api.models.outer_object_with_enum_property import OuterObjectWithEnumProperty
2930
from petstore_api.models.pet import Pet
3031
from petstore_api.models.tag import Tag
@@ -2179,6 +2180,7 @@ def _fake_outer_string_serialize_serialize(
21792180
async def fake_property_enum_integer_serialize(
21802181
self,
21812182
outer_object_with_enum_property: Annotated[OuterObjectWithEnumProperty, Field(description="Input enum (int) as post body")],
2183+
param: Optional[List[OuterEnumInteger]] = None,
21822184
_request_timeout: Union[
21832185
None,
21842186
Annotated[StrictFloat, Field(gt=0)],
@@ -2198,6 +2200,8 @@ async def fake_property_enum_integer_serialize(
21982200
21992201
:param outer_object_with_enum_property: Input enum (int) as post body (required)
22002202
:type outer_object_with_enum_property: OuterObjectWithEnumProperty
2203+
:param param:
2204+
:type param: List[OuterEnumInteger]
22012205
:param _request_timeout: timeout setting for this request. If one
22022206
number provided, it will be total request
22032207
timeout. It can also be a pair (tuple) of
@@ -2222,6 +2226,7 @@ async def fake_property_enum_integer_serialize(
22222226

22232227
_param = self._fake_property_enum_integer_serialize_serialize(
22242228
outer_object_with_enum_property=outer_object_with_enum_property,
2229+
param=param,
22252230
_request_auth=_request_auth,
22262231
_content_type=_content_type,
22272232
_headers=_headers,
@@ -2246,6 +2251,7 @@ async def fake_property_enum_integer_serialize(
22462251
async def fake_property_enum_integer_serialize_with_http_info(
22472252
self,
22482253
outer_object_with_enum_property: Annotated[OuterObjectWithEnumProperty, Field(description="Input enum (int) as post body")],
2254+
param: Optional[List[OuterEnumInteger]] = None,
22492255
_request_timeout: Union[
22502256
None,
22512257
Annotated[StrictFloat, Field(gt=0)],
@@ -2265,6 +2271,8 @@ async def fake_property_enum_integer_serialize_with_http_info(
22652271
22662272
:param outer_object_with_enum_property: Input enum (int) as post body (required)
22672273
:type outer_object_with_enum_property: OuterObjectWithEnumProperty
2274+
:param param:
2275+
:type param: List[OuterEnumInteger]
22682276
:param _request_timeout: timeout setting for this request. If one
22692277
number provided, it will be total request
22702278
timeout. It can also be a pair (tuple) of
@@ -2289,6 +2297,7 @@ async def fake_property_enum_integer_serialize_with_http_info(
22892297

22902298
_param = self._fake_property_enum_integer_serialize_serialize(
22912299
outer_object_with_enum_property=outer_object_with_enum_property,
2300+
param=param,
22922301
_request_auth=_request_auth,
22932302
_content_type=_content_type,
22942303
_headers=_headers,
@@ -2313,6 +2322,7 @@ async def fake_property_enum_integer_serialize_with_http_info(
23132322
async def fake_property_enum_integer_serialize_without_preload_content(
23142323
self,
23152324
outer_object_with_enum_property: Annotated[OuterObjectWithEnumProperty, Field(description="Input enum (int) as post body")],
2325+
param: Optional[List[OuterEnumInteger]] = None,
23162326
_request_timeout: Union[
23172327
None,
23182328
Annotated[StrictFloat, Field(gt=0)],
@@ -2332,6 +2342,8 @@ async def fake_property_enum_integer_serialize_without_preload_content(
23322342
23332343
:param outer_object_with_enum_property: Input enum (int) as post body (required)
23342344
:type outer_object_with_enum_property: OuterObjectWithEnumProperty
2345+
:param param:
2346+
:type param: List[OuterEnumInteger]
23352347
:param _request_timeout: timeout setting for this request. If one
23362348
number provided, it will be total request
23372349
timeout. It can also be a pair (tuple) of
@@ -2356,6 +2368,7 @@ async def fake_property_enum_integer_serialize_without_preload_content(
23562368

23572369
_param = self._fake_property_enum_integer_serialize_serialize(
23582370
outer_object_with_enum_property=outer_object_with_enum_property,
2371+
param=param,
23592372
_request_auth=_request_auth,
23602373
_content_type=_content_type,
23612374
_headers=_headers,
@@ -2375,6 +2388,7 @@ async def fake_property_enum_integer_serialize_without_preload_content(
23752388
def _fake_property_enum_integer_serialize_serialize(
23762389
self,
23772390
outer_object_with_enum_property,
2391+
param,
23782392
_request_auth,
23792393
_content_type,
23802394
_headers,
@@ -2384,6 +2398,7 @@ def _fake_property_enum_integer_serialize_serialize(
23842398
_host = None
23852399

23862400
_collection_formats: Dict[str, str] = {
2401+
'param': 'multi',
23872402
}
23882403

23892404
_path_params: Dict[str, str] = {}
@@ -2395,6 +2410,10 @@ def _fake_property_enum_integer_serialize_serialize(
23952410

23962411
# process the path parameters
23972412
# process the query parameters
2413+
if param is not None:
2414+
2415+
_query_params.append(('param', param))
2416+
23982417
# process the header parameters
23992418
# process the form parameters
24002419
# process the body parameter

samples/openapi3/client/petstore/python-aiohttp/petstore_api/api_client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,8 @@ def sanitize_for_serialization(self, obj):
353353
"""
354354
if obj is None:
355355
return None
356+
elif isinstance(obj, Enum):
357+
return obj.value
356358
elif isinstance(obj, SecretStr):
357359
return obj.get_secret_value()
358360
elif isinstance(obj, self.PRIMITIVE_TYPES):

samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/docs/FakeApi.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ No authorization required
624624
[[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md)
625625

626626
# **fake_property_enum_integer_serialize**
627-
> OuterObjectWithEnumProperty fake_property_enum_integer_serialize(outer_object_with_enum_property)
627+
> OuterObjectWithEnumProperty fake_property_enum_integer_serialize(outer_object_with_enum_property, param=param)
628628
629629

630630

@@ -636,6 +636,7 @@ Test serialization of enum (int) properties with examples
636636
import time
637637
import os
638638
import petstore_api
639+
from petstore_api.models.outer_enum_integer import OuterEnumInteger
639640
from petstore_api.models.outer_object_with_enum_property import OuterObjectWithEnumProperty
640641
from petstore_api.rest import ApiException
641642
from pprint import pprint
@@ -652,9 +653,10 @@ async with petstore_api.ApiClient(configuration) as api_client:
652653
# Create an instance of the API class
653654
api_instance = petstore_api.FakeApi(api_client)
654655
outer_object_with_enum_property = petstore_api.OuterObjectWithEnumProperty() # OuterObjectWithEnumProperty | Input enum (int) as post body
656+
param = [petstore_api.OuterEnumInteger()] # List[OuterEnumInteger] | (optional)
655657

656658
try:
657-
api_response = await api_instance.fake_property_enum_integer_serialize(outer_object_with_enum_property)
659+
api_response = await api_instance.fake_property_enum_integer_serialize(outer_object_with_enum_property, param=param)
658660
print("The response of FakeApi->fake_property_enum_integer_serialize:\n")
659661
pprint(api_response)
660662
except Exception as e:
@@ -668,6 +670,7 @@ async with petstore_api.ApiClient(configuration) as api_client:
668670
Name | Type | Description | Notes
669671
------------- | ------------- | ------------- | -------------
670672
**outer_object_with_enum_property** | [**OuterObjectWithEnumProperty**](OuterObjectWithEnumProperty.md)| Input enum (int) as post body |
673+
**param** | [**List[OuterEnumInteger]**](OuterEnumInteger.md)| | [optional]
671674

672675
### Return type
673676

samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/petstore_api/api/fake_api.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from petstore_api.models.file_schema_test_class import FileSchemaTestClass
3232
from petstore_api.models.health_check_result import HealthCheckResult
3333
from petstore_api.models.outer_composite import OuterComposite
34+
from petstore_api.models.outer_enum_integer import OuterEnumInteger
3435
from petstore_api.models.outer_object_with_enum_property import OuterObjectWithEnumProperty
3536
from petstore_api.models.pet import Pet
3637
from petstore_api.models.tag import Tag
@@ -1075,13 +1076,15 @@ async def fake_outer_string_serialize_with_http_info(self, body : Annotated[Opti
10751076
_request_auth=_params.get('_request_auth'))
10761077

10771078
@validate_arguments
1078-
async def fake_property_enum_integer_serialize(self, outer_object_with_enum_property : Annotated[OuterObjectWithEnumProperty, Field(..., description="Input enum (int) as post body")], **kwargs) -> OuterObjectWithEnumProperty: # noqa: E501
1079+
async def fake_property_enum_integer_serialize(self, outer_object_with_enum_property : Annotated[OuterObjectWithEnumProperty, Field(..., description="Input enum (int) as post body")], param : Optional[conlist(OuterEnumInteger)] = None, **kwargs) -> OuterObjectWithEnumProperty: # noqa: E501
10791080
"""fake_property_enum_integer_serialize # noqa: E501
10801081
10811082
Test serialization of enum (int) properties with examples # noqa: E501
10821083
10831084
:param outer_object_with_enum_property: Input enum (int) as post body (required)
10841085
:type outer_object_with_enum_property: OuterObjectWithEnumProperty
1086+
:param param:
1087+
:type param: List[OuterEnumInteger]
10851088
:param _request_timeout: timeout setting for this request.
10861089
If one number provided, it will be total request
10871090
timeout. It can also be a pair (tuple) of
@@ -1095,16 +1098,18 @@ async def fake_property_enum_integer_serialize(self, outer_object_with_enum_prop
10951098
if '_preload_content' in kwargs:
10961099
message = "Error! Please call the fake_property_enum_integer_serialize_with_http_info method with `_preload_content` instead and obtain raw data from ApiResponse.raw_data" # noqa: E501
10971100
raise ValueError(message)
1098-
return await self.fake_property_enum_integer_serialize_with_http_info(outer_object_with_enum_property, **kwargs) # noqa: E501
1101+
return await self.fake_property_enum_integer_serialize_with_http_info(outer_object_with_enum_property, param, **kwargs) # noqa: E501
10991102

11001103
@validate_arguments
1101-
async def fake_property_enum_integer_serialize_with_http_info(self, outer_object_with_enum_property : Annotated[OuterObjectWithEnumProperty, Field(..., description="Input enum (int) as post body")], **kwargs) -> ApiResponse: # noqa: E501
1104+
async def fake_property_enum_integer_serialize_with_http_info(self, outer_object_with_enum_property : Annotated[OuterObjectWithEnumProperty, Field(..., description="Input enum (int) as post body")], param : Optional[conlist(OuterEnumInteger)] = None, **kwargs) -> ApiResponse: # noqa: E501
11021105
"""fake_property_enum_integer_serialize # noqa: E501
11031106
11041107
Test serialization of enum (int) properties with examples # noqa: E501
11051108
11061109
:param outer_object_with_enum_property: Input enum (int) as post body (required)
11071110
:type outer_object_with_enum_property: OuterObjectWithEnumProperty
1111+
:param param:
1112+
:type param: List[OuterEnumInteger]
11081113
:param _preload_content: if False, the ApiResponse.data will
11091114
be set to none and raw_data will store the
11101115
HTTP response body without reading/decoding.
@@ -1131,7 +1136,8 @@ async def fake_property_enum_integer_serialize_with_http_info(self, outer_object
11311136
_params = locals()
11321137

11331138
_all_params = [
1134-
'outer_object_with_enum_property'
1139+
'outer_object_with_enum_property',
1140+
'param'
11351141
]
11361142
_all_params.extend(
11371143
[
@@ -1161,6 +1167,10 @@ async def fake_property_enum_integer_serialize_with_http_info(self, outer_object
11611167

11621168
# process the query parameters
11631169
_query_params = []
1170+
if _params.get('param') is not None: # noqa: E501
1171+
_query_params.append(('param', _params['param']))
1172+
_collection_formats['param'] = 'multi'
1173+
11641174
# process the header parameters
11651175
_header_params = dict(_params.get('_headers', {}))
11661176
# process the form parameters

samples/openapi3/client/petstore/python-pydantic-v1/docs/FakeApi.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ No authorization required
624624
[[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md)
625625

626626
# **fake_property_enum_integer_serialize**
627-
> OuterObjectWithEnumProperty fake_property_enum_integer_serialize(outer_object_with_enum_property)
627+
> OuterObjectWithEnumProperty fake_property_enum_integer_serialize(outer_object_with_enum_property, param=param)
628628
629629

630630

@@ -636,6 +636,7 @@ Test serialization of enum (int) properties with examples
636636
import time
637637
import os
638638
import petstore_api
639+
from petstore_api.models.outer_enum_integer import OuterEnumInteger
639640
from petstore_api.models.outer_object_with_enum_property import OuterObjectWithEnumProperty
640641
from petstore_api.rest import ApiException
641642
from pprint import pprint
@@ -652,9 +653,10 @@ with petstore_api.ApiClient(configuration) as api_client:
652653
# Create an instance of the API class
653654
api_instance = petstore_api.FakeApi(api_client)
654655
outer_object_with_enum_property = petstore_api.OuterObjectWithEnumProperty() # OuterObjectWithEnumProperty | Input enum (int) as post body
656+
param = [petstore_api.OuterEnumInteger()] # List[OuterEnumInteger] | (optional)
655657

656658
try:
657-
api_response = api_instance.fake_property_enum_integer_serialize(outer_object_with_enum_property)
659+
api_response = api_instance.fake_property_enum_integer_serialize(outer_object_with_enum_property, param=param)
658660
print("The response of FakeApi->fake_property_enum_integer_serialize:\n")
659661
pprint(api_response)
660662
except Exception as e:
@@ -668,6 +670,7 @@ with petstore_api.ApiClient(configuration) as api_client:
668670
Name | Type | Description | Notes
669671
------------- | ------------- | ------------- | -------------
670672
**outer_object_with_enum_property** | [**OuterObjectWithEnumProperty**](OuterObjectWithEnumProperty.md)| Input enum (int) as post body |
673+
**param** | [**List[OuterEnumInteger]**](OuterEnumInteger.md)| | [optional]
671674

672675
### Return type
673676

0 commit comments

Comments
 (0)