From 4fd0026eb567641acda5daae4fdafbfcdfc8de52 Mon Sep 17 00:00:00 2001 From: Bukkit/Spigot Date: Sun, 2 Jan 2022 21:08:01 +0100 Subject: [PATCH] #703: Fix/test yaml anchors and merge By: Wolf2323 --- .../configuration/file/YamlConfiguration.java | 1 + .../configuration/file/YamlConstructor.java | 6 ++ .../file/YamlConfigurationTest.java | 55 +++++++++++++++++++ 3 files changed, 62 insertions(+) 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 5849e8e5d3..019eec6a25 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 @@ -141,6 +141,7 @@ public class YamlConfiguration extends FileConfiguration { } private void fromNodeTree(@NotNull MappingNode input, @NotNull ConfigurationSection section) { + constructor.flattenMapping(input); for (NodeTuple nodeTuple : input.getValue()) { ScalarNode key = (ScalarNode) nodeTuple.getKeyNode(); String keyString = key.getValue(); diff --git a/paper-api/src/main/java/org/bukkit/configuration/file/YamlConstructor.java b/paper-api/src/main/java/org/bukkit/configuration/file/YamlConstructor.java index ca167df516..7052f8a56f 100644 --- a/paper-api/src/main/java/org/bukkit/configuration/file/YamlConstructor.java +++ b/paper-api/src/main/java/org/bukkit/configuration/file/YamlConstructor.java @@ -7,6 +7,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.yaml.snakeyaml.constructor.SafeConstructor; import org.yaml.snakeyaml.error.YAMLException; +import org.yaml.snakeyaml.nodes.MappingNode; import org.yaml.snakeyaml.nodes.Node; import org.yaml.snakeyaml.nodes.Tag; @@ -16,6 +17,11 @@ public class YamlConstructor extends SafeConstructor { this.yamlConstructors.put(Tag.MAP, new ConstructCustomObject()); } + @Override + public void flattenMapping(@NotNull final MappingNode node) { + super.flattenMapping(node); + } + @NotNull public Object construct(@NotNull Node node) { return constructObject(node); 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 fa5f54a289..3e01af7643 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 @@ -4,6 +4,7 @@ import static org.junit.Assert.*; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import org.bukkit.configuration.InvalidConfigurationException; import org.junit.Test; public class YamlConfigurationTest extends FileConfigurationTest { @@ -89,4 +90,58 @@ public class YamlConfigurationTest extends FileConfigurationTest { assertEquals(expected, result); } + + @Test + public void testAnchorNode() throws InvalidConfigurationException { + YamlConfiguration config = getConfig(); + String content = "effects:\n" + + " eff1: &novaEarth\n" + + " position: special\n" + + " effect: nova\n" + + " eff2: *novaEarth\n" + + " eff3: *novaEarth"; + config.loadFromString(content); + + String expected = "effects:\n" + + " eff1:\n" + + " position: special\n" + + " effect: nova\n" + + " eff2:\n" + + " position: special\n" + + " effect: nova\n" + + " eff3:\n" + + " position: special\n" + + " effect: nova\n"; + assertEquals(expected, config.saveToString()); + } + + @Test + public void testMergeNode() throws InvalidConfigurationException { + YamlConfiguration config = getConfig(); + String content = "effects:\n" + + " eff1: &novaEarth\n" + + " position: special\n" + + " effect: nova\n" + + " eff2: \n" + + " <<: *novaEarth\n" + + " height-offset: 0\n" + + " eff3: \n" + + " <<: *novaEarth\n" + + " height-offset: 2"; + config.loadFromString(content); + + String expected = "effects:\n" + + " eff1:\n" + + " position: special\n" + + " effect: nova\n" + + " eff2:\n" + + " position: special\n" + + " effect: nova\n" + + " height-offset: 0\n" + + " eff3:\n" + + " position: special\n" + + " effect: nova\n" + + " height-offset: 2\n"; + assertEquals(expected, config.saveToString()); + } }