Skip to content

Commit

Permalink
cast create Entity request data to string
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvin-muchiri committed Aug 27, 2024
1 parent 2b7a611 commit d2466f0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
4 changes: 2 additions & 2 deletions onadata/apps/api/tests/viewsets/test_entity_list_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ def test_create_entity(self, mock_now):
"label": "30cm mora",
"geometry": "-1.286805 36.772845 0 0",
"species": "mora",
"circumference_cm": 30,
"circumference_cm": "30",
}
self.assertEqual(
response.data,
Expand Down Expand Up @@ -1157,7 +1157,7 @@ def test_updating_entity(self, mock_now):
expected_json = {
"geometry": "-1.286805 36.772845 0 0",
"species": "mora",
"circumference_cm": 30,
"circumference_cm": "30",
"label": "30cm mora",
}

Expand Down
5 changes: 4 additions & 1 deletion onadata/libs/serializers/entity_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ class EntitySerializer(serializers.ModelSerializer):
data = serializers.JSONField(write_only=True, required=False)

def validate_data(self, value):
"""Validate `data` field"""
"""Validate and cast `data` field values to strings"""
allowed_properties = set(self.context["entity_list"].properties)
invalid_properties = [
key for key in value.keys() if key not in allowed_properties
Expand All @@ -195,6 +195,9 @@ def validate_data(self, value):
)
)

# Cast all data field values to strings
value = {key: str(val) for key, val in value.items()}

return value

def validate(self, attrs):
Expand Down

0 comments on commit d2466f0

Please sign in to comment.