SPR-5847: require quotes for dotted map key names, eg. map['a.b.c']

This commit is contained in:
Andy Clement 2009-07-07 16:32:56 +00:00
parent a4b7ce168c
commit dbdac9fa31
2 changed files with 28 additions and 3 deletions

View File

@ -57,8 +57,15 @@ public class Indexer extends SpelNodeImpl {
index = reference.getName();
indexValue = new TypedValue(index,CommonTypeDescriptors.STRING_TYPE_DESCRIPTOR);
} else {
// In case the map key is unqualified, we want it evaluated against the root object so
// temporarily push that on whilst evaluating the key
try {
state.pushActiveContextObject(state.getRootContextObject());
indexValue = children[0].getValueInternal(state);
index = indexValue.getValue();
} finally {
state.popActiveContextObject();
}
}
// Indexing into a Map

View File

@ -152,17 +152,35 @@ public class SpringEL300Tests extends ExpressionTestCase {
name = expr.getValue(eContext,String.class);
Assert.assertEquals("Dave",name);
// MapAccessor required for this to work
expr = new SpelExpressionParser().parse("jdbcProperties.username");
eContext.addPropertyAccessor(new MapAccessor());
name = expr.getValue(eContext,String.class);
Assert.assertEquals("Dave",name);
// --- dotted property names
// lookup foo on the root, then bar on that, then use that as the key into jdbcProperties
expr = new SpelExpressionParser().parse("jdbcProperties[foo.bar]");
eContext.addPropertyAccessor(new MapAccessor());
name = expr.getValue(eContext,String.class);
Assert.assertEquals("Dave2",name);
// key is foo.bar
expr = new SpelExpressionParser().parse("jdbcProperties['foo.bar']");
eContext.addPropertyAccessor(new MapAccessor());
name = expr.getValue(eContext,String.class);
Assert.assertEquals("Elephant",name);
}
static class TestProperties {
public Properties jdbcProperties = new Properties();
public Properties foo = new Properties();
TestProperties() {
jdbcProperties.put("username","Dave");
jdbcProperties.put("foo.bar","Dave");
jdbcProperties.put("alias","Dave2");
jdbcProperties.put("foo.bar","Elephant");
foo.put("bar","alias");
}
}