Watch out for non-Strings in YAML map keys

Fixes gh-205
pull/208/head
Dave Syer 11 years ago
parent bd73705393
commit ff3af47ef0

@ -19,6 +19,7 @@ package org.springframework.boot.config;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
@ -164,7 +165,20 @@ public class YamlProcessor {
@SuppressWarnings("unchecked")
private Map<String, Object> asMap(Object object) {
return (Map<String, Object>) object;
// YAML can have numbers as keys
Map<String, Object> result = new LinkedHashMap<String, Object>();
Map<Object, Object> map = (Map<Object, Object>) object;
for (Entry<Object, Object> entry : map.entrySet()) {
Object key = entry.getKey();
if (key instanceof CharSequence) {
result.put(key.toString(), entry.getValue());
}
else {
// It has to be a map key in this case
result.put("[" + key.toString() + "]", entry.getValue());
}
}
return result;
}
private boolean process(Map<String, Object> map, MatchCallback callback) {

@ -63,4 +63,17 @@ public class YamlProcessorTests {
});
}
@Test
public void integerKeyBehaves() {
this.processor.setResources(new Resource[] { new ByteArrayResource(
"foo: bar\n1: bar".getBytes()) });
this.processor.process(new MatchCallback() {
@Override
public void process(Properties properties, Map<String, Object> map) {
assertEquals("bar", properties.get("[1]"));
assertEquals(2, properties.size());
}
});
}
}

Loading…
Cancel
Save