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 01fadb4d..69da879b 100644 --- a/config/src/main/java/com/typesafe/config/impl/ConfigBeanImpl.java +++ b/config/src/main/java/com/typesafe/config/impl/ConfigBeanImpl.java @@ -277,7 +277,7 @@ public class ConfigBeanImpl { private static boolean isOptionalProperty(Class beanClass, PropertyDescriptor beanProp) { Field field = getField(beanClass, beanProp.getName()); - return (field.getAnnotationsByType(Optional.class).length > 0); + return field != null && (field.getAnnotationsByType(Optional.class).length > 0); } private static Field getField(Class beanClass, String fieldName) { diff --git a/config/src/test/java/beanconfig/DifferentFieldNameFromAccessorsConfig.java b/config/src/test/java/beanconfig/DifferentFieldNameFromAccessorsConfig.java new file mode 100644 index 00000000..0eb3518f --- /dev/null +++ b/config/src/test/java/beanconfig/DifferentFieldNameFromAccessorsConfig.java @@ -0,0 +1,24 @@ +package beanconfig; + +public class DifferentFieldNameFromAccessorsConfig { + + private String customStringField; + private Long number; + + + public String getStringField() { + return customStringField; + } + + public void setStringField(String stringField) { + this.customStringField = stringField; + } + + public Long getNumber() { + return number; + } + + public void setNumber(Long number) { + this.number = number; + } +} 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 c6d62782..8c85e455 100644 --- a/config/src/test/scala/com/typesafe/config/impl/ConfigBeanFactoryTest.scala +++ b/config/src/test/scala/com/typesafe/config/impl/ConfigBeanFactoryTest.scala @@ -237,6 +237,14 @@ class ConfigBeanFactoryTest extends TestUtils { assertTrue("error about the right property", e.getMessage.contains("'map'")) } + @Test + def testDifferentFieldNameFromAccessors(): Unit = { + val e = intercept[ConfigException.ValidationFailed] { + ConfigBeanFactory.create(ConfigFactory.empty(), classOf[DifferentFieldNameFromAccessorsConfig]) + } + assertTrue("only one missing value error", e.getMessage.contains("No setting")) + } + private def loadConfig(): Config = { val configIs: InputStream = this.getClass().getClassLoader().getResourceAsStream("beanconfig/beanconfig01.conf") try {