From 4d98a0069e549321b0470b9f1abc79087272febc Mon Sep 17 00:00:00 2001 From: tzarouali Date: Mon, 20 Feb 2017 20:02:31 +0000 Subject: [PATCH 1/2] Fix for issue #447. Ignore bean properties without a proper setter/getter --- .../typesafe/config/impl/ConfigBeanImpl.java | 4 +++- ...DifferentFieldNameFromAccessorsConfig.java | 24 +++++++++++++++++++ .../config/impl/ConfigBeanFactoryTest.scala | 8 +++++++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 config/src/test/java/beanconfig/DifferentFieldNameFromAccessorsConfig.java 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..15341afc 100644 --- a/config/src/main/java/com/typesafe/config/impl/ConfigBeanImpl.java +++ b/config/src/main/java/com/typesafe/config/impl/ConfigBeanImpl.java @@ -69,7 +69,9 @@ public class ConfigBeanImpl { try { List beanProps = new ArrayList(); for (PropertyDescriptor beanProp : beanInfo.getPropertyDescriptors()) { - if (beanProp.getReadMethod() == null || beanProp.getWriteMethod() == null) { + if (beanProp.getReadMethod() == null + || beanProp.getWriteMethod() == null + || getField(clazz, beanProp.getName()) == null) { continue; } beanProps.add(beanProp); 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 { From 870dd28cc7da1cfee35ff16b536aa2baeb598d72 Mon Sep 17 00:00:00 2001 From: tzarouali Date: Mon, 20 Feb 2017 21:29:54 +0000 Subject: [PATCH 2/2] Fix for issue #447. Instead of ignoring fields without a proper getter/setter, avoid the NPE checking first that there's a field to work with. --- .../main/java/com/typesafe/config/impl/ConfigBeanImpl.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) 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 15341afc..69da879b 100644 --- a/config/src/main/java/com/typesafe/config/impl/ConfigBeanImpl.java +++ b/config/src/main/java/com/typesafe/config/impl/ConfigBeanImpl.java @@ -69,9 +69,7 @@ public class ConfigBeanImpl { try { List beanProps = new ArrayList(); for (PropertyDescriptor beanProp : beanInfo.getPropertyDescriptors()) { - if (beanProp.getReadMethod() == null - || beanProp.getWriteMethod() == null - || getField(clazz, beanProp.getName()) == null) { + if (beanProp.getReadMethod() == null || beanProp.getWriteMethod() == null) { continue; } beanProps.add(beanProp); @@ -279,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) {