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 4 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 @@ -9,12 +9,11 @@
$ pytest
"""

import os
import time
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you revert this change? It has nothing to do with your original PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep yep

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import atexit
import weakref
import unittest
from dateutil.parser import parse
from decimal import Decimal

import petstore_api
import petstore_api.configuration
Expand Down Expand Up @@ -156,6 +155,11 @@ def test_sanitize_for_serialization(self):
result = self.api_client.sanitize_for_serialization(data)
self.assertEqual(result, "1997-07-16T19:20:30.450000+01:00")

# decimal
data = Decimal("1.0")
result = self.api_client.sanitize_for_serialization(data)
self.assertEquals(result, "1.0")

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, can you move this into samples/openapi3/client/petstore/python/tests/test_api_client.py instead?

The "pydantic v1" backend will be removed at some point.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh good catch, thats on me, fixing now

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@multani @wing328 do i need to worry about the failing steps in the build? They appear to be failures to install poetry for the v1 petstore sample.

Run pipx install --python '/opt/hostedtoolcache/Python/3.7.17/x64/bin/python' poetry
creating virtual environment...
installing poetry...
Fatal error from pip prevented installation. Full pip output in file:
    /opt/pipx/logs/cmd_2024-07-22_16.55.46_pip_errors.log

Some possibly relevant errors from pip install:
    TypeError: Expected maxsize to be an integer or None
Error installing poetry.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I can't retry, but I don't think it's related to your change :)

# list
data = [1]
result = self.api_client.sanitize_for_serialization(data)
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 @@ -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