From ff3af47ef08ed64bfbfc7b6c23d1c0ff78ff0465 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Fri, 10 Jan 2014 12:16:03 +0000 Subject: [PATCH] Watch out for non-Strings in YAML map keys Fixes gh-205 --- .../boot/config/YamlProcessor.java | 16 +++++++++++++++- .../boot/config/YamlProcessorTests.java | 13 +++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/config/YamlProcessor.java b/spring-boot/src/main/java/org/springframework/boot/config/YamlProcessor.java index 0b7fd35116..a33a18dc9d 100644 --- a/spring-boot/src/main/java/org/springframework/boot/config/YamlProcessor.java +++ b/spring-boot/src/main/java/org/springframework/boot/config/YamlProcessor.java @@ -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 asMap(Object object) { - return (Map) object; + // YAML can have numbers as keys + Map result = new LinkedHashMap(); + Map map = (Map) object; + for (Entry 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 map, MatchCallback callback) { diff --git a/spring-boot/src/test/java/org/springframework/boot/config/YamlProcessorTests.java b/spring-boot/src/test/java/org/springframework/boot/config/YamlProcessorTests.java index b6a268b1cc..11638fa88e 100644 --- a/spring-boot/src/test/java/org/springframework/boot/config/YamlProcessorTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/config/YamlProcessorTests.java @@ -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 map) { + assertEquals("bar", properties.get("[1]")); + assertEquals(2, properties.size()); + } + }); + } + }