Don't Bail If Optional Property Is Nullified

This commit is contained in:
Karthick Sankarachary 2017-03-31 12:52:15 -07:00
parent b5578f4083
commit 77fc35c137
4 changed files with 42 additions and 6 deletions

View File

@ -120,6 +120,13 @@ public class ConfigBeanImpl {
} }
// Otherwise, raise a {@link Missing} exception right here // Otherwise, raise a {@link Missing} exception right here
throw new ConfigException.Missing(beanProp.getName()); throw new ConfigException.Missing(beanProp.getName());
} 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); Object unwrapped = getValue(clazz, parameterType, parameterClass, config, configPropName);
setter.invoke(bean, unwrapped); setter.invoke(bean, unwrapped);

View File

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

View File

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

View File

@ -206,12 +206,23 @@ class ConfigBeanFactoryTest extends TestUtils {
} }
@Test @Test
def testOptionalProperties() { def testOptionalMissingProperties() {
val beanConfig: ObjectsConfig = ConfigBeanFactory.create(loadConfig().getConfig("objects"), classOf[ObjectsConfig]) val beanConfig: ObjectsConfig = ConfigBeanFactory.create(loadConfig().getConfig("objectsMissing"), classOf[ObjectsConfig])
assertNotNull(beanConfig) assertNotNull(beanConfig)
assertNotNull(beanConfig.getValueObject) assertNotNull(beanConfig.getValueObject)
assertNull(beanConfig.getValueObject.getOptionalValue) 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 @Test