Fix: do not accept empty string for doc link value via API (#8596)

This commit is contained in:
shamoon 2025-01-03 15:16:00 -08:00 committed by GitHub
parent 94a20b4510
commit 8e6de2790e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 0 deletions

View File

@ -668,6 +668,10 @@ class CustomFieldInstanceSerializer(serializers.ModelSerializer):
f"Value must be an id of an element in {select_options}",
)
elif field.data_type == CustomField.FieldDataType.DOCUMENTLINK:
if not (isinstance(data["value"], list) or data["value"] is None):
raise serializers.ValidationError(
"Value must be a list",
)
doc_ids = data["value"]
if Document.objects.filter(id__in=doc_ids).count() != len(
data["value"],

View File

@ -886,6 +886,7 @@ class TestCustomFieldsAPI(DirectoriesMixin, APITestCase):
- Document & custom field exist
WHEN:
- API request to set a field value to a document that does not exist
- API request to set a field value to empty string
THEN:
- HTTP 400 is returned
- No field instance is created or attached to the document
@ -916,6 +917,19 @@ class TestCustomFieldsAPI(DirectoriesMixin, APITestCase):
self.assertEqual(CustomFieldInstance.objects.count(), 0)
self.assertEqual(len(doc.custom_fields.all()), 0)
resp = self.client.patch(
f"/api/documents/{doc.id}/",
data={
"custom_fields": [
{"field": custom_field_documentlink.id, "value": ""},
],
},
format="json",
)
self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(CustomFieldInstance.objects.count(), 0)
def test_custom_field_not_null(self):
"""
GIVEN: