From 1336751ab689414b1a9a5545e29bdc85370433aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=90=A7=E5=B2=B3?= Date: Wed, 5 Mar 2014 10:33:29 +0800 Subject: [PATCH] Handle non-String values in a Properties object The java docs for Properties say avoid this, but it is possible, so just silently skip any non-strings we find in the Properties. Fixes #147 --- .../config/impl/PropertiesParser.java | 26 ++++++++++--------- .../typesafe/config/impl/PropertiesTest.scala | 13 +++++++++- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/config/src/main/java/com/typesafe/config/impl/PropertiesParser.java b/config/src/main/java/com/typesafe/config/impl/PropertiesParser.java index 7c8c81fb..12b57171 100644 --- a/config/src/main/java/com/typesafe/config/impl/PropertiesParser.java +++ b/config/src/main/java/com/typesafe/config/impl/PropertiesParser.java @@ -134,20 +134,22 @@ final class PropertiesParser { /* Store string values in the associated scope maps */ for (Path path : valuePaths) { - Path parentPath = path.parent(); - Map parent = parentPath != null ? scopes - .get(parentPath) : root; - - String last = path.last(); Object rawValue = pathMap.get(path); - AbstractConfigValue value; - if (convertedFromProperties) { - value = new ConfigString(origin, (String) rawValue); - } else { - value = ConfigImpl.fromAnyRef(pathMap.get(path), origin, - FromMapMode.KEYS_ARE_PATHS); + if(!convertedFromProperties || rawValue instanceof String || rawValue instanceof Number || rawValue==null) { + Path parentPath = path.parent(); + Map parent = parentPath != null ? scopes + .get(parentPath) : root; + + String last = path.last(); + AbstractConfigValue value; + if (convertedFromProperties) { + value = new ConfigString(origin, rawValue.toString()); + } else { + value = ConfigImpl.fromAnyRef(pathMap.get(path), origin, + FromMapMode.KEYS_ARE_PATHS); + } + parent.put(last, value); } - parent.put(last, value); } /* diff --git a/config/src/test/scala/com/typesafe/config/impl/PropertiesTest.scala b/config/src/test/scala/com/typesafe/config/impl/PropertiesTest.scala index 582dc93a..75cfe0fe 100644 --- a/config/src/test/scala/com/typesafe/config/impl/PropertiesTest.scala +++ b/config/src/test/scala/com/typesafe/config/impl/PropertiesTest.scala @@ -5,7 +5,7 @@ package com.typesafe.config.impl import org.junit.Assert._ import org.junit._ -import java.util.Properties +import java.util.{ Date, Properties } import com.typesafe.config.Config import com.typesafe.config.ConfigParseOptions import com.typesafe.config.ConfigFactory @@ -184,4 +184,15 @@ class PropertiesTest extends TestUtils { assertEquals(Seq(-2, -1, 0, 1, 2, 3, 4, 5, 6), conf.getIntList("a").asScala.toSeq) conf.checkValid(reference) } + + @Test + def skipNonStringsInProperties() { + val props = new Properties() + props.put("a", new ThreadLocal[String]()) + props.put("b", new Date()) + + val conf = ConfigFactory.parseProperties(props) + + assertEquals(0, conf.root().size()) + } }