Skip to content

Commit b3ec1ff

Browse files
committed
serde supports datetime.datetime type.
1 parent 29ca24b commit b3ec1ff

File tree

2 files changed

+151
-1
lines changed

2 files changed

+151
-1
lines changed

alibabacloud_oss_v2/serde.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ def _deserialize_xml_any(upper_obj: Model, elem: ET.Element, attr_types: List[st
399399
return int(elem.text)
400400
if attr_type == 'float':
401401
return float(elem.text)
402-
if attr_type == 'datetime':
402+
if 'datetime' in attr_type:
403403
return _deserialize_datetime(elem.text, attr_types)
404404

405405
raise exceptions.DeserializationError(error=f'Unsupport type {attr_type}')

tests/unit/test_serde.py

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,6 +1401,91 @@ def __init__(
14011401
self.assertEqual(datetime_utc, result.config.httptime_xml)
14021402
self.assertEqual(datetime_utc, result.config.unixtime_xml)
14031403

1404+
def test_deserialize_response_outline_body_xml_new_datatime_type(self):
1405+
1406+
class Configuration(serde.Model):
1407+
def __init__(
1408+
self,
1409+
str_xml: Optional[str] = None,
1410+
int_xml: Optional[int] = None,
1411+
bool_xml: Optional[bool] = None,
1412+
float_xml: Optional[float] = None,
1413+
isotime_xml: Optional[datetime.datetime] = None,
1414+
httptime_xml: Optional[datetime.datetime] = None,
1415+
unixtime_xml: Optional[datetime.datetime] = None,
1416+
**kwargs: Any
1417+
) -> None:
1418+
super().__init__(**kwargs)
1419+
self.str_xml = str_xml
1420+
self.int_xml = int_xml
1421+
self.bool_xml = bool_xml
1422+
self.float_xml = float_xml
1423+
self.isotime_xml = isotime_xml
1424+
self.httptime_xml = httptime_xml
1425+
self.unixtime_xml = unixtime_xml
1426+
1427+
_attribute_map = {
1428+
"str_xml": {"tag": "xml", "rename": "StrField"},
1429+
"int_xml": {"tag": "xml", "rename": "IntField", "type":"int"},
1430+
"bool_xml": {"tag": "xml", "rename": "BoolField", "type":"bool"},
1431+
"float_xml": {"tag": "xml", "rename": "FloatField", "type":"float"},
1432+
"isotime_xml": {"tag": "xml", "rename": "IsotimeField", "type":"datetime.datetime"},
1433+
"httptime_xml": {"tag": "xml", "rename": "HttptimeField", "type":"datetime.datetime,httptime"},
1434+
"unixtime_xml": {"tag": "xml", "rename": "UnixtimeField", "type":"datetime.datetime,unixtime"},
1435+
}
1436+
1437+
_xml_map = {'name':'Configuration'}
1438+
1439+
1440+
class PutApiResult(serde.ResultModel):
1441+
def __init__(
1442+
self,
1443+
config: Optional[Configuration] = None,
1444+
**kwargs: Any
1445+
) -> None:
1446+
super().__init__(**kwargs)
1447+
self.config = config
1448+
1449+
_attribute_map = {
1450+
"config": {"tag": "output", "position":"body", "type":"Configuration,xml"},
1451+
}
1452+
_dependency_map = {
1453+
"Configuration": {"new": lambda:Configuration()},
1454+
}
1455+
1456+
xml_data = r'''
1457+
<Configuration>
1458+
<StrField>str-1</StrField>
1459+
<IntField>1234</IntField>
1460+
<BoolField>true</BoolField>
1461+
<FloatField>3.5</FloatField>
1462+
<IsotimeField>2023-12-17T03:30:09.000000Z</IsotimeField>
1463+
<HttptimeField>Sun, 17 Dec 2023 03:30:09 GMT</HttptimeField>
1464+
<UnixtimeField>1702783809</UnixtimeField>
1465+
</Configuration>
1466+
'''
1467+
1468+
result = PutApiResult()
1469+
op_output = OperationOutput(
1470+
status='OK',
1471+
status_code=200,
1472+
headers= {},
1473+
http_response=HttpResponseStub(data=xml_data)
1474+
)
1475+
datetime_utc = datetime.datetime.fromtimestamp(1702783809, tz=datetime.timezone.utc)
1476+
deserializer = [serde.deserialize_output_xmlbody]
1477+
serde.deserialize_output(result, op_output, custom_deserializer=deserializer)
1478+
self.assertEqual('OK', result.status)
1479+
self.assertEqual(200, result.status_code)
1480+
self.assertEqual('', result.request_id)
1481+
self.assertEqual('str-1', result.config.str_xml)
1482+
self.assertEqual(1234, result.config.int_xml)
1483+
self.assertEqual(True, result.config.bool_xml)
1484+
self.assertEqual(3.5, result.config.float_xml)
1485+
self.assertEqual(datetime_utc, result.config.isotime_xml)
1486+
self.assertEqual(datetime_utc, result.config.httptime_xml)
1487+
self.assertEqual(datetime_utc, result.config.unixtime_xml)
1488+
14041489

14051490
def test_deserialize_response_header(self):
14061491
class PutApiResult(serde.ResultModel):
@@ -1467,6 +1552,71 @@ def __init__(
14671552
self.assertEqual(datetime_utc, result.httptime_header)
14681553
self.assertEqual(datetime_utc, result.unixtime_header)
14691554

1555+
def test_deserialize_response_header_new_datetime_type(self):
1556+
class PutApiResult(serde.ResultModel):
1557+
def __init__(
1558+
self,
1559+
str_header: Optional[str] = None,
1560+
int_header: Optional[int] = None,
1561+
bool_header: Optional[bool] = None,
1562+
float_header: Optional[float] = None,
1563+
isotime_header: Optional[datetime.datetime] = None,
1564+
httptime_header: Optional[datetime.datetime] = None,
1565+
unixtime_header: Optional[datetime.datetime] = None,
1566+
**kwargs: Any
1567+
) -> None:
1568+
super().__init__(**kwargs)
1569+
self.str_header = str_header
1570+
self.int_header = int_header
1571+
self.bool_header = bool_header
1572+
self.float_header = float_header
1573+
self.isotime_header = isotime_header
1574+
self.httptime_header = httptime_header
1575+
self.unixtime_header = unixtime_header
1576+
1577+
_attribute_map = {
1578+
"str_header": {"tag": "output", "position": "header", "rename": "x-oss-str"},
1579+
"int_header": {"tag": "output", "position": "header", "rename": "x-oss-int", "type":"int"},
1580+
"bool_header": {"tag": "output", "position": "header", "rename": "x-oss-bool", "type":"bool"},
1581+
"float_header": {"tag": "output", "position": "header", "rename": "x-oss-float", "type":"float"},
1582+
"isotime_header": {"tag": "output", "position": "header", "rename": "x-oss-isotime", "type":"datetime.datetime"},
1583+
"httptime_header": {"tag": "output", "position": "header", "rename": "x-oss-httptime", "type":"datetime.datetime,httptime"},
1584+
"unixtime_header": {"tag": "output", "position": "header", "rename": "x-oss-unixtime", "type":"datetime.datetime,unixtime"},
1585+
}
1586+
1587+
1588+
result = PutApiResult()
1589+
1590+
headers = CaseInsensitiveDict({
1591+
'x-oss-str':'str-1',
1592+
'x-oss-int':'123',
1593+
'x-oss-bool':'false',
1594+
'x-oss-float':'3.5',
1595+
'x-oss-isotime':'2023-12-17T03:30:09.000000Z',
1596+
'x-oss-httptime':'Sun, 17 Dec 2023 03:30:09 GMT',
1597+
'x-oss-unixtime':'1702783809',
1598+
'x-oss-request-id':'id-12345'
1599+
})
1600+
op_output = OperationOutput(
1601+
status='OK',
1602+
status_code=200,
1603+
headers= headers,
1604+
)
1605+
datetime_utc = datetime.datetime.fromtimestamp(1702783809, tz=datetime.timezone.utc)
1606+
deserializer = [serde.deserialize_output_headers]
1607+
serde.deserialize_output(result, op_output, custom_deserializer=deserializer)
1608+
self.assertEqual('OK', result.status)
1609+
self.assertEqual(200, result.status_code)
1610+
self.assertEqual('id-12345', result.request_id)
1611+
self.assertEqual(8, len(result.headers.items()))
1612+
self.assertEqual('str-1', result.str_header)
1613+
self.assertEqual(123, result.int_header)
1614+
self.assertEqual(False, result.bool_header)
1615+
self.assertEqual(3.5, result.float_header)
1616+
self.assertEqual(datetime_utc, result.isotime_header)
1617+
self.assertEqual(datetime_utc, result.httptime_header)
1618+
self.assertEqual(datetime_utc, result.unixtime_header)
1619+
14701620
def test_deserialize_response_body_and_header(self):
14711621
class PutApiResult(serde.ResultModel):
14721622
def __init__(

0 commit comments

Comments
 (0)