fix: validate list content to be values (#350)

validate list content

Signed-off-by: Kavindu Dodanduwa <kavindudodanduwa@gmail.com>
Co-authored-by: Justin Abrahms <jabrahms@ebay.com>
This commit is contained in:
Kavindu Dodanduwa 2023-03-22 09:46:53 -07:00 committed by GitHub
parent 77890303c3
commit d8e7d9e10c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 4 deletions

View File

@ -125,14 +125,27 @@ public class Value implements Cloneable {
}
/**
* 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;
}
/**

View File

@ -134,4 +134,12 @@ public class ValueTest {
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));
}
}