mirror of
https://github.com/lightbend/config.git
synced 2025-03-22 23:30:27 +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)) {
|
if (isOptionalProperty(clazz, beanProp)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// Otherwise, raise a {@link Missing} exception right here
|
// Otherwise, raise a {@link BugOrBroken} exception
|
||||||
throw new ConfigException.Missing(beanProp.getName());
|
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);
|
Object unwrapped = getValue(clazz, parameterType, parameterClass, config, configPropName);
|
||||||
setter.invoke(bean, unwrapped);
|
setter.invoke(bean, unwrapped);
|
||||||
|
@ -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;
|
||||||
|
@ -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"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -173,12 +173,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
|
||||||
|
Loading…
Reference in New Issue
Block a user