Skip to content

SpEL: Conversion of collection method parameters fails [SPR-7631] #12287

@spring-projects-issues

Description

@spring-projects-issues

Oliver Becker opened SPR-7631 and commented

In the SpEL when a method is invoked that has a method parameter of a generic collection type (say Collection<T>), the elements of the passed collection (say Collection<S>) will not be converted. (Note: this works fine for setting properties.)

Here is a test case:

public class CollectionToCollectionConverterTest {

public static class Foo {

    private Collection<Foo> foos;

    public final String value;

    public Foo(String value) {
        this.value = value;
    }

    public void setFoos(Collection<Foo> foos) {
        this.foos = foos;
    }

    public Collection<Foo> getFoos() {
        return this.foos;
    }

}

@Test
public void testConvert() {
    ExpressionParser parser = new SpelExpressionParser();
    Foo root = new Foo("bar");
    EvaluationContext context = new StandardEvaluationContext(root);

    Collection<String> foos = Collections.singletonList("baz");

    // property access, works
    Expression expression = parser.parseExpression("foos");
    expression.setValue(context, foos);
    Foo baz = root.getFoos().iterator().next();
    Assert.assertEquals("baz", baz.value);

    // method call, fails (ClassCastException)
    expression = parser.parseExpression("setFoos(#foos)");
    context.setVariable("foos", foos);
    expression.getValue(context);
    baz = root.getFoos().iterator().next();
    Assert.assertEquals("baz", baz.value);
}

}


Affects: 3.0.4

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions