Uploaded image for project: 'Spigot'
  1. Spigot
  2. SPIGOT-6885

Using references with comments are broken in configurations

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: Minor Minor
    • None
    • None
    • CraftBukkit version 3395-Spigot-f4ff00f-cc86ab1 (MC: 1.18.1) (Implementing API version 1.18.1-R0.1-SNAPSHOT)
    • Yes

      Using a reference in a configuration with a comment behind it throws an error.

      example spigot.yml:

      settings: 
      
        debug: &a false
        save-user-cache-on-stop-only: *a #bad comment
        moved-wrongly-threshold: 0.0625
        # The rest is omitted for brevity
      

      It should be noted that this is a valid yaml file. By removing #bad comment no error is thrown.

      Error thrown 

      [Server thread/ERROR]: Could not load spigot.yml, please correct your syntax errors
      org.bukkit.configuration.InvalidConfigurationException: Top level is not a Map.
              at org.bukkit.configuration.file.YamlConfiguration.loadFromString(YamlConfiguration.java:105) ~[spigot-api-1.18.1-R0.1-SNAPSHOT.jar:?]
              at org.bukkit.configuration.file.FileConfiguration.load(FileConfiguration.java:160) ~[spigot-api-1.18.1-R0.1-SNAPSHOT.jar:?]
              at org.bukkit.configuration.file.FileConfiguration.load(FileConfiguration.java:128) ~[spigot-api-1.18.1-R0.1-SNAPSHOT.jar:?]
              at org.spigotmc.SpigotConfig.init(SpigotConfig.java:59) ~[spigot-1.18.1-R0.1-SNAPSHOT.jar:3395-Spigot-f4ff00f-cc86ab1]
              at net.minecraft.server.dedicated.DedicatedServer.e(DedicatedServer.java:195) ~[spigot-1.18.1-R0.1-SNAPSHOT.jar:3395-Spigot-f4ff00f-cc86ab1]
              at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:994) ~[spigot-1.18.1-R0.1-SNAPSHOT.jar:3395-Spigot-f4ff00f-cc86ab1]
              at net.minecraft.server.MinecraftServer.lambda$0(MinecraftServer.java:304) ~[spigot-1.18.1-R0.1-SNAPSHOT.jar:3395-Spigot-f4ff00f-cc86ab1]
              at java.lang.Thread.run(Thread.java:833) [?:?]
      

      The internal error when loading string in a YamlConfiguration with loadFromString is in reality a ClassCastException with the detail message class org.yaml.snakeyaml.events.CommentEvent cannot be cast to class org.yaml.snakeyaml.events.NodeEvent (org.yaml.snakeyaml.events.CommentEvent and org.yaml.snakeyaml.events.NodeEvent are in unnamed module of loader 'app').

      Disabling comment parsing fixes the issue. e.g., new YamlConfiguration().options().parseComments(false)

      and finally here is a simple test which fails:

      import static org.junit.jupiter.api.Assertions.assertNotNull;
      
      import org.bukkit.configuration.InvalidConfigurationException;
      import org.bukkit.configuration.file.YamlConfiguration;
      import org.jetbrains.annotations.NotNull;
      import org.jetbrains.annotations.Nullable;
      import org.junit.jupiter.api.Test;
      
      public class BadPointerYamlTest {
      
        @Nullable
        public static YamlConfiguration toConfig(@NotNull String contents, boolean parseComments) {
          var config = new YamlConfiguration();
          config.options().parseComments(parseComments);
           try {
            config.loadFromString(contents);
          } catch (InvalidConfigurationException e) {
            e.printStackTrace();
            return null;
          }
          return config;
        }
      
        @Test
        void name() {
          String okYAML = """
            dummy: test
            conf:
              - test #comment ok
            """;
          assertNotNull(toConfig(okYAML, false));
          assertNotNull(toConfig(okYAML, true));
      
          String badYAML = """
            dummy: &a test
            conf:
              - *a #comment not ok here
            """;
          assertNotNull(toConfig(badYAML, false));
          assertNotNull(toConfig(badYAML, true)); // <--- throws here
        }
      }
      

            Unassigned Unassigned
            kh498 kh498
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: