Skip to content

A community-supported supercharged document management system: scan, index and archive all your documents

Fix: empty strings for consumption template fields should be treated as None (#4762)

shamoon285a4b5aef0a
diff --git a/src/documents/serialisers.py b/src/documents/serialisers.py
index 86d7f0e37..b75ca3418 100644
--- a/src/documents/serialisers.py
+++ b/src/documents/serialisers.py
@@ -1171,10 +1171,19 @@ class ConsumptionTemplateSerializer(serializers.ModelSerializer):
def validate(self, attrs):
if ("filter_mailrule") in attrs and attrs["filter_mailrule"] is not None:
attrs["sources"] = {DocumentSource.MailFetch.value}
+
+ # Empty strings treated as None to avoid unexpected behavior
+ if ("assign_title") in attrs and len(attrs["assign_title"]) == 0:
+ attrs["assign_title"] = None
+ if "filter_filename" in attrs and len(attrs["filter_filename"]) == 0:
+ attrs["filter_filename"] = None
+ if "filter_path" in attrs and len(attrs["filter_path"]) == 0:
+ attrs["filter_path"] = None
+
if (
- ("filter_mailrule" not in attrs)
- and ("filter_filename" not in attrs or len(attrs["filter_filename"]) == 0)
- and ("filter_path" not in attrs or len(attrs["filter_path"]) == 0)
+ "filter_mailrule" not in attrs
+ and ("filter_filename" not in attrs or attrs["filter_filename"] is None)
+ and ("filter_path" not in attrs or attrs["filter_path"] is None)
):
raise serializers.ValidationError(
"File name, path or mail rule filter are required",
diff --git a/src/documents/tests/test_api.py b/src/documents/tests/test_api.py
index b82dc8f10..2cda45e7f 100644
--- a/src/documents/tests/test_api.py
+++ b/src/documents/tests/test_api.py
@@ -5740,7 +5740,55 @@ class TestApiConsumptionTemplates(DirectoriesMixin, APITestCase):
content_type="application/json",
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
- self.assertEqual(StoragePath.objects.count(), 1)
+ self.assertEqual(ConsumptionTemplate.objects.count(), 1)
+
+ def test_api_create_consumption_template_empty_fields(self):
+ """
+ GIVEN:
+ - API request to create a consumption template
+ - Path or filename filter or assign title are empty string
+ WHEN:
+ - API is called
+ THEN:
+ - Template is created but filter or title assignment is not set if ""
+ """
+ response = self.client.post(
+ self.ENDPOINT,
+ json.dumps(
+ {
+ "name": "Template 2",
+ "order": 1,
+ "sources": [DocumentSource.ApiUpload],
+ "filter_filename": "*test*",
+ "filter_path": "",
+ "assign_title": "",
+ },
+ ),
+ content_type="application/json",
+ )
+ self.assertEqual(response.status_code, status.HTTP_201_CREATED)
+ ct = ConsumptionTemplate.objects.get(name="Template 2")
+ self.assertEqual(ct.filter_filename, "*test*")
+ self.assertIsNone(ct.filter_path)
+ self.assertIsNone(ct.assign_title)
+
+ response = self.client.post(
+ self.ENDPOINT,
+ json.dumps(
+ {
+ "name": "Template 3",
+ "order": 1,
+ "sources": [DocumentSource.ApiUpload],
+ "filter_filename": "",
+ "filter_path": "*/test/*",
+ },
+ ),
+ content_type="application/json",
+ )
+ self.assertEqual(response.status_code, status.HTTP_201_CREATED)
+ ct2 = ConsumptionTemplate.objects.get(name="Template 3")
+ self.assertEqual(ct2.filter_path, "*/test/*")
+ self.assertIsNone(ct2.filter_filename)
def test_api_create_consumption_template_with_mailrule(self):
"""