Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix: treat empty default values for optional file inputs as unset (#28948)

fang lupinga84941197804
diff --git a/api/core/app/apps/base_app_generator.py b/api/core/app/apps/base_app_generator.py
index 1c6ca879252..1b0474142ed 100644
--- a/api/core/app/apps/base_app_generator.py
+++ b/api/core/app/apps/base_app_generator.py
@@ -99,6 +99,15 @@ class BaseAppGenerator:
if value is None:
return None
+ # Treat empty placeholders for optional file inputs as unset
+ if (
+ variable_entity.type in {VariableEntityType.FILE, VariableEntityType.FILE_LIST}
+ and not variable_entity.required
+ ):
+ # Treat empty string (frontend default) or empty list as unset
+ if not value and isinstance(value, (str, list)):
+ return None
+
if variable_entity.type in {
VariableEntityType.TEXT_INPUT,
VariableEntityType.SELECT,
diff --git a/api/tests/unit_tests/core/app/apps/test_base_app_generator.py b/api/tests/unit_tests/core/app/apps/test_base_app_generator.py
index fdab39f1332..d622c3a555c 100644
--- a/api/tests/unit_tests/core/app/apps/test_base_app_generator.py
+++ b/api/tests/unit_tests/core/app/apps/test_base_app_generator.py
@@ -265,3 +265,82 @@ def test_validate_inputs_with_default_value():
)
assert result == [{"id": "file1", "name": "doc1.pdf"}, {"id": "file2", "name": "doc2.pdf"}]
+
+
+def test_validate_inputs_optional_file_with_empty_string():
+ """Test that optional FILE variable with empty string returns None"""
+ base_app_generator = BaseAppGenerator()
+
+ var_file = VariableEntity(
+ variable="test_file",
+ label="test_file",
+ type=VariableEntityType.FILE,
+ required=False,
+ )
+
+ result = base_app_generator._validate_inputs(
+ variable_entity=var_file,
+ value="",
+ )
+
+ assert result is None
+
+
+def test_validate_inputs_optional_file_list_with_empty_list():
+ """Test that optional FILE_LIST variable with empty list returns None"""
+ base_app_generator = BaseAppGenerator()
+
+ var_file_list = VariableEntity(
+ variable="test_file_list",
+ label="test_file_list",
+ type=VariableEntityType.FILE_LIST,
+ required=False,
+ )
+
+ result = base_app_generator._validate_inputs(
+ variable_entity=var_file_list,
+ value=[],
+ )
+
+ assert result is None
+
+
+def test_validate_inputs_required_file_with_empty_string_fails():
+ """Test that required FILE variable with empty string still fails validation"""
+ base_app_generator = BaseAppGenerator()
+
+ var_file = VariableEntity(
+ variable="test_file",
+ label="test_file",
+ type=VariableEntityType.FILE,
+ required=True,
+ )
+
+ with pytest.raises(ValueError) as exc_info:
+ base_app_generator._validate_inputs(
+ variable_entity=var_file,
+ value="",
+ )
+
+ assert "must be a file" in str(exc_info.value)
+
+
+def test_validate_inputs_optional_file_with_empty_string_ignores_default():
+ """Test that optional FILE variable with empty string returns None, not the default"""
+ base_app_generator = BaseAppGenerator()
+
+ var_file = VariableEntity(
+ variable="test_file",
+ label="test_file",
+ type=VariableEntityType.FILE,
+ required=False,
+ default={"id": "file123", "name": "default.pdf"},
+ )
+
+ # When value is empty string (from frontend), should return None, not default
+ result = base_app_generator._validate_inputs(
+ variable_entity=var_file,
+ value="",
+ )
+
+ assert result is None