If property key is missing in config, raise error early.

This commit is contained in:
Karthick Sankarachary 2016-05-31 12:42:59 -07:00
parent 2b6452762f
commit 13c5fc057e
2 changed files with 18 additions and 8 deletions
config/src
main/java/com/typesafe/config/impl
test/scala/com/typesafe/config/impl

View File

@ -110,17 +110,17 @@ public class ConfigBeanImpl {
Type parameterType = setter.getGenericParameterTypes()[0];
Class<?> parameterClass = setter.getParameterTypes()[0];
String configPropName = originalNames.get(beanProp.getName());
// Is the property key missing in the config?
if (configPropName == null) {
configPropName = beanProp.getName();
}
try {
Object unwrapped = getValue(clazz, parameterType, parameterClass, config, configPropName);
setter.invoke(bean, unwrapped);
} catch (ConfigException.Missing e) {
if (!isOptionalProperty(clazz, beanProp)) {
throw e;
// If so, continue if the field is marked as @{link Optional}
if (isOptionalProperty(clazz, beanProp)) {
continue;
}
// Otherwise, raise a {@link Missing} exception right here
throw new ConfigException.Missing(beanProp.getName());
}
Object unwrapped = getValue(clazz, parameterType, parameterClass, config, configPropName);
setter.invoke(bean, unwrapped);
}
return bean;
} catch (InstantiationException e) {

View File

@ -171,6 +171,16 @@ class ConfigBeanFactoryTest extends TestUtils {
assertEquals("notNull", beanConfig.getValueObject.getMandatoryValue)
}
@Test
def testNotAnOptionalProperty(): Unit = {
val e = intercept[ConfigException.ValidationFailed] {
ConfigBeanFactory.create(parseConfig("{valueObject: {}}"), classOf[ObjectsConfig])
}
assertTrue("missing value error", e.getMessage.contains("No setting"))
assertTrue("error about the right property", e.getMessage.contains("mandatoryValue"))
}
@Test
def testNotABeanField() {
val e = intercept[ConfigException.BadBean] {