From 81ab0710ea56af65eb65c7f95832b8f58c559a51 Mon Sep 17 00:00:00 2001 From: Robert Grassian <89157164+rgrassian-split@users.noreply.github.com> Date: Tue, 13 Sep 2022 15:09:59 -0700 Subject: [PATCH] fix: isList check in Value checks type of list (#70) * isList check of Value checks type of list Signed-off-by: Robert Grassian * test for empty list Signed-off-by: Robert Grassian Signed-off-by: Robert Grassian --- .../java/dev/openfeature/javasdk/Value.java | 4 +++- .../dev/openfeature/javasdk/ValueTest.java | 24 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/main/java/dev/openfeature/javasdk/Value.java b/src/main/java/dev/openfeature/javasdk/Value.java index 859348ad4..abadd606f 100644 --- a/src/main/java/dev/openfeature/javasdk/Value.java +++ b/src/main/java/dev/openfeature/javasdk/Value.java @@ -125,7 +125,9 @@ public boolean isStructure() { * @return boolean */ public boolean isList() { - return this.innerObject instanceof List; + return this.innerObject instanceof List + && (((List) this.innerObject).isEmpty() + || ((List) this.innerObject).get(0) instanceof Value); } /** diff --git a/src/test/java/dev/openfeature/javasdk/ValueTest.java b/src/test/java/dev/openfeature/javasdk/ValueTest.java index 57cd6aacb..12210321c 100644 --- a/src/test/java/dev/openfeature/javasdk/ValueTest.java +++ b/src/test/java/dev/openfeature/javasdk/ValueTest.java @@ -110,4 +110,28 @@ class Something {} assertTrue(value.isList()); assertEquals(ITEM_VALUE, value.asList().get(0).asString()); } + + @Test public void listMustBeOfValues() { + String item = "item"; + List list = new ArrayList<>(); + list.add(item); + try { + new Value((Object) list); + fail("Should fail due to creation of list of non-values."); + } catch (InstantiationException e) { + assertEquals("Invalid value type: class java.util.ArrayList", e.getMessage()); + } + } + + @Test public void emptyListAllowed() { + List list = new ArrayList<>(); + try { + Value value = new Value((Object) list); + assertTrue(value.isList()); + List values = value.asList(); + assertTrue(values.isEmpty()); + } catch (Exception e) { + fail("Unexpected exception occurred.", e); + } + } }