SPIGOT-6830: Fix addDefaults with Configuration overrides child Sections in the defaults

By: Wolf2323 <gabrielpatrikurban@gmail.com>
This commit is contained in:
Bukkit/Spigot 2021-12-04 21:38:45 +01:00
parent 36fb1f9c00
commit 3ddde21487
2 changed files with 42 additions and 1 deletions

View File

@ -54,7 +54,11 @@ public class MemoryConfiguration extends MemorySection implements Configuration
public void addDefaults(@NotNull Configuration defaults) {
Validate.notNull(defaults, "Defaults may not be null");
addDefaults(defaults.getValues(true));
for (String key : defaults.getKeys(true)) {
if (!defaults.isConfigurationSection(key)) {
addDefault(key, defaults.get(key));
}
}
}
@Override

View File

@ -103,6 +103,43 @@ public abstract class ConfigurationTest {
}
}
/**
* Test of addDefaults method, of class Configuration but with existing
* defaults in a child section.
*/
@Test
public void testAddDefaults_Configuration_WithExisting() {
Configuration config = getConfig();
Map<String, Object> values = getTestValues();
values.put("default-section.string", "String Value");
Configuration defaults = getConfig();
Configuration defaultsAdditional = getConfig();
for (Map.Entry<String, Object> entry : values.entrySet()) {
defaults.set(entry.getKey(), entry.getValue());
}
config.addDefaults(defaults);
Map<String, Object> additionalValues = new HashMap<>();
additionalValues.put("default-section.additionalString", "Additional String");
additionalValues.put("default-section.additionalInt", 42);
for (Map.Entry<String, Object> entry : additionalValues.entrySet()) {
defaultsAdditional.set(entry.getKey(), entry.getValue());
}
config.addDefaults(defaultsAdditional);
values.putAll(additionalValues);
for (Map.Entry<String, Object> entry : values.entrySet()) {
String path = entry.getKey();
Object object = entry.getValue();
assertEquals(object, config.get(path));
assertTrue(config.contains(path));
assertFalse(config.isSet(path));
assertTrue(config.getDefaults().isSet(path));
}
}
/**
* Test of setDefaults method, of class Configuration.
*/