Skip to content

Commit 877a5b6

Browse files
author
Julien Barreau
authored
fix(creation): create a record with relation only (#327)
1 parent 7f9194c commit 877a5b6

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

src/agent_toolkit/forestadmin/agent_toolkit/services/serializers/json_api_deserializer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def deserialize(self, data: DumpedResult, collection: Collection) -> RecordsData
2929
ret = {}
3030
data["data"] = cast(Data, data["data"])
3131

32-
for key, value in data["data"]["attributes"].items():
32+
for key, value in data["data"].get("attributes", {}).items():
3333
if key not in collection.schema["fields"]:
3434
raise JsonApiDeserializerException(f"Field {key} doesn't exists in collection {collection.name}.")
3535
ret[key] = self._deserialize_value(value, cast(Column, collection.schema["fields"][key]))

src/agent_toolkit/tests/services/serializers/test_jsonapi.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,24 @@ def test_should_correctly_load_one_to_one_relation(self):
799799
},
800800
)
801801

802+
def test_should_work_if_attribute_not_present(self):
803+
deserializer = JsonApiDeserializer(self.datasource)
804+
805+
request_body = {
806+
"data": {
807+
"id": "43661dae-97c3-4ea9-bd43-a6d8ac3f4ca7",
808+
"type": "Orders",
809+
"relationships": {"customer": {"data": {"id": "12", "type": "Persons"}}},
810+
}
811+
}
812+
data = deserializer.deserialize(request_body, self.collection_order)
813+
self.assertEqual(
814+
data,
815+
{
816+
"customer": 12,
817+
},
818+
)
819+
802820

803821
class TestJsonApiSerializer(TestJsonApi):
804822
@classmethod

0 commit comments

Comments
 (0)