From 3ddde21487b11225acc410377282ea12b18456f2 Mon Sep 17 00:00:00 2001 From: Bukkit/Spigot Date: Sat, 4 Dec 2021 21:38:45 +0100 Subject: [PATCH] SPIGOT-6830: Fix addDefaults with Configuration overrides child Sections in the defaults By: Wolf2323 --- .../configuration/MemoryConfiguration.java | 6 ++- .../configuration/ConfigurationTest.java | 37 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/paper-api/src/main/java/org/bukkit/configuration/MemoryConfiguration.java b/paper-api/src/main/java/org/bukkit/configuration/MemoryConfiguration.java index 010a97fee2..47dbd5c6a4 100644 --- a/paper-api/src/main/java/org/bukkit/configuration/MemoryConfiguration.java +++ b/paper-api/src/main/java/org/bukkit/configuration/MemoryConfiguration.java @@ -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 diff --git a/paper-api/src/test/java/org/bukkit/configuration/ConfigurationTest.java b/paper-api/src/test/java/org/bukkit/configuration/ConfigurationTest.java index e554ebba65..65f6ba389d 100644 --- a/paper-api/src/test/java/org/bukkit/configuration/ConfigurationTest.java +++ b/paper-api/src/test/java/org/bukkit/configuration/ConfigurationTest.java @@ -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 values = getTestValues(); + values.put("default-section.string", "String Value"); + Configuration defaults = getConfig(); + Configuration defaultsAdditional = getConfig(); + + for (Map.Entry entry : values.entrySet()) { + defaults.set(entry.getKey(), entry.getValue()); + } + config.addDefaults(defaults); + + Map additionalValues = new HashMap<>(); + additionalValues.put("default-section.additionalString", "Additional String"); + additionalValues.put("default-section.additionalInt", 42); + for (Map.Entry entry : additionalValues.entrySet()) { + defaultsAdditional.set(entry.getKey(), entry.getValue()); + } + config.addDefaults(defaultsAdditional); + values.putAll(additionalValues); + + for (Map.Entry 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. */