Skip to content

SpEL: 'select last' operator for maps has inconsistent behavior [SPR-7323] #11981

@spring-projects-issues

Description

@spring-projects-issues

Sam Brannen opened SPR-7323 and commented

The inconsistent behavior is due to improper tracking of the lastKey for maps in Selection.getValueInternal(ExpressionState).

The following can be used to reproduce the bug:

class MapTestBean {

	private final Map<String, String> colors = new TreeMap<String, String>();

	MapTestBean() {
		colors.put("red", "rot");
		colors.put("brown", "braun");
		colors.put("blue", "blau");
		colors.put("yellow", "gelb");
		colors.put("beige", "beige");
	}

	public Map<String, String> getColors() {
		return colors;
	}
}
EvaluationContext context = new StandardEvaluationContext(new MapTestBean());
ExpressionParser parser = new SpelExpressionParser();

Expression exp = parser.parseExpression("colors.$[key.startsWith('b')]");
Map<String, String> colorsMap = (Map<String, String>) exp.getValue(context);

assertEquals(1, colorsMap.size());
assertEquals("brown", colorsMap.keySet().iterator().next());

The above test currently fails with:

org.junit.ComparisonFailure: expected:<[brown]> but was:<[yellow]>

Affects: 3.0.3

Referenced from: commits 8bd0633, db7531b

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