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: incorrect array element validation in SegmentType (#23289)

Yongtao Huangbe914438a52e
diff --git a/api/core/variables/types.py b/api/core/variables/types.py
index e79b2410bfa..d28fb11401a 100644
--- a/api/core/variables/types.py
+++ b/api/core/variables/types.py
@@ -109,7 +109,7 @@ class SegmentType(StrEnum):
elif array_validation == ArrayValidation.FIRST:
return element_type.is_valid(value[0])
else:
- return all([element_type.is_valid(i, array_validation=ArrayValidation.NONE)] for i in value)
+ return all(element_type.is_valid(i, array_validation=ArrayValidation.NONE) for i in value)
def is_valid(self, value: Any, array_validation: ArrayValidation = ArrayValidation.FIRST) -> bool:
"""
@@ -152,7 +152,7 @@ class SegmentType(StrEnum):
_ARRAY_ELEMENT_TYPES_MAPPING: Mapping[SegmentType, SegmentType] = {
- # ARRAY_ANY does not have correpond element type.
+ # ARRAY_ANY does not have corresponding element type.
SegmentType.ARRAY_STRING: SegmentType.STRING,
SegmentType.ARRAY_NUMBER: SegmentType.NUMBER,
SegmentType.ARRAY_OBJECT: SegmentType.OBJECT,
diff --git a/api/tests/unit_tests/core/variables/test_segment_type.py b/api/tests/unit_tests/core/variables/test_segment_type.py
index 64d0d8c7e75..b33a83ba771 100644
--- a/api/tests/unit_tests/core/variables/test_segment_type.py
+++ b/api/tests/unit_tests/core/variables/test_segment_type.py
@@ -1,4 +1,4 @@
-from core.variables.types import SegmentType
+from core.variables.types import ArrayValidation, SegmentType
class TestSegmentTypeIsArrayType:
@@ -17,7 +17,6 @@ class TestSegmentTypeIsArrayType:
value is tested for the is_array_type method.
"""
# Arrange
- all_segment_types = set(SegmentType)
expected_array_types = [
SegmentType.ARRAY_ANY,
SegmentType.ARRAY_STRING,
@@ -58,3 +57,27 @@ class TestSegmentTypeIsArrayType:
for seg_type in enum_values:
is_array = seg_type.is_array_type()
assert isinstance(is_array, bool), f"is_array_type does not return a boolean for segment type {seg_type}"
+
+
+class TestSegmentTypeIsValidArrayValidation:
+ """
+ Test SegmentType.is_valid with array types using different validation strategies.
+ """
+
+ def test_array_validation_all_success(self):
+ value = ["hello", "world", "foo"]
+ assert SegmentType.ARRAY_STRING.is_valid(value, array_validation=ArrayValidation.ALL)
+
+ def test_array_validation_all_fail(self):
+ value = ["hello", 123, "world"]
+ # Should return False, since 123 is not a string
+ assert not SegmentType.ARRAY_STRING.is_valid(value, array_validation=ArrayValidation.ALL)
+
+ def test_array_validation_first(self):
+ value = ["hello", 123, None]
+ assert SegmentType.ARRAY_STRING.is_valid(value, array_validation=ArrayValidation.FIRST)
+
+ def test_array_validation_none(self):
+ value = [1, 2, 3]
+ # validation is None, skip
+ assert SegmentType.ARRAY_STRING.is_valid(value, array_validation=ArrayValidation.NONE)