Skip to content

Commit

Permalink
🐛 [#466] fix validation for merge PATCH
Browse files Browse the repository at this point in the history
  • Loading branch information
annashamray committed Oct 29, 2024
1 parent 5924ad5 commit ab75c7d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/objects/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down
5 changes: 4 additions & 1 deletion src/objects/api/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
9 changes: 6 additions & 3 deletions src/objects/tests/v2/test_object_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit ab75c7d

Please sign in to comment.