Skip to content

Collection Membership operator can return unintuitive results if collection has mix of types #1240

Description

@davidding

Since the CollectionMembershipOperator coerces the type of the item being tested to the first (or first non-null) element in the list, it can lead to unexpected/unintuitive results.

for (Object value : collection) {
if (value == null) {
if (o1 == null) {
return Boolean.TRUE;
}
} else {
try {
return collection.contains(converter.convert(o1, value.getClass()));
} catch (ELException e) {
return Boolean.FALSE;
}
}
}
}

{{ null in [null, 1, 2] }} {# true #}
{{ null in [1, null, 2] }} {# false - null converted to 0; no 0 in list #}
{{ null in [1, null, 2, 0] }} {# true - because of the 0 in the list, not the null #}
{{ null in [0, 1, 2] }} {# true #}

{{ 1 in ['1', 2 ] }} {# true - 1 converted to '1' #}
{{ 2 in ['1', 2 ] }} {# false - 2 is converted to '2', no '2' in list #}
{{ 1 in [false, 1] }} {# false - 1 converted to true #}
{{ 1 in [true, 0] }} {# true #}
{{ 1 in [null, 'abc', 1] }} {# false #}

{{ '' in [1, '', 2] }} {# false #}
{{ '' in [0, 1, 2] }} {# true #}

(The same applies to is containing, e.g. [false, 1] is containing 1 is false)

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