diff --git a/config/src/main/java/com/typesafe/config/impl/ConfigBeanImpl.java b/config/src/main/java/com/typesafe/config/impl/ConfigBeanImpl.java index 9a021c8a..ed3eeeec 100644 --- a/config/src/main/java/com/typesafe/config/impl/ConfigBeanImpl.java +++ b/config/src/main/java/com/typesafe/config/impl/ConfigBeanImpl.java @@ -33,9 +33,17 @@ public class ConfigBeanImpl { Map configProps = new HashMap(); Map originalNames = new HashMap(); for (Map.Entry configProp : config.root().entrySet()) { - String camelName = ConfigImplUtil.toCamelCase(configProp.getKey()); - configProps.put(camelName, (AbstractConfigValue) configProp.getValue()); - originalNames.put(camelName, configProp.getKey()); + String originalName = configProp.getKey(); + String camelName = ConfigImplUtil.toCamelCase(originalName); + // if a setting is in there both as some hyphen name and the camel name, + // the camel one wins + if (originalNames.containsKey(camelName) && originalName != camelName) { + // if we aren't a camel name to start with, we lose. + // if we are or we are the first matching key, we win. + } else { + configProps.put(camelName, (AbstractConfigValue) configProp.getValue()); + originalNames.put(camelName, originalName); + } } BeanInfo beanInfo = null; diff --git a/config/src/test/java/beanconfig/PreferCamelNamesConfig.java b/config/src/test/java/beanconfig/PreferCamelNamesConfig.java new file mode 100644 index 00000000..94f17a84 --- /dev/null +++ b/config/src/test/java/beanconfig/PreferCamelNamesConfig.java @@ -0,0 +1,22 @@ +package beanconfig; + + +public class PreferCamelNamesConfig { + + + private String fooBar; + private String bazBar; + + public String getFooBar() { + return fooBar; + } + public void setFooBar(String v) { + this.fooBar = v; + } + public String getBazBar() { + return bazBar; + } + public void setBazBar(String v) { + this.bazBar = v; + } +} diff --git a/config/src/test/resources/beanconfig/beanconfig01.conf b/config/src/test/resources/beanconfig/beanconfig01.conf index 3d9373e4..58f5983b 100644 --- a/config/src/test/resources/beanconfig/beanconfig01.conf +++ b/config/src/test/resources/beanconfig/beanconfig01.conf @@ -62,5 +62,11 @@ "shouldBeInt" : true, "should-be-boolean" : 42, "should-be-list" : "hello" + }, + "preferCamelNames" : { + "foo-bar" : "no", + "fooBar" : "yes", + "baz-bar" : "no", + "bazBar" : "yes" } } diff --git a/config/src/test/scala/com/typesafe/config/impl/ConfigBeanFactoryTest.scala b/config/src/test/scala/com/typesafe/config/impl/ConfigBeanFactoryTest.scala index 42b8c76a..d1183266 100644 --- a/config/src/test/scala/com/typesafe/config/impl/ConfigBeanFactoryTest.scala +++ b/config/src/test/scala/com/typesafe/config/impl/ConfigBeanFactoryTest.scala @@ -118,6 +118,15 @@ class ConfigBeanFactoryTest extends TestUtils { assertEquals(ConfigMemorySize.ofBytes(1000), beanConfig.getThousandBytes) } + @Test + def testPreferCamelNames() { + val beanConfig = ConfigBeanFactory.create(loadConfig().getConfig("preferCamelNames"), classOf[PreferCamelNamesConfig]) + assertNotNull(beanConfig) + + assertEquals("yes", beanConfig.getFooBar) + assertEquals("yes", beanConfig.getBazBar) + } + private def loadConfig(): Config = { val configIs: InputStream = this.getClass().getClassLoader().getResourceAsStream("beanconfig/beanconfig01.conf") try {