Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding decimal support for python client generation #19203

Merged
merged 6 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import datetime
from dateutil.parser import parse
from enum import Enum
import decimal
import json
import mimetypes
import os
Expand Down Expand Up @@ -59,6 +60,7 @@ class ApiClient:
'bool': bool,
'date': datetime.date,
'datetime': datetime.datetime,
'decimal': decimal.Decimal,
'object': object,
}
_pool = None
Expand Down Expand Up @@ -346,6 +348,7 @@ class ApiClient:
If obj is str, int, long, float, bool, return directly.
If obj is datetime.datetime, datetime.date
convert to string in iso8601 format.
If obj is decimal.Decimal return string representation.
If obj is list, sanitize each element in the list.
If obj is dict, return the dict.
If obj is OpenAPI model, return the properties dict.
Expand All @@ -371,6 +374,8 @@ class ApiClient:
)
elif isinstance(obj, (datetime.datetime, datetime.date)):
return obj.isoformat()
elif isinstance(obj, decimal.Decimal):
return str(obj)

elif isinstance(obj, dict):
obj_dict = obj
Expand Down Expand Up @@ -462,6 +467,8 @@ class ApiClient:
return self.__deserialize_date(data)
elif klass == datetime.datetime:
return self.__deserialize_datetime(data)
elif klass == decimal.Decimal:
return decimal.Decimal(data)
elif issubclass(klass, Enum):
return self.__deserialize_enum(data, klass)
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import datetime
from dateutil.parser import parse
from enum import Enum
import decimal
import json
import mimetypes
import os
Expand Down Expand Up @@ -67,6 +68,7 @@ class ApiClient:
'bool': bool,
'date': datetime.date,
'datetime': datetime.datetime,
'decimal': decimal.Decimal,
'object': object,
}
_pool = None
Expand Down Expand Up @@ -339,6 +341,7 @@ def sanitize_for_serialization(self, obj):
If obj is str, int, long, float, bool, return directly.
If obj is datetime.datetime, datetime.date
convert to string in iso8601 format.
If obj is decimal.Decimal return string representation.
If obj is list, sanitize each element in the list.
If obj is dict, return the dict.
If obj is OpenAPI model, return the properties dict.
Expand All @@ -364,6 +367,8 @@ def sanitize_for_serialization(self, obj):
)
elif isinstance(obj, (datetime.datetime, datetime.date)):
return obj.isoformat()
elif isinstance(obj, decimal.Decimal):
return str(obj)

elif isinstance(obj, dict):
obj_dict = obj
Expand Down Expand Up @@ -455,6 +460,8 @@ def __deserialize(self, data, klass):
return self.__deserialize_date(data)
elif klass == datetime.datetime:
return self.__deserialize_datetime(data)
elif klass == decimal.Decimal:
return decimal.Decimal(data)
elif issubclass(klass, Enum):
return self.__deserialize_enum(data, klass)
else:
Expand Down
7 changes: 7 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 @@ -16,6 +16,7 @@
import datetime
from dateutil.parser import parse
from enum import Enum
import decimal
import json
import mimetypes
import os
Expand Down Expand Up @@ -67,6 +68,7 @@ class ApiClient:
'bool': bool,
'date': datetime.date,
'datetime': datetime.datetime,
'decimal': decimal.Decimal,
'object': object,
}
_pool = None
Expand Down Expand Up @@ -339,6 +341,7 @@ def sanitize_for_serialization(self, obj):
If obj is str, int, long, float, bool, return directly.
If obj is datetime.datetime, datetime.date
convert to string in iso8601 format.
If obj is decimal.Decimal return string representation.
If obj is list, sanitize each element in the list.
If obj is dict, return the dict.
If obj is OpenAPI model, return the properties dict.
Expand All @@ -364,6 +367,8 @@ def sanitize_for_serialization(self, obj):
)
elif isinstance(obj, (datetime.datetime, datetime.date)):
return obj.isoformat()
elif isinstance(obj, decimal.Decimal):
return str(obj)

elif isinstance(obj, dict):
obj_dict = obj
Expand Down Expand Up @@ -455,6 +460,8 @@ def __deserialize(self, data, klass):
return self.__deserialize_date(data)
elif klass == datetime.datetime:
return self.__deserialize_datetime(data)
elif klass == decimal.Decimal:
return decimal.Decimal(data)
elif issubclass(klass, Enum):
return self.__deserialize_enum(data, klass)
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import datetime
from dateutil.parser import parse
from enum import Enum
import decimal
import json
import mimetypes
import os
Expand Down Expand Up @@ -66,6 +67,7 @@ class ApiClient:
'bool': bool,
'date': datetime.date,
'datetime': datetime.datetime,
'decimal': decimal.Decimal,
'object': object,
}
_pool = None
Expand Down Expand Up @@ -341,6 +343,7 @@ def sanitize_for_serialization(self, obj):
If obj is str, int, long, float, bool, return directly.
If obj is datetime.datetime, datetime.date
convert to string in iso8601 format.
If obj is decimal.Decimal return string representation.
If obj is list, sanitize each element in the list.
If obj is dict, return the dict.
If obj is OpenAPI model, return the properties dict.
Expand All @@ -366,6 +369,8 @@ def sanitize_for_serialization(self, obj):
)
elif isinstance(obj, (datetime.datetime, datetime.date)):
return obj.isoformat()
elif isinstance(obj, decimal.Decimal):
return str(obj)

elif isinstance(obj, dict):
obj_dict = obj
Expand Down Expand Up @@ -457,6 +462,8 @@ def __deserialize(self, data, klass):
return self.__deserialize_date(data)
elif klass == datetime.datetime:
return self.__deserialize_datetime(data)
elif klass == decimal.Decimal:
return decimal.Decimal(data)
elif issubclass(klass, Enum):
return self.__deserialize_enum(data, klass)
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import datetime
from dateutil.parser import parse
from enum import Enum
import decimal
import json
import mimetypes
import os
Expand Down Expand Up @@ -66,6 +67,7 @@ class ApiClient:
'bool': bool,
'date': datetime.date,
'datetime': datetime.datetime,
'decimal': decimal.Decimal,
'object': object,
}
_pool = None
Expand Down Expand Up @@ -338,6 +340,7 @@ def sanitize_for_serialization(self, obj):
If obj is str, int, long, float, bool, return directly.
If obj is datetime.datetime, datetime.date
convert to string in iso8601 format.
If obj is decimal.Decimal return string representation.
If obj is list, sanitize each element in the list.
If obj is dict, return the dict.
If obj is OpenAPI model, return the properties dict.
Expand All @@ -363,6 +366,8 @@ def sanitize_for_serialization(self, obj):
)
elif isinstance(obj, (datetime.datetime, datetime.date)):
return obj.isoformat()
elif isinstance(obj, decimal.Decimal):
return str(obj)

elif isinstance(obj, dict):
obj_dict = obj
Expand Down Expand Up @@ -454,6 +459,8 @@ def __deserialize(self, data, klass):
return self.__deserialize_date(data)
elif klass == datetime.datetime:
return self.__deserialize_datetime(data)
elif klass == decimal.Decimal:
return decimal.Decimal(data)
elif issubclass(klass, Enum):
return self.__deserialize_enum(data, klass)
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"""

import unittest
from decimal import Decimal
from enum import Enum

from dateutil.parser import parse
Expand Down Expand Up @@ -180,6 +181,11 @@ def test_sanitize_for_serialization_datetime(self):
result = self.api_client.sanitize_for_serialization(data)
self.assertEqual(result, "1997-07-16T19:20:30.450000+01:00")

def test_sanitize_for_serialization_decimal(self):
data = Decimal("1.0")
result = self.api_client.sanitize_for_serialization(data)
self.assertEquals(result, "1.0")

def test_sanitize_for_serialization_list_enum(self):
class EnumSerialization(int, Enum):
NUMBER_0 = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import time
import unittest
import datetime
from decimal import Decimal

import pytest as pytest

Expand Down Expand Up @@ -130,6 +131,15 @@ def test_deserialize_datetime(self):
deserialized = self.deserialize(response, "datetime", 'application/json')
self.assertTrue(isinstance(deserialized, datetime.datetime))

def test_deserialize_decimal(self):
""" deserialize decimal """
data = 1.1
response = json.dumps(data)

deserialized = self.deserialize(response, "decimal", 'application/json')
self.assertTrue(isinstance(deserialized, Decimal))
self.assertEqual(deserialized, Decimal(1.1))

def test_deserialize_pet(self):
""" deserialize pet """
data = {
Expand Down
Loading