mirror of
https://github.com/lightbend/config.git
synced 2025-03-14 19:30:25 +08:00
Don't Bail If Optional Property Is Nullified
This commit is contained in:
parent
5b4ec9a9e6
commit
95592e1f06
@ -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);
|
||||
|
@ -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;
|
||||
|
@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user