Skip to content

Commit

Permalink
Python: Correctly serialize enum with its value (#18327)
Browse files Browse the repository at this point in the history
  • Loading branch information
BaptisteSaves committed Apr 10, 2024
1 parent 9351217 commit ef86851
Show file tree
Hide file tree
Showing 19 changed files with 233 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,8 @@ class ApiClient:
"""
if obj is None:
return None
elif isinstance(obj, Enum):
return obj.value
elif isinstance(obj, SecretStr):
return obj.get_secret_value()
elif isinstance(obj, self.PRIMITIVE_TYPES):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,13 @@ paths:
'*/*':
schema:
$ref: '#/components/schemas/OuterObjectWithEnumProperty'
parameters:
- in: query
name: param
schema:
type: array
items:
$ref: '#/components/schemas/OuterEnumInteger'
requestBody:
required: true
content:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,8 @@ def sanitize_for_serialization(self, obj):
"""
if obj is None:
return None
elif isinstance(obj, Enum):
return obj.value
elif isinstance(obj, SecretStr):
return obj.get_secret_value()
elif isinstance(obj, self.PRIMITIVE_TYPES):
Expand Down
2 changes: 2 additions & 0 deletions samples/client/echo_api/python/openapi_client/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,8 @@ def sanitize_for_serialization(self, obj):
"""
if obj is None:
return None
elif isinstance(obj, Enum):
return obj.value
elif isinstance(obj, SecretStr):
return obj.get_secret_value()
elif isinstance(obj, self.PRIMITIVE_TYPES):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ No authorization required
[[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)

# **fake_property_enum_integer_serialize**
> OuterObjectWithEnumProperty fake_property_enum_integer_serialize(outer_object_with_enum_property)
> OuterObjectWithEnumProperty fake_property_enum_integer_serialize(outer_object_with_enum_property, param=param)


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

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

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

### Return type

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from petstore_api.models.file_schema_test_class import FileSchemaTestClass
from petstore_api.models.health_check_result import HealthCheckResult
from petstore_api.models.outer_composite import OuterComposite
from petstore_api.models.outer_enum_integer import OuterEnumInteger
from petstore_api.models.outer_object_with_enum_property import OuterObjectWithEnumProperty
from petstore_api.models.pet import Pet
from petstore_api.models.tag import Tag
Expand Down Expand Up @@ -2179,6 +2180,7 @@ def _fake_outer_string_serialize_serialize(
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[List[OuterEnumInteger]] = None,
_request_timeout: Union[
None,
Annotated[StrictFloat, Field(gt=0)],
Expand All @@ -2198,6 +2200,8 @@ async def fake_property_enum_integer_serialize(
:param outer_object_with_enum_property: Input enum (int) as post body (required)
:type outer_object_with_enum_property: OuterObjectWithEnumProperty
:param param:
:type param: List[OuterEnumInteger]
:param _request_timeout: timeout setting for this request. If one
number provided, it will be total request
timeout. It can also be a pair (tuple) of
Expand All @@ -2222,6 +2226,7 @@ async def fake_property_enum_integer_serialize(

_param = self._fake_property_enum_integer_serialize_serialize(
outer_object_with_enum_property=outer_object_with_enum_property,
param=param,
_request_auth=_request_auth,
_content_type=_content_type,
_headers=_headers,
Expand All @@ -2246,6 +2251,7 @@ async def fake_property_enum_integer_serialize(
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[List[OuterEnumInteger]] = None,
_request_timeout: Union[
None,
Annotated[StrictFloat, Field(gt=0)],
Expand All @@ -2265,6 +2271,8 @@ async def fake_property_enum_integer_serialize_with_http_info(
:param outer_object_with_enum_property: Input enum (int) as post body (required)
:type outer_object_with_enum_property: OuterObjectWithEnumProperty
:param param:
:type param: List[OuterEnumInteger]
:param _request_timeout: timeout setting for this request. If one
number provided, it will be total request
timeout. It can also be a pair (tuple) of
Expand All @@ -2289,6 +2297,7 @@ async def fake_property_enum_integer_serialize_with_http_info(

_param = self._fake_property_enum_integer_serialize_serialize(
outer_object_with_enum_property=outer_object_with_enum_property,
param=param,
_request_auth=_request_auth,
_content_type=_content_type,
_headers=_headers,
Expand All @@ -2313,6 +2322,7 @@ async def fake_property_enum_integer_serialize_with_http_info(
async def fake_property_enum_integer_serialize_without_preload_content(
self,
outer_object_with_enum_property: Annotated[OuterObjectWithEnumProperty, Field(description="Input enum (int) as post body")],
param: Optional[List[OuterEnumInteger]] = None,
_request_timeout: Union[
None,
Annotated[StrictFloat, Field(gt=0)],
Expand All @@ -2332,6 +2342,8 @@ async def fake_property_enum_integer_serialize_without_preload_content(
:param outer_object_with_enum_property: Input enum (int) as post body (required)
:type outer_object_with_enum_property: OuterObjectWithEnumProperty
:param param:
:type param: List[OuterEnumInteger]
:param _request_timeout: timeout setting for this request. If one
number provided, it will be total request
timeout. It can also be a pair (tuple) of
Expand All @@ -2356,6 +2368,7 @@ async def fake_property_enum_integer_serialize_without_preload_content(

_param = self._fake_property_enum_integer_serialize_serialize(
outer_object_with_enum_property=outer_object_with_enum_property,
param=param,
_request_auth=_request_auth,
_content_type=_content_type,
_headers=_headers,
Expand All @@ -2375,6 +2388,7 @@ async def fake_property_enum_integer_serialize_without_preload_content(
def _fake_property_enum_integer_serialize_serialize(
self,
outer_object_with_enum_property,
param,
_request_auth,
_content_type,
_headers,
Expand All @@ -2384,6 +2398,7 @@ def _fake_property_enum_integer_serialize_serialize(
_host = None

_collection_formats: Dict[str, str] = {
'param': 'multi',
}

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

# process the path parameters
# process the query parameters
if param is not None:

_query_params.append(('param', param))

# process the header parameters
# process the form parameters
# process the body parameter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,8 @@ def sanitize_for_serialization(self, obj):
"""
if obj is None:
return None
elif isinstance(obj, Enum):
return obj.value
elif isinstance(obj, SecretStr):
return obj.get_secret_value()
elif isinstance(obj, self.PRIMITIVE_TYPES):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ No authorization required
[[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)

# **fake_property_enum_integer_serialize**
> OuterObjectWithEnumProperty fake_property_enum_integer_serialize(outer_object_with_enum_property)
> OuterObjectWithEnumProperty fake_property_enum_integer_serialize(outer_object_with_enum_property, param=param)


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

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

### Return type

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from petstore_api.models.file_schema_test_class import FileSchemaTestClass
from petstore_api.models.health_check_result import HealthCheckResult
from petstore_api.models.outer_composite import OuterComposite
from petstore_api.models.outer_enum_integer import OuterEnumInteger
from petstore_api.models.outer_object_with_enum_property import OuterObjectWithEnumProperty
from petstore_api.models.pet import Pet
from petstore_api.models.tag import Tag
Expand Down Expand Up @@ -1075,13 +1076,15 @@ async def fake_outer_string_serialize_with_http_info(self, body : Annotated[Opti
_request_auth=_params.get('_request_auth'))

@validate_arguments
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
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
"""fake_property_enum_integer_serialize # noqa: E501
Test serialization of enum (int) properties with examples # noqa: E501
:param outer_object_with_enum_property: Input enum (int) as post body (required)
:type outer_object_with_enum_property: OuterObjectWithEnumProperty
:param param:
:type param: List[OuterEnumInteger]
:param _request_timeout: timeout setting for this request.
If one number provided, it will be total request
timeout. It can also be a pair (tuple) of
Expand All @@ -1095,16 +1098,18 @@ async def fake_property_enum_integer_serialize(self, outer_object_with_enum_prop
if '_preload_content' in kwargs:
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
raise ValueError(message)
return await self.fake_property_enum_integer_serialize_with_http_info(outer_object_with_enum_property, **kwargs) # noqa: E501
return await self.fake_property_enum_integer_serialize_with_http_info(outer_object_with_enum_property, param, **kwargs) # noqa: E501

@validate_arguments
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
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
"""fake_property_enum_integer_serialize # noqa: E501
Test serialization of enum (int) properties with examples # noqa: E501
:param outer_object_with_enum_property: Input enum (int) as post body (required)
:type outer_object_with_enum_property: OuterObjectWithEnumProperty
:param param:
:type param: List[OuterEnumInteger]
:param _preload_content: if False, the ApiResponse.data will
be set to none and raw_data will store the
HTTP response body without reading/decoding.
Expand All @@ -1131,7 +1136,8 @@ async def fake_property_enum_integer_serialize_with_http_info(self, outer_object
_params = locals()

_all_params = [
'outer_object_with_enum_property'
'outer_object_with_enum_property',
'param'
]
_all_params.extend(
[
Expand Down Expand Up @@ -1161,6 +1167,10 @@ async def fake_property_enum_integer_serialize_with_http_info(self, outer_object

# process the query parameters
_query_params = []
if _params.get('param') is not None: # noqa: E501
_query_params.append(('param', _params['param']))
_collection_formats['param'] = 'multi'

# process the header parameters
_header_params = dict(_params.get('_headers', {}))
# process the form parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ No authorization required
[[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)

# **fake_property_enum_integer_serialize**
> OuterObjectWithEnumProperty fake_property_enum_integer_serialize(outer_object_with_enum_property)
> OuterObjectWithEnumProperty fake_property_enum_integer_serialize(outer_object_with_enum_property, param=param)


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

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

### Return type

Expand Down
Loading

0 comments on commit ef86851

Please sign in to comment.