Skip to content

Commit 81ab071

Browse files
fix: isList check in Value checks type of list (#70)
* isList check of Value checks type of list Signed-off-by: Robert Grassian <[email protected]> * test for empty list Signed-off-by: Robert Grassian <[email protected]> Signed-off-by: Robert Grassian <[email protected]>
1 parent 00af2f8 commit 81ab071

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/main/java/dev/openfeature/javasdk/Value.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,9 @@ public boolean isStructure() {
125125
* @return boolean
126126
*/
127127
public boolean isList() {
128-
return this.innerObject instanceof List;
128+
return this.innerObject instanceof List
129+
&& (((List) this.innerObject).isEmpty()
130+
|| ((List) this.innerObject).get(0) instanceof Value);
129131
}
130132

131133
/**

src/test/java/dev/openfeature/javasdk/ValueTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,28 @@ class Something {}
110110
assertTrue(value.isList());
111111
assertEquals(ITEM_VALUE, value.asList().get(0).asString());
112112
}
113+
114+
@Test public void listMustBeOfValues() {
115+
String item = "item";
116+
List<String> list = new ArrayList<>();
117+
list.add(item);
118+
try {
119+
new Value((Object) list);
120+
fail("Should fail due to creation of list of non-values.");
121+
} catch (InstantiationException e) {
122+
assertEquals("Invalid value type: class java.util.ArrayList", e.getMessage());
123+
}
124+
}
125+
126+
@Test public void emptyListAllowed() {
127+
List<String> list = new ArrayList<>();
128+
try {
129+
Value value = new Value((Object) list);
130+
assertTrue(value.isList());
131+
List<Value> values = value.asList();
132+
assertTrue(values.isEmpty());
133+
} catch (Exception e) {
134+
fail("Unexpected exception occurred.", e);
135+
}
136+
}
113137
}

0 commit comments

Comments
 (0)