From 9ffa2370213ed6bf964c7206d61a98d000b5ea80 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Sun, 9 Aug 2026 19:32:59 -0700 Subject: [PATCH] Fix: dont allow empty string in wf assign cf value to overwrite --- src/documents/tests/test_workflows.py | 49 +++++++++++++++++++++++++++ src/documents/workflows/mutations.py | 3 +- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/documents/tests/test_workflows.py b/src/documents/tests/test_workflows.py index f3dace48f..574cecf4f 100644 --- a/src/documents/tests/test_workflows.py +++ b/src/documents/tests/test_workflows.py @@ -2000,6 +2000,55 @@ class TestWorkflows( r"Doc added in \w{3,}", ) # Match any 3-letter month name + def test_document_updated_workflow_existing_custom_field_empty_value(self) -> None: + """ + GIVEN: + - Existing workflow with UPDATED trigger and action that assigns a custom field + with an empty value + WHEN: + - Document is updated that already contains the field with a value + THEN: + - The existing value is left untouched, see GH #13627 + """ + trigger = WorkflowTrigger.objects.create( + type=WorkflowTrigger.WorkflowTriggerType.DOCUMENT_UPDATED, + filter_has_document_type=self.dt, + ) + action = WorkflowAction.objects.create() + action.assign_custom_fields.add(self.cf1) + action.assign_custom_fields_values = {self.cf1.pk: ""} + action.save() + w = Workflow.objects.create( + name="Workflow 1", + order=0, + ) + w.triggers.add(trigger) + w.actions.add(action) + w.save() + + doc = Document.objects.create( + title="sample test", + correspondent=self.c, + original_filename="sample.pdf", + ) + CustomFieldInstance.objects.create( + document=doc, + field=self.cf1, + value_text="existing value", + ) + + superuser = User.objects.create_superuser("superuser") + self.client.force_authenticate(user=superuser) + + self.client.patch( + f"/api/documents/{doc.id}/", + {"document_type": self.dt.id}, + format="json", + ) + + doc.refresh_from_db() + self.assertEqual(doc.custom_fields.get(field=self.cf1).value, "existing value") + def test_document_updated_workflow_existing_custom_field(self) -> None: """ GIVEN: diff --git a/src/documents/workflows/mutations.py b/src/documents/workflows/mutations.py index 7d33bc545..869ea240f 100644 --- a/src/documents/workflows/mutations.py +++ b/src/documents/workflows/mutations.py @@ -105,7 +105,8 @@ def apply_assignment_to_document( field=field, document=document, ).first() - if instance and args[value_field_name] is not None: + # empty string is indistinguishable from no value in the UI + if instance and args[value_field_name] not in (None, ""): setattr(instance, value_field_name, args[value_field_name]) instance.save() elif not instance: