Skip to content

Commit ea51f51

Browse files
author
Julien Barreau
authored
fix(create): bad type for data when setting a many to one (#330)
1 parent c0e5ded commit ea51f51

2 files changed

Lines changed: 47 additions & 4 deletions

File tree

  • src/agent_toolkit

src/agent_toolkit/forestadmin/agent_toolkit/resources/collections/crud.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -421,10 +421,6 @@ async def extract_data(
421421
value = await CollectionUtils.get_value(
422422
caller, cast(Collection, foreign_collection), [value], field["foreign_key_target"]
423423
)
424-
try:
425-
value = int(value)
426-
except ValueError:
427-
pass
428424
record[field["foreign_key"]] = value
429425

430426
return record, one_to_one_relations

src/agent_toolkit/tests/resources/collections/test_crud.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,53 @@ def test_add_with_uuid_should_work_with_uuid_as_obj_or_str(self):
10751075
],
10761076
)
10771077

1078+
def test_add_should_not_change_json_api_deserialization_data(self):
1079+
request = RequestCollection(
1080+
RequestMethod.POST,
1081+
self.collection_book,
1082+
body={
1083+
"data": {
1084+
"attributes": {
1085+
"name": "Foundation",
1086+
},
1087+
"relationships": {
1088+
"author": {"data": {"type": "author", "id": "123e4567-e89b-12d3-a456-426614174000"}}
1089+
},
1090+
},
1091+
"type": "book",
1092+
},
1093+
query={
1094+
"collection_name": "book",
1095+
"timezone": "Europe/Paris",
1096+
},
1097+
headers={},
1098+
client_ip="127.0.0.1",
1099+
)
1100+
crud_resource = CrudResource(
1101+
self.datasource_composite,
1102+
self.datasource,
1103+
self.permission_service,
1104+
self.ip_white_list_service,
1105+
self.options,
1106+
)
1107+
with patch.object(
1108+
self.collection_book,
1109+
"create",
1110+
new_callable=AsyncMock,
1111+
return_value=[
1112+
{
1113+
"name": "Foundation",
1114+
"id": "123e4567-e89b-12d3-a456-42661417400a",
1115+
"author_id": "123e4567-e89b-12d3-a456-426614174000",
1116+
}
1117+
],
1118+
) as mock_create:
1119+
self.loop.run_until_complete(crud_resource.add(request))
1120+
mock_create.assert_awaited_with(
1121+
FAKE_USER, [{"name": "Foundation", "author_id": UUID("123e4567-e89b-12d3-a456-426614174000")}]
1122+
)
1123+
mock_create.assert_awaited_once()
1124+
10781125
# list
10791126
def test_list(self):
10801127
mock_orders = [{"id": 10, "cost": 200}, {"id": 11, "cost": 201}]

0 commit comments

Comments
 (0)