diff --git a/paper-api/src/main/java/org/bukkit/configuration/file/YamlRepresenter.java b/paper-api/src/main/java/org/bukkit/configuration/file/YamlRepresenter.java index f03da36223..9dd3890f97 100644 --- a/paper-api/src/main/java/org/bukkit/configuration/file/YamlRepresenter.java +++ b/paper-api/src/main/java/org/bukkit/configuration/file/YamlRepresenter.java @@ -14,6 +14,9 @@ public class YamlRepresenter extends Representer { public YamlRepresenter() { this.multiRepresenters.put(ConfigurationSection.class, new RepresentConfigurationSection()); this.multiRepresenters.put(ConfigurationSerializable.class, new RepresentConfigurationSerializable()); + // SPIGOT-6234: We could just switch YamlConstructor to extend Constructor rather than SafeConstructor, however there is a very small risk of issues with plugins treating config as untrusted input + // So instead we will just allow future plugins to have their enums extend ConfigurationSerializable + this.multiRepresenters.remove(Enum.class); } private class RepresentConfigurationSection extends RepresentMap { diff --git a/paper-api/src/test/java/org/bukkit/configuration/ConfigurationSectionTest.java b/paper-api/src/test/java/org/bukkit/configuration/ConfigurationSectionTest.java index 4356b90711..1e52992839 100644 --- a/paper-api/src/test/java/org/bukkit/configuration/ConfigurationSectionTest.java +++ b/paper-api/src/test/java/org/bukkit/configuration/ConfigurationSectionTest.java @@ -2,11 +2,13 @@ package org.bukkit.configuration; import static org.junit.Assert.*; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import org.bukkit.Material; +import org.bukkit.configuration.serialization.ConfigurationSerializable; import org.bukkit.inventory.ItemStack; import org.bukkit.util.Vector; import org.junit.Test; @@ -592,9 +594,18 @@ public abstract class ConfigurationSectionTest { assertFalse(section.isConfigurationSection("doesntExist")); } - public enum TestEnum { + public enum TestEnum implements ConfigurationSerializable { HELLO, WORLD, - BANANAS + BANANAS; + + @Override + public Map serialize() { + return Collections.singletonMap("variant", this.name()); + } + + public static TestEnum deserialize(Map map) { + return TestEnum.valueOf((String) map.get("variant")); + } } } 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 94eb854e94..e554ebba65 100644 --- a/paper-api/src/test/java/org/bukkit/configuration/ConfigurationTest.java +++ b/paper-api/src/test/java/org/bukkit/configuration/ConfigurationTest.java @@ -7,10 +7,16 @@ import java.util.HashSet; import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; +import org.bukkit.configuration.serialization.ConfigurationSerialization; import org.bukkit.util.Vector; import org.junit.Test; public abstract class ConfigurationTest { + + static { + ConfigurationSerialization.registerClass(ConfigurationSectionTest.TestEnum.class); + } + public abstract Configuration getConfig(); public Map getTestValues() { @@ -24,6 +30,7 @@ public abstract class ConfigurationTest { result.put("vector", new Vector(12345.67, 64, -12345.6789)); result.put("list", Arrays.asList(1, 2, 3, 4, 5)); result.put("42", "The Answer"); + result.put("enum", ConfigurationSectionTest.TestEnum.BANANAS); return result; } diff --git a/paper-api/src/test/java/org/bukkit/configuration/file/YamlConfigurationTest.java b/paper-api/src/test/java/org/bukkit/configuration/file/YamlConfigurationTest.java index 06ac381b5c..cd79a3487f 100644 --- a/paper-api/src/test/java/org/bukkit/configuration/file/YamlConfigurationTest.java +++ b/paper-api/src/test/java/org/bukkit/configuration/file/YamlConfigurationTest.java @@ -38,7 +38,10 @@ public class YamlConfigurationTest extends FileConfigurationTest { + "- 3\n" + "- 4\n" + "- 5\n" - + "'42': The Answer\n"; + + "'42': The Answer\n" + + "enum:\n" + + " ==: org.bukkit.configuration.ConfigurationSectionTest$TestEnum\n" + + " variant: BANANAS\n"; } @Test