Fix: Dont allow null custom_fields property via API (#5063)

This commit is contained in:
shamoon 2023-12-19 13:53:56 -08:00 committed by GitHub
parent 638d9970fd
commit b1f6f52486
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 1 deletions

View File

@ -591,7 +591,11 @@ class DocumentSerializer(
archived_file_name = SerializerMethodField()
created_date = serializers.DateField(required=False)
custom_fields = CustomFieldInstanceSerializer(many=True, allow_null=True)
custom_fields = CustomFieldInstanceSerializer(
many=True,
allow_null=False,
required=False,
)
owner = serializers.PrimaryKeyRelatedField(
queryset=User.objects.all(),

View File

@ -400,6 +400,32 @@ class TestCustomField(DirectoriesMixin, APITestCase):
self.assertEqual(CustomFieldInstance.objects.count(), 0)
self.assertEqual(len(doc.custom_fields.all()), 0)
def test_custom_field_not_null(self):
"""
GIVEN:
- Existing document
WHEN:
- API request with custom_fields set to null
THEN:
- HTTP 400 is returned
"""
doc = Document.objects.create(
title="WOW",
content="the content",
checksum="123",
mime_type="application/pdf",
)
resp = self.client.patch(
f"/api/documents/{doc.id}/",
data={
"custom_fields": None,
},
format="json",
)
self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST)
def test_bidirectional_doclink_fields(self):
"""
GIVEN: