Skip to content

Commit

Permalink
fix: isList check in Value checks type of list (#70)
Browse files Browse the repository at this point in the history
* isList check of Value checks type of list

Signed-off-by: Robert Grassian <robert.grassian@split.io>

* test for empty list

Signed-off-by: Robert Grassian <robert.grassian@split.io>

Signed-off-by: Robert Grassian <robert.grassian@split.io>
  • Loading branch information
rgrassian-split authored Sep 13, 2022
1 parent 00af2f8 commit 81ab071
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/main/java/dev/openfeature/javasdk/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/dev/openfeature/javasdk/ValueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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<String> list = new ArrayList<>();
try {
Value value = new Value((Object) list);
assertTrue(value.isList());
List<Value> values = value.asList();
assertTrue(values.isEmpty());
} catch (Exception e) {
fail("Unexpected exception occurred.", e);
}
}
}

0 comments on commit 81ab071

Please sign in to comment.