From ab75c7d4ace0fc4736c4e66ee4b7e9575e691d77 Mon Sep 17 00:00:00 2001 From: Anna Shamray Date: Tue, 29 Oct 2024 10:47:18 +0100 Subject: [PATCH] :bug: [#466] fix validation for merge PATCH --- src/objects/api/serializers.py | 4 ++++ src/objects/api/validators.py | 5 ++++- src/objects/tests/v2/test_object_api.py | 9 ++++++--- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/objects/api/serializers.py b/src/objects/api/serializers.py index f6221636..750d1dfe 100644 --- a/src/objects/api/serializers.py +++ b/src/objects/api/serializers.py @@ -131,6 +131,10 @@ def update(self, instance, validated_data): # version should be set if "version" not in validated_data: validated_data["version"] = instance.version + # start_at should be set + if "start_at" not in validated_data: + validated_data["start_at"] = instance.start_at + if self.partial and "data" in validated_data: # Apply JSON Merge Patch for record data validated_data["data"] = merge_patch(instance.data, validated_data["data"]) diff --git a/src/objects/api/validators.py b/src/objects/api/validators.py index 50c2cd2c..11b86567 100644 --- a/src/objects/api/validators.py +++ b/src/objects/api/validators.py @@ -9,7 +9,7 @@ from objects.core.utils import check_objecttype from .constants import Operators -from .utils import string_to_value +from .utils import merge_patch, string_to_value class JsonSchemaValidator: @@ -34,6 +34,9 @@ def __call__(self, attrs, serializer): ) version = attrs.get("version") if "version" in attrs else instance.version data = attrs.get("data", {}) if "data" in attrs else instance.data + if serializer.partial and "data" in attrs: + # Apply JSON Merge Patch for record data + data = merge_patch(instance.data, attrs["data"]) if not object_type or not version: return diff --git a/src/objects/tests/v2/test_object_api.py b/src/objects/tests/v2/test_object_api.py index 1c2cd2f4..ba572946 100644 --- a/src/objects/tests/v2/test_object_api.py +++ b/src/objects/tests/v2/test_object_api.py @@ -361,13 +361,16 @@ def test_patch_validates_merged_object_rather_than_partial_object(self, m): response = self.client.patch(url, data, **GEO_WRITE_KWARGS) self.assertEqual(response.status_code, status.HTTP_200_OK) - - initial_record.refresh_from_db() self.assertEqual( - initial_record.data, + response.json()["record"]["data"], {"plantDate": "2024-10-09", "diameter": 20, "name": "Name"}, ) + last_record = initial_record.object.last_record + self.assertEqual( + last_record.data, + {"plantDate": "2024-10-09", "diameter": 20, "name": "Name"}, + ) def test_delete_object(self, m): record = ObjectRecordFactory.create(object__object_type=self.object_type)