Description
If I create a custom bean property, and pass default bean with default value, config is created with that instance.
If one of the fields is then removed, ConfigMe throws ConfigMeMapperException
instead of adding the missing field from default instance.
According to https://github.com/AuthMe/ConfigMe/wiki/Bean-properties#error-handling , it should use the default object instead:
If it does not succeed to set a field's value, the mapping is considered to have failed and the default object is used. It is therefore important to initialize fields as shown with the country name above if you don't want the absence of one field to make ConfigMe ignore all fields and use the default bean object provided on initialization.
I've written example project, based on https://github.com/AuthMe/ConfigMe/wiki/Bean-properties#using-beans-within-beans.
Example stacktrace:
Exception in thread "main" ch.jalu.configme.beanmapper.ConfigMeMapperException: Could not create object of type 'dev.bloodstone.tests.configmetest.Country'. It is required to have a default constructor.
at ch.jalu.configme.beanmapper.MapperImpl.createBeanMatchingType(MapperImpl.java:359)
at ch.jalu.configme.beanmapper.MapperImpl.createBean(MapperImpl.java:333)
at ch.jalu.configme.beanmapper.MapperImpl.convertValueForType(MapperImpl.java:189)
at ch.jalu.configme.beanmapper.MapperImpl.convertToBean(MapperImpl.java:159)
at ch.jalu.configme.properties.types.BeanPropertyType.convert(BeanPropertyType.java:28)
at ch.jalu.configme.properties.TypeBasedProperty.getFromReader(TypeBasedProperty.java:34)
at ch.jalu.configme.properties.BaseProperty.determineValue(BaseProperty.java:46)
at ch.jalu.configme.configurationdata.ConfigurationDataImpl.lambda$initializeValues$0(ConfigurationDataImpl.java:67)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1540)
at java.base/java.util.Collections$UnmodifiableCollection.forEach(Collections.java:1085)
at ch.jalu.configme.configurationdata.ConfigurationDataImpl.initializeValues(ConfigurationDataImpl.java:67)
at ch.jalu.configme.SettingsManagerImpl.loadFromResourceAndValidate(SettingsManagerImpl.java:88)
at ch.jalu.configme.SettingsManagerImpl.<init>(SettingsManagerImpl.java:45)
at ch.jalu.configme.SettingsManagerBuilder.create(SettingsManagerBuilder.java:115)
at dev.bloodstone.tests.configmetest.ConfigManager.<init>(ConfigManager.kt:11)
at dev.bloodstone.tests.configmetest.MainKt.main(Main.kt:5)
Caused by: java.lang.NoSuchMethodException: dev.bloodstone.tests.configmetest.Country.<init>()
at java.base/java.lang.Class.getConstructor0(Class.java:3349)
at java.base/java.lang.Class.getDeclaredConstructor(Class.java:2553)
at ch.jalu.configme.beanmapper.MapperImpl.createBeanMatchingType(MapperImpl.java:357)
... 15 more
Reproduction
Download: https://github.com/Prof-Bloodstone/ConfigMeTest
Build the project and run it once - it should create /tmp/config.yml
Remove leader name from config, and run it again - it'll throw above error.
git clone https://github.com/Prof-Bloodstone/ConfigMeTest.git /tmp/cmt
cd /tmp/cmt
./gradlew build
java -jar build/libs/configmetest-1.0-SNAPSHOT.jar
sed -i '5 d' /tmp/config.yml
java -jar build/libs/configmetest-1.0-SNAPSHOT.jar
Use case
I have multiple fields in a config - each of those is the same bean type, but each of those have different default values. This makes it harder to do that.