Skip to content

Instantly share code, notes, and snippets.

@TheBusyBiscuit
Created January 1, 2022 02:59
Show Gist options
  • Save TheBusyBiscuit/1cc621aff4e0f39d642abd5703dd96db to your computer and use it in GitHub Desktop.
Save TheBusyBiscuit/1cc621aff4e0f39d642abd5703dd96db to your computer and use it in GitHub Desktop.
Yaml comment overload demo
package io.github.thebusybiscuit.yamlcommentoverload;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor;
import org.yaml.snakeyaml.nodes.MappingNode;
import org.yaml.snakeyaml.representer.Representer;
/**
* Small demonstration on how too many comments can break a yaml file when
* set up in a certain way.
*
* @author TheBusyBiscuit
*
*/
public class CommentOverloadDemo {
public static void main(String[] args) throws IOException, URISyntaxException {
// Loading in our test yaml file
Path path = Paths.get(CommentOverloadDemo.class.getResource("/test.yml").toURI());
MappingNode node;
// Setting up our yaml instance to also process comments
LoaderOptions loaderOptions = new LoaderOptions();
loaderOptions.setProcessComments(true);
DumperOptions dumperOptions = new DumperOptions();
dumperOptions.setProcessComments(true);
Yaml yaml = new Yaml(new SafeConstructor(), new Representer(), dumperOptions, loaderOptions);
// Reading the yaml file
try (BufferedReader reader = Files.newBufferedReader(path)) {
node = (MappingNode) yaml.compose(reader);
}
// Serializing our contents
try (PrintWriter writer = new PrintWriter(System.out)) {
/*
* This will result in "java.lang.IllegalStateException: Queue full".
* As Yaml does not appear to be able to handle this many successive comments
* due to the internal event queue having a max size of 100.
* Every comment will also trigger a CommentEvent.
*/
yaml.serialize(node, writer);
}
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.thebusybiscuit</groupId>
<artifactId>yamlcommentoverload</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>YamlCommentOverload</name>
<description>A simple demo showcasing that yaml does not support more than 100 successive comments</description>
<build>
<sourceDirectory>${basedir}/src/main/java</sourceDirectory>
<resources>
<resource>
<filtering>false</filtering>
<directory>${basedir}/src/main/resources/</directory>
<includes>
<include>*</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>14</release>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.30</version>
</dependency>
</dependencies>
</project>
first-data: true
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
# Comments (x97)
second-data: true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment