Skip to content

Value.isList accepts List<Object> where only the first element is a Value #286

Closed
@javicv

Description

@javicv

public boolean isList() {
return this.innerObject instanceof List
&& (((List) this.innerObject).isEmpty()
|| ((List) this.innerObject).get(0) instanceof Value);
}

This method isList() is used in constructor

public Value(Object value) throws InstantiationException {
but it's checking only the first element of the list. With this check a List<Object> can be added if only the first element is a Value while the other aren't.

In my opinion this test must pass, but it fails because the exception isn't thrown

@Test
public void listMustContainOnlyValues() {
    String item = "item";
    List<Object> list = new ArrayList<>();
    list.add(new Value(item));
    list.add(item);
    try {
        new Value((Object) list);
        fail("Should fail because list contains values and non-values and the exception should be thrown.");
    } catch (InstantiationException e) {
        assertEquals("Invalid value type: class java.util.ArrayList", e.getMessage());
    }
}

Maybe the constructor with Object parameter shouldn't accept lists to force the use of constructor with List<Value> where the list will only contain elements of type Value

public Value(List<Value> value) {
this.innerObject = value;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions