This is an automated email from the ASF dual-hosted git repository.

pdallig pushed a commit to branch branch-0.12
in repository https://gitbox.apache.org/repos/asf/zeppelin.git


The following commit(s) were added to refs/heads/branch-0.12 by this push:
     new 4ba0f21901 [ZEPPELIN-6244] Handle Collection as defaultValue in 
GUI.select()
4ba0f21901 is described below

commit 4ba0f21901ad8cc18b77c9a8dc4bb353b634f1aa
Author: renechoi <115696395+renec...@users.noreply.github.com>
AuthorDate: Fri Aug 15 15:31:35 2025 +0900

    [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.dal...@gmail.com>
    (cherry picked from commit 0da4f8d93d81b41f10bc7f7c54f56f7272c5aa29)
    Signed-off-by: Philipp Dallig <philipp.dal...@gmail.com>
---
 .../main/java/org/apache/zeppelin/display/GUI.java | 10 ++++++
 .../java/org/apache/zeppelin/display/GUITest.java  | 42 ++++++++++++++++++++++
 2 files changed, 52 insertions(+)

diff --git 
a/zeppelin-interpreter/src/main/java/org/apache/zeppelin/display/GUI.java 
b/zeppelin-interpreter/src/main/java/org/apache/zeppelin/display/GUI.java
index 2e703d241d..2672a5707e 100644
--- a/zeppelin-interpreter/src/main/java/org/apache/zeppelin/display/GUI.java
+++ b/zeppelin-interpreter/src/main/java/org/apache/zeppelin/display/GUI.java
@@ -96,7 +96,17 @@ public class GUI implements Serializable {
     return params.get(id);
   }
 
+  /**
+   * Create a select form.
+   *
+   * <p>If {@code defaultValue} is a {@link java.util.Collection} the first
+   * element will be used as the actual default value.</p>
+   */
   public Object select(String id, ParamOption[] options, Object defaultValue) {
+    if (defaultValue instanceof Collection) {
+      Collection<?> values = (Collection<?>) defaultValue;
+      defaultValue = values.isEmpty() ? null : values.iterator().next();
+    }
     if (defaultValue == null && options != null && options.length > 0) {
       defaultValue = options[0].getValue();
     }
diff --git 
a/zeppelin-interpreter/src/test/java/org/apache/zeppelin/display/GUITest.java 
b/zeppelin-interpreter/src/test/java/org/apache/zeppelin/display/GUITest.java
index e4e67e85b0..0ab56b7a13 100644
--- 
a/zeppelin-interpreter/src/test/java/org/apache/zeppelin/display/GUITest.java
+++ 
b/zeppelin-interpreter/src/test/java/org/apache/zeppelin/display/GUITest.java
@@ -30,6 +30,8 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
 import java.util.List;
 
 
@@ -64,6 +66,46 @@ class GUITest {
     assertEquals("2", selected);
   }
 
+  @Test
+  void testSelectWithCollectionDefault() {
+    GUI gui = new GUI();
+    List<String> collectionDefault = Arrays.asList("2");
+    Object selected = gui.select("list_collection", options, 
collectionDefault);
+    
+    // Verify that "2" (first element of collection) is used as default
+    assertEquals("2", selected);
+    
+    // Verify the form's default value is correctly set
+    Select selectForm = (Select) gui.forms.get("list_collection");
+    assertEquals("2", selectForm.getDefaultValue());
+  }
+
+  @Test
+  void testSelectWithCollectionDefaultMultiElement() {
+    GUI gui = new GUI();
+    List<String> collectionDefault = Arrays.asList("2", "3");
+    Object selected = gui.select("list_collection_multi", options, 
collectionDefault);
+
+    // First element of collection should be used
+    assertEquals("2", selected);
+
+    Select selectForm = (Select) gui.forms.get("list_collection_multi");
+    assertEquals("2", selectForm.getDefaultValue());
+  }
+
+  @Test
+  void testSelectWithEmptyCollectionDefault() {
+    GUI gui = new GUI();
+    List<String> emptyDefault = Collections.emptyList();
+    Object selected = gui.select("list_collection_empty", options, 
emptyDefault);
+
+    // Empty collection -> null -> fallback to first option ("1")
+    assertEquals("1", selected);
+
+    Select selectForm = (Select) gui.forms.get("list_collection_empty");
+    assertEquals("1", selectForm.getDefaultValue());
+  }
+
   @Test
   void testGson() {
     GUI gui = new GUI();

Reply via email to