Don't Bail If Optional Property Is Nullified

This commit is contained in:
Karthick Sankarachary 2017-03-31 12:52:15 -07:00
parent 5b4ec9a9e6
commit 95592e1f06
4 changed files with 44 additions and 8 deletions

View File

@ -116,8 +116,15 @@ public class ConfigBeanImpl {
if (isOptionalProperty(clazz, beanProp)) {
continue;
}
// Otherwise, raise a {@link Missing} exception right here
throw new ConfigException.Missing(beanProp.getName());
// Otherwise, raise a {@link BugOrBroken} exception
throw new ConfigException.BugOrBroken("Should have detected missing props earlier");
} else if (isOptionalProperty(clazz, beanProp)) {
// Is the property null in the config?
if (config.getIsNull(configPropName)) {
// If so, set it as such in the bean.
setter.invoke(bean, (Object) null);
continue;
}
}
Object unwrapped = getValue(clazz, parameterType, parameterClass, config, configPropName);
setter.invoke(bean, unwrapped);

View File

@ -5,9 +5,11 @@ import com.typesafe.config.Optional;
public class ObjectsConfig {
public static class ValueObject {
private String mandatoryValue;
@Optional
private String optionalValue;
private String mandatoryValue;
@Optional
private String defaultedValue = "hello";
public String getMandatoryValue() {
return mandatoryValue;
@ -24,6 +26,14 @@ public class ObjectsConfig {
public void setOptionalValue(String optionalValue) {
this.optionalValue = optionalValue;
}
public String getDefaultedValue() {
return defaultedValue;
}
public void setDefaultedValue(String defaultedValue) {
this.defaultedValue = defaultedValue;
}
}
private ValueObject valueObject;

View File

@ -97,9 +97,17 @@
"problem" : "P1",
"solutions" : ["S1", "S3"]
},
"objects" : {
"objectsMissing" : {
"valueObject": {
"mandatoryValue": "notNull"
"mandatoryValue": "hello"
"defaultedValue": null
}
},
"objectsPresent" : {
"valueObject": {
"mandatoryValue": "hello"
"optionalValue": "hello"
"defaultedValue": "hello"
}
}
}

View File

@ -173,12 +173,23 @@ class ConfigBeanFactoryTest extends TestUtils {
}
@Test
def testOptionalProperties() {
val beanConfig: ObjectsConfig = ConfigBeanFactory.create(loadConfig().getConfig("objects"), classOf[ObjectsConfig])
def testOptionalMissingProperties() {
val beanConfig: ObjectsConfig = ConfigBeanFactory.create(loadConfig().getConfig("objectsMissing"), classOf[ObjectsConfig])
assertNotNull(beanConfig)
assertNotNull(beanConfig.getValueObject)
assertNull(beanConfig.getValueObject.getOptionalValue)
assertEquals("notNull", beanConfig.getValueObject.getMandatoryValue)
assertNull(beanConfig.getValueObject.getDefaultedValue)
assertEquals("hello", beanConfig.getValueObject.getMandatoryValue)
}
@Test
def testOptionalPresentProperties() {
val beanConfig: ObjectsConfig = ConfigBeanFactory.create(loadConfig().getConfig("objectsPresent"), classOf[ObjectsConfig])
assertNotNull(beanConfig)
assertNotNull(beanConfig.getValueObject)
assertEquals("hello", beanConfig.getValueObject.getOptionalValue)
assertEquals("hello", beanConfig.getValueObject.getDefaultedValue)
assertEquals("hello", beanConfig.getValueObject.getMandatoryValue)
}
@Test