OkaeriPoland / okaeri-configs

Simple Java/POJO config library written with love and Lombok
MIT License
77 stars 11 forks source link

Null value after config update #25

Closed plytki closed 2 years ago

plytki commented 2 years ago

When config file is updated with new variable, it sets null value for it and in the config class I have something different in my initializer.

Steps to reproduce the behavior:

Create configuration file with built-in class. Initialize class and assign it to variable at same time to create an instance of object to the config. After doing that update your configuration file by adding new String variable to constructor params and initialize it in the class also (class variable defined earlier). Try to update your configuration with preset presented on the bottom. Your updated String variable will be null after updating the config with newly added custom class variable.

All used libs are versioned in 3.4.2.

Config classes and setup routine

public PluginConfiguration createPluginConfiguration(File pluginConfigurationFile) {
        return ConfigManager.create(PluginConfiguration.class, (it) -> {
            it.withConfigurer(new OkaeriValidator(new YamlBukkitConfigurer(SectionSeparator.NEW_LINE), true), new SerdesBukkit());
            it.withBindFile(pluginConfigurationFile);
            it.saveDefaults();
            it.load(true);
        });
    }

Try to update this config file with java class file attached. bug-files.zip

dasavick commented 2 years ago

Please make sure a default constructor is available. Otherwise, okaeri-configs can in some cases create the instance with unsafe which does not fill default values.

plytki commented 2 years ago

What do you mean by default constructor?

dasavick commented 2 years ago

A default constructor is a constructor which can be called with no arguments (either defined with an empty parameter list, or with default arguments provided for every parameter).

This can be also achieved using @NoArgsConstructor from Lombok.

plytki commented 2 years ago

I tried this way but there is still a null value.

dasavick commented 2 years ago

Please provide specific instructions where and what to add to reproduce the problem, as your configuration has multiple levels.

plytki commented 2 years ago

These variables are already added to the PluginConfiguration.java file so you just have to run the creator to see newly added null values. On line 168 you have that 2 last constructor params that are null values when you try to update the config with provided ConfigManager creator. This built-in MySQL class that produces null values is initialized on line 149.

dasavick commented 2 years ago

That explains a bit. Please use fields for the default values, instead of the full constructor for the subconfig initialization, if you want okaeri-configs to add the new default values.

// don't
public MySQL mysql = new MySQL("localhost", 3306 /* ... */);
// do
public String hostname = "localhost";
public int port = 3306;
// ...

Note that this behavior is indeed correct, because you are only providing the default value for the mysql itself, which is, in fact, already present when adding properties to the MySQL class.