SPIGOT-6234: enum classes don't serialize properly when implementing ConfigurationSerializable

By: md_5 <git@md-5.net>
This commit is contained in:
Bukkit/Spigot 2020-11-17 12:56:36 +11:00
parent 39f6cd7ffa
commit 139ca6a269
4 changed files with 27 additions and 3 deletions

View File

@ -14,6 +14,9 @@ public class YamlRepresenter extends Representer {
public YamlRepresenter() { public YamlRepresenter() {
this.multiRepresenters.put(ConfigurationSection.class, new RepresentConfigurationSection()); this.multiRepresenters.put(ConfigurationSection.class, new RepresentConfigurationSection());
this.multiRepresenters.put(ConfigurationSerializable.class, new RepresentConfigurationSerializable()); 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 { private class RepresentConfigurationSection extends RepresentMap {

View File

@ -2,11 +2,13 @@ package org.bukkit.configuration;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import org.junit.Test; import org.junit.Test;
@ -592,9 +594,18 @@ public abstract class ConfigurationSectionTest {
assertFalse(section.isConfigurationSection("doesntExist")); assertFalse(section.isConfigurationSection("doesntExist"));
} }
public enum TestEnum { public enum TestEnum implements ConfigurationSerializable {
HELLO, HELLO,
WORLD, WORLD,
BANANAS BANANAS;
@Override
public Map<String, Object> serialize() {
return Collections.singletonMap("variant", this.name());
}
public static TestEnum deserialize(Map<String, Object> map) {
return TestEnum.valueOf((String) map.get("variant"));
}
} }
} }

View File

@ -7,10 +7,16 @@ import java.util.HashSet;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import org.bukkit.configuration.serialization.ConfigurationSerialization;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import org.junit.Test; import org.junit.Test;
public abstract class ConfigurationTest { public abstract class ConfigurationTest {
static {
ConfigurationSerialization.registerClass(ConfigurationSectionTest.TestEnum.class);
}
public abstract Configuration getConfig(); public abstract Configuration getConfig();
public Map<String, Object> getTestValues() { public Map<String, Object> getTestValues() {
@ -24,6 +30,7 @@ public abstract class ConfigurationTest {
result.put("vector", new Vector(12345.67, 64, -12345.6789)); result.put("vector", new Vector(12345.67, 64, -12345.6789));
result.put("list", Arrays.asList(1, 2, 3, 4, 5)); result.put("list", Arrays.asList(1, 2, 3, 4, 5));
result.put("42", "The Answer"); result.put("42", "The Answer");
result.put("enum", ConfigurationSectionTest.TestEnum.BANANAS);
return result; return result;
} }

View File

@ -38,7 +38,10 @@ public class YamlConfigurationTest extends FileConfigurationTest {
+ "- 3\n" + "- 3\n"
+ "- 4\n" + "- 4\n"
+ "- 5\n" + "- 5\n"
+ "'42': The Answer\n"; + "'42': The Answer\n"
+ "enum:\n"
+ " ==: org.bukkit.configuration.ConfigurationSectionTest$TestEnum\n"
+ " variant: BANANAS\n";
} }
@Test @Test