Repository: decorators-squad/eo-yaml
Pull request #474 (2021-09-12 08:29:56)

#473 Fixed bug in YamlStream where top node comments were duplicated …
PR for #473

Fixed bug in YamlStream where top node comments were duplicated and doc comments ignored.


Patch:
diff --git a/src/main/java/com/amihaiemil/eoyaml/FirstCommentFound.java b/src/main/java/com/amihaiemil/eoyaml/FirstCommentFound.java
index ff33a72..25922d6 100644
--- a/src/main/java/com/amihaiemil/eoyaml/FirstCommentFound.java
+++ b/src/main/java/com/amihaiemil/eoyaml/FirstCommentFound.java
@@ -146,12 +146,10 @@ final class FirstCommentFound implements YamlLines {
                         if(line.trimmed().startsWith("#")) {
                             comment.add(line);
                         }
-                    } else {
+                    } else if (!line.trimmed().isEmpty()){
                         break;
                     }
                 }
-            }
-            if(comment.size() > 0) {
                 break;
             }
         }
diff --git a/src/main/java/com/amihaiemil/eoyaml/ReadYamlStream.java b/src/main/java/com/amihaiemil/eoyaml/ReadYamlStream.java
index 8cdc558..6d2995d 100644
--- a/src/main/java/com/amihaiemil/eoyaml/ReadYamlStream.java
+++ b/src/main/java/com/amihaiemil/eoyaml/ReadYamlStream.java
@@ -81,7 +81,6 @@ final class ReadYamlStream extends BaseYamlStream {
         );
         this.all = new Skip(
             lines,
-            line -> line.trimmed().startsWith("#"),
             line -> line.trimmed().startsWith("%")
         );
         this.guessIndentation = guessIndentation;
@@ -110,11 +109,17 @@ final class ReadYamlStream extends BaseYamlStream {
      */
     private YamlLines readDocument(final YamlLine start) {
         final List<YamlLine> yamlDocLines = new ArrayList<>();
-        final YamlLine startLine = this.all.line(start.number());
-        if(!"---".equals(startLine.trimmed())) {
-            yamlDocLines.add(startLine);
+        final YamlLines docComment = new Backwards(
+            new FirstCommentFound(
+                new Backwards(
+                    new Skip(this.all, (line) -> line.number() > start.number())
+                ), true
+            )
+        );
+        for (final YamlLine line: docComment){
+            yamlDocLines.add(line);
         }
-        for(final YamlLine line : this.all.original()) {
+        for(final YamlLine line : this.all) {
             if(line.number() > start.number()) {
                 final String current = line.trimmed();
                 if("---".equals(current) || "...".equals(current)) {


Test patch:
diff --git a/src/test/java/com/amihaiemil/eoyaml/ReadYamlStreamTest.java b/src/test/java/com/amihaiemil/eoyaml/ReadYamlStreamTest.java
index 0b107c8..c2f1639 100644
--- a/src/test/java/com/amihaiemil/eoyaml/ReadYamlStreamTest.java
+++ b/src/test/java/com/amihaiemil/eoyaml/ReadYamlStreamTest.java
@@ -31,8 +31,11 @@ import org.hamcrest.MatcherAssert;
 import org.hamcrest.Matchers;
 import org.junit.Test;
 
+import java.io.File;
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.stream.Collectors;
 
 /**
  * Unit tests for {@link ReadYamlStream}.
@@ -163,4 +166,51 @@ public final class ReadYamlStreamTest {
         );
         MatcherAssert.assertThat(stream.values(), Matchers.iterableWithSize(3));
     }
+
+    /**
+     * YamlStream should print the comment only once for each document/first
+     * node (in case doc starter is missing).
+     * <br/>
+     * Related to bug: <a href="https://github.com/decorators-squad/eo-yaml/issues/473">#473</a>.
+     * @throws IOException When something goes wrong.
+     */
+    @Test
+    public void shouldPrintTheFirstNodeOrDocumentCommentOnce()
+        throws IOException {
+        YamlStream yamlStream = Yaml
+            .createYamlInput(new File("src/test/resources"
+                + "/streamWithDocumentComments.yml"))
+            .readYamlStream();
+
+        List<YamlNode> collect = yamlStream.collect(Collectors.toList());
+
+        MatcherAssert.assertThat(collect, Matchers.iterableWithSize(4));
+
+        MatcherAssert.assertThat(
+            collect.get(0).comment().value(),
+            Matchers.isEmptyString()
+        );
+        MatcherAssert.assertThat(
+            collect.get(0).asMapping()
+                .value("architect")
+                .comment()
+                .value(),
+            Matchers.equalTo("Node comment")
+        );
+
+        MatcherAssert.assertThat(
+            collect.get(1).comment().value(),
+            Matchers.equalTo("Second whole document comment")
+        );
+
+        MatcherAssert.assertThat(
+            collect.get(2).comment().value(),
+            Matchers.equalTo("Third whole document trimmed")
+        );
+
+        MatcherAssert.assertThat(
+            collect.get(3).comment().value(),
+            Matchers.isEmptyString()
+        );
+    }
 }
diff --git a/src/test/resources/streamWithDocumentComments.yml b/src/test/resources/streamWithDocumentComments.yml
new file mode 100644
index 0000000..8dfd091
--- /dev/null
+++ b/src/test/resources/streamWithDocumentComments.yml
@@ -0,0 +1,28 @@
+#Node comment
+architect: mihai
+developers:
+  - rultor
+  - salikjan
+  - sherif
+name: eo-yaml
+
+
+
+#Second whole document comment
+
+
+
+---
+architect: vlad
+developers:
+  - andrei
+name: eo-json-impl
+#Third whole document trimmed
+---
+architect: felicia
+developer: sara
+name: docker-java-api
+---
+architect: john
+developer: dan
+name: docker-java-api