mirror of
https://github.com/lightbend/config.git
synced 2025-04-26 04:50:29 +08:00
If property key is missing in config, raise error early.
This commit is contained in:
parent
2b6452762f
commit
13c5fc057e
config/src
main/java/com/typesafe/config/impl
test/scala/com/typesafe/config/impl
@ -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) {
|
||||
|
@ -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] {
|
||||
|
Loading…
Reference in New Issue
Block a user