Skip to content

Commit

Permalink
fix: validate list content to be values (#350)
Browse files Browse the repository at this point in the history
validate list content

Signed-off-by: Kavindu Dodanduwa <[email protected]>
Co-authored-by: Justin Abrahms <[email protected]>
  • Loading branch information
Kavindu-Dodan and justinabrahms authored Mar 22, 2023
1 parent 7789030 commit d8e7d9e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/main/java/dev/openfeature/sdk/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,27 @@ public boolean isStructure() {
}

/**
* Check if this Value represents a List.
* Check if this Value represents a List of Values.
*
* @return boolean
*/
public boolean isList() {
return this.innerObject instanceof List
&& (((List) this.innerObject).isEmpty()
|| ((List) this.innerObject).get(0) instanceof Value);
if (!(this.innerObject instanceof List)) {
return false;
}

List<?> list = (List<?>) this.innerObject;
if (list.isEmpty()) {
return true;
}

for (Object obj : list) {
if (!(obj instanceof Value)) {
return false;
}
}

return true;
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/dev/openfeature/sdk/ValueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,12 @@ class Something {}
fail("Unexpected exception occurred.", e);
}
}

@Test public void valueConstructorValidateListInternals() {
List<Object> list = new ArrayList<>();
list.add(new Value("item"));
list.add("item");

assertThrows(InstantiationException.class, ()-> new Value(list));
}
}

0 comments on commit d8e7d9e

Please sign in to comment.