Skip to content

Commit 0da4f8d

Browse files
authored
[ZEPPELIN-6244] Handle Collection as defaultValue in GUI.select()
# [MINOR] Handle Collection as defaultValue in GUI.select() ## What is this PR for? This PR improves the `GUI.select()` method to properly handle Collection objects passed as defaultValue. When a Collection is provided, the method now extracts the first element as the actual default value instead of using the Collection object itself. ## What type of PR is it? Improvement ## What is the Jira issue? N/A (Minor improvement) ## How should this be tested? 1. Run the test: `./mvnw test -pl zeppelin-interpreter -Dtest=GUITest` 2. Verify that the new test `testSelectWithCollectionDefault()` passes 3. The test verifies that: - When a Collection containing `["2"]` is passed as defaultValue - The select form uses `"2"` as the default value (not the Collection object) ## Questions: * Does the license files need update? No * Is there breaking changes for older versions? No * Does this needs documentation? No ## Description ### Problem Currently, when using `GUI.select()` with a Collection as the defaultValue parameter, the entire Collection object is used as the default value. This can cause issues since the select form expects a single value, not a Collection. ### Solution Added logic to check if the defaultValue is a Collection (but not a String, since String implements CharSequence which is a Collection-like interface). If it is: - Extract the first element from the Collection - Use that element as the actual default value - If the Collection is empty, use null ### Code Changes ```java // Added to GUI.select() method if (defaultValue instanceof Collection && !(defaultValue instanceof String)) { Collection<?> values = (Collection<?>) defaultValue; defaultValue = values.isEmpty() ? null : values.iterator().next(); } ``` ### Test Coverage Added `testSelectWithCollectionDefault()` test that verifies: - A Collection containing `"2"` is properly converted to default value `"2"` - The form's default value is correctly set to `"2"` - The selected value matches the expected default This change improves the API's flexibility and prevents potential issues when Collections are inadvertently passed as default values. Closes #4973 from renechoi/fix-gui-select-collection-default. Signed-off-by: Philipp Dallig <philipp.dallig@gmail.com>
1 parent 4efa2a3 commit 0da4f8d

File tree

2 files changed

+52
-0
lines changed
  • zeppelin-interpreter/src

2 files changed

+52
-0
lines changed

zeppelin-interpreter/src/main/java/org/apache/zeppelin/display/GUI.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,17 @@ public Object password(String id) {
9696
return params.get(id);
9797
}
9898

99+
/**
100+
* Create a select form.
101+
*
102+
* <p>If {@code defaultValue} is a {@link java.util.Collection} the first
103+
* element will be used as the actual default value.</p>
104+
*/
99105
public Object select(String id, ParamOption[] options, Object defaultValue) {
106+
if (defaultValue instanceof Collection) {
107+
Collection<?> values = (Collection<?>) defaultValue;
108+
defaultValue = values.isEmpty() ? null : values.iterator().next();
109+
}
100110
if (defaultValue == null && options != null && options.length > 0) {
101111
defaultValue = options[0].getValue();
102112
}

zeppelin-interpreter/src/test/java/org/apache/zeppelin/display/GUITest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030

3131
import java.io.IOException;
3232
import java.util.ArrayList;
33+
import java.util.Arrays;
34+
import java.util.Collections;
3335
import java.util.List;
3436

3537

@@ -64,6 +66,46 @@ void testSelect() {
6466
assertEquals("2", selected);
6567
}
6668

69+
@Test
70+
void testSelectWithCollectionDefault() {
71+
GUI gui = new GUI();
72+
List<String> collectionDefault = Arrays.asList("2");
73+
Object selected = gui.select("list_collection", options, collectionDefault);
74+
75+
// Verify that "2" (first element of collection) is used as default
76+
assertEquals("2", selected);
77+
78+
// Verify the form's default value is correctly set
79+
Select selectForm = (Select) gui.forms.get("list_collection");
80+
assertEquals("2", selectForm.getDefaultValue());
81+
}
82+
83+
@Test
84+
void testSelectWithCollectionDefaultMultiElement() {
85+
GUI gui = new GUI();
86+
List<String> collectionDefault = Arrays.asList("2", "3");
87+
Object selected = gui.select("list_collection_multi", options, collectionDefault);
88+
89+
// First element of collection should be used
90+
assertEquals("2", selected);
91+
92+
Select selectForm = (Select) gui.forms.get("list_collection_multi");
93+
assertEquals("2", selectForm.getDefaultValue());
94+
}
95+
96+
@Test
97+
void testSelectWithEmptyCollectionDefault() {
98+
GUI gui = new GUI();
99+
List<String> emptyDefault = Collections.emptyList();
100+
Object selected = gui.select("list_collection_empty", options, emptyDefault);
101+
102+
// Empty collection -> null -> fallback to first option ("1")
103+
assertEquals("1", selected);
104+
105+
Select selectForm = (Select) gui.forms.get("list_collection_empty");
106+
assertEquals("1", selectForm.getDefaultValue());
107+
}
108+
67109
@Test
68110
void testGson() {
69111
GUI gui = new GUI();

0 commit comments

Comments
 (0)