Skip to content

Commit

Permalink
feat(ts): support Date and DateTime manual types (box/box-codegen#476)
Browse files Browse the repository at this point in the history
  • Loading branch information
box-sdk-build committed Apr 26, 2024
1 parent e289ade commit 374b69c
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .codegen.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{ "engineHash": "f0bd1ce", "specHash": "1698c95", "version": "0.6.4" }
{ "engineHash": "267eec8", "specHash": "1698c95", "version": "0.6.4" }
53 changes: 30 additions & 23 deletions box_sdk_gen/internal/base_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def to_dict(self) -> dict:

@classmethod
def _deserialize(cls, key, value, annotation=None):
if annotation is None:
if annotation is None or value is None:
return value
if get_origin(annotation) == Optional:
return cls._deserialize(key, value, get_args(annotation))
Expand All @@ -69,13 +69,15 @@ def _deserialize(cls, key, value, annotation=None):
return cls._deserialize_datetime(key, value, annotation)
elif annotation == date:
return cls._deserialize_date(key, value, annotation)
else:
elif isinstance(annotation, type) and issubclass(annotation, BaseObject):
return cls._deserialize_nested_type(key, value, annotation)
else:
return value

@classmethod
def _deserialize_list(cls, key, value, annotation: list):
list_type = get_args(annotation)[0]
try:
list_type = get_args(annotation)[0]
return [
cls._deserialize(key, list_entry, list_type) for list_entry in value
]
Expand All @@ -84,27 +86,32 @@ def _deserialize_list(cls, key, value, annotation: list):

@classmethod
def _deserialize_union(cls, key, value, annotation):
possible_types = get_args(annotation)
if value is None:
if type(None) not in possible_types:
print('Value: ', value, 'should not be allowed in Union:', annotation)
try:
possible_types = get_args(annotation)
if value is None:
if type(None) not in possible_types:
print(
'Value: ', value, 'should not be allowed in Union:', annotation
)
return value

for possible_type in possible_types:
if (
isinstance(possible_type, type)
and issubclass(possible_type, BaseObject)
and value.get(possible_type._discriminator[0], None)
in possible_type._discriminator[1]
):
return cls._deserialize(key, value, possible_type)

for possible_type in possible_types:
try:
return cls._deserialize(key, value, possible_type)
except Exception:
continue
return value
except Exception:
return value

for possible_type in possible_types:
if (
issubclass(possible_type, BaseObject)
and value.get(possible_type._discriminator[0], None)
in possible_type._discriminator[1]
):
return cls._deserialize(key, value, possible_type)

for possible_type in possible_types:
try:
return cls._deserialize(key, value, possible_type)
except Exception:
continue

return value

@classmethod
def _deserialize_enum(cls, key, value, annotation):
Expand Down
2 changes: 1 addition & 1 deletion docs/tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ See the endpoint docs at
<!-- sample post_tasks -->

```python
client.tasks.create_task(CreateTaskItem(type=CreateTaskItemTypeField.FILE.value, id=file.id), action=CreateTaskAction.REVIEW.value, message='test message', due_at=date, completion_rule=CreateTaskCompletionRule.ALL_ASSIGNEES.value)
client.tasks.create_task(CreateTaskItem(type=CreateTaskItemTypeField.FILE.value, id=file.id), action=CreateTaskAction.REVIEW.value, message='test message', due_at=date_time, completion_rule=CreateTaskCompletionRule.ALL_ASSIGNEES.value)
```

### Arguments
Expand Down
22 changes: 12 additions & 10 deletions test/legal_hold_policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from box_sdk_gen.schemas import LegalHoldPolicies

from box_sdk_gen.internal.utils import DateTime

from box_sdk_gen.internal.utils import get_uuid

from box_sdk_gen.internal.utils import date_time_from_string
Expand Down Expand Up @@ -47,23 +49,23 @@ def testCreateUpdateGetDeleteLegalHoldPolicy():
def testCreateNotOngoingLegalHoldPolicy():
legal_hold_policy_name: str = get_uuid()
legal_hold_description: str = 'test description'
filter_started_at: DateTime = date_time_from_string('2021-01-01T00:00:00-08:00')
filter_ended_at: DateTime = date_time_from_string('2022-01-01T00:00:00-08:00')
legal_hold_policy: LegalHoldPolicy = (
client.legal_hold_policies.create_legal_hold_policy(
legal_hold_policy_name,
description=legal_hold_description,
filter_started_at=date_time_from_string('2021-01-01T00:00:00-08:00'),
filter_ended_at=date_time_from_string('2022-01-01T00:00:00-08:00'),
filter_started_at=filter_started_at,
filter_ended_at=filter_ended_at,
is_ongoing=False,
)
)
assert legal_hold_policy.policy_name == legal_hold_policy_name
assert legal_hold_policy.description == legal_hold_description
assert (
date_time_to_string(legal_hold_policy.filter_started_at)
== '2021-01-01T00:00:00-08:00'
)
assert (
date_time_to_string(legal_hold_policy.filter_ended_at)
== '2022-01-01T00:00:00-08:00'
)
assert date_time_to_string(
legal_hold_policy.filter_started_at
) == date_time_to_string(filter_started_at)
assert date_time_to_string(
legal_hold_policy.filter_ended_at
) == date_time_to_string(filter_ended_at)
client.legal_hold_policies.delete_legal_hold_policy_by_id(legal_hold_policy.id)
6 changes: 3 additions & 3 deletions test/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@ def testCreateUpdateGetDeleteTask():
generate_byte_stream(10),
)
file: FileFull = files.entries[0]
date: DateTime = date_time_from_string('2035-01-01T00:00:00Z')
date_time: DateTime = date_time_from_string('2035-01-01T00:00:00Z')
task: Task = client.tasks.create_task(
CreateTaskItem(type=CreateTaskItemTypeField.FILE.value, id=file.id),
action=CreateTaskAction.REVIEW.value,
message='test message',
due_at=date,
due_at=date_time,
completion_rule=CreateTaskCompletionRule.ALL_ASSIGNEES.value,
)
assert task.message == 'test message'
assert task.item.id == file.id
assert date_time_to_string(task.due_at) == '2035-01-01T00:00:00Z'
assert date_time_to_string(task.due_at) == date_time_to_string(date_time)
task_by_id: Task = client.tasks.get_task_by_id(task.id)
assert task_by_id.id == task.id
task_on_file: Tasks = client.tasks.get_file_tasks(file.id)
Expand Down

0 comments on commit 374b69c

Please sign in to comment.