From 7c9037dfeecff45c8308560a03dc86e078dff884 Mon Sep 17 00:00:00 2001 From: Bukkit/Spigot Date: Sun, 16 Apr 2023 10:26:10 +1000 Subject: [PATCH] SPIGOT-6885: Add test and easier to debug code for reference in yaml configuration comments By: Wolf2323 --- .../configuration/file/YamlConfiguration.java | 11 +++++---- .../file/YamlConfigurationTest.java | 24 +++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/paper-api/src/main/java/org/bukkit/configuration/file/YamlConfiguration.java b/paper-api/src/main/java/org/bukkit/configuration/file/YamlConfiguration.java index 4b17c796b1..a090ef390d 100644 --- a/paper-api/src/main/java/org/bukkit/configuration/file/YamlConfiguration.java +++ b/paper-api/src/main/java/org/bukkit/configuration/file/YamlConfiguration.java @@ -100,11 +100,14 @@ public class YamlConfiguration extends FileConfiguration { MappingNode node; try (Reader reader = new UnicodeReader(new ByteArrayInputStream(contents.getBytes(StandardCharsets.UTF_8)))) { - node = (MappingNode) yaml.compose(reader); - } catch (YAMLException | IOException e) { + Node rawNode = yaml.compose(reader); + try { + node = (MappingNode) rawNode; + } catch (ClassCastException e) { + throw new InvalidConfigurationException("Top level is not a Map."); + } + } catch (YAMLException | IOException | ClassCastException e) { throw new InvalidConfigurationException(e); - } catch (ClassCastException e) { - throw new InvalidConfigurationException("Top level is not a Map."); } this.map.clear(); 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 194949d74a..003f45d8cd 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 @@ -11,6 +11,7 @@ import java.util.Map.Entry; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.MemoryConfiguration; +import org.jetbrains.annotations.NotNull; import org.junit.Test; public class YamlConfigurationTest extends FileConfigurationTest { @@ -170,6 +171,29 @@ public class YamlConfigurationTest extends FileConfigurationTest { assertEquals(data, result); } + // SPIGOT-6885 + @Test + public void testReference() throws InvalidConfigurationException { + String okYAML = "dummy: test\n" + + "conf:\n" + + " - test #comment ok\n"; + assertNotNull(toConfig(okYAML, false)); + assertNotNull(toConfig(okYAML, true)); + + String badYAML = "dummy: &a test\n" + + "conf:\n" + + " - *a #comment not ok here\n"; + assertNotNull(toConfig(badYAML, false)); + assertNotNull(toConfig(badYAML, true)); + } + + private YamlConfiguration toConfig(@NotNull String contents, boolean parseComments) throws InvalidConfigurationException { + YamlConfiguration config = new YamlConfiguration(); + config.options().parseComments(parseComments); + config.loadFromString(contents); + return config; + } + @Test public void testOnlyHeader() { YamlConfiguration config = getConfig();