[Git][java-team/snakeyaml][upstream] New upstream version 1.29

Emmanuel Bourg (@ebourg) gitlab at salsa.debian.org
Sun Sep 26 21:19:22 BST 2021



Emmanuel Bourg pushed to branch upstream at Debian Java Maintainers / snakeyaml


Commits:
ee460f9b by Emmanuel Bourg at 2021-09-26T22:14:21+02:00
New upstream version 1.29
- - - - -


23 changed files:

- docker-run-jdk11.sh
- − docker-run-jdk14.sh
- + docker-run-jdk15.sh
- + docker-run-jdk16.sh
- docker-run-jdk7.sh
- docker-run-jdk8.sh
- pom.xml
- docker-run.sh → run-in-docker.sh
- src/changes/changes.xml
- src/main/java/org/yaml/snakeyaml/DumperOptions.java
- src/main/java/org/yaml/snakeyaml/Yaml.java
- src/main/java/org/yaml/snakeyaml/composer/Composer.java
- src/main/java/org/yaml/snakeyaml/emitter/Emitter.java
- src/main/java/org/yaml/snakeyaml/parser/ParserImpl.java
- src/main/java/org/yaml/snakeyaml/scanner/Constant.java
- src/main/java/org/yaml/snakeyaml/scanner/ScannerImpl.java
- src/test/java/org/yaml/snakeyaml/comment/ComposerWithCommentEnabledTest.java
- src/test/java/org/yaml/snakeyaml/comment/EmitterWithCommentEnabledTest.java
- src/test/java/org/yaml/snakeyaml/comment/ParserWithCommentEnabledTest.java
- + src/test/java/org/yaml/snakeyaml/comment/ProblematicYamlTest.java
- src/test/java/org/yaml/snakeyaml/comment/ScannerWithCommentEnabledTest.java
- src/test/java/org/yaml/snakeyaml/comment/SerializerWithCommentEnabledTest.java
- + src/test/java/org/yaml/snakeyaml/issues/issue500/FailIteratorOnlyWhenErrorFoundTest.java


Changes:

=====================================
docker-run-jdk11.sh
=====================================
@@ -1,2 +1,2 @@
 #!/usr/bin/env bash
-./docker-run.sh 11-slim -Pwith-java11-tests $@
+./run-in-docker.sh 3.6-jdk-11 -Pwith-java11-tests $@


=====================================
docker-run-jdk14.sh deleted
=====================================
@@ -1,2 +0,0 @@
-#!/usr/bin/env bash
-./docker-run.sh 14 -Pwith-java11-tests $@


=====================================
docker-run-jdk15.sh
=====================================
@@ -0,0 +1,2 @@
+#!/usr/bin/env bash
+./run-in-docker.sh 3.6.3-openjdk-15 -Pwith-java11-tests $@


=====================================
docker-run-jdk16.sh
=====================================
@@ -0,0 +1,2 @@
+#!/usr/bin/env bash
+./run-in-docker.sh 3.6.3-openjdk-16 -Pwith-java11-tests $@


=====================================
docker-run-jdk7.sh
=====================================
@@ -1,2 +1,2 @@
 #!/usr/bin/env bash
-./docker-run.sh 7 $@
+./run-in-docker.sh 3.6-jdk-7 $@


=====================================
docker-run-jdk8.sh
=====================================
@@ -1,2 +1,2 @@
 #!/usr/bin/env bash
-./docker-run.sh 8 -Pwith-java8-tests $@
+./run-in-docker.sh 3.6.3-jdk-8 -Pwith-java8-tests $@


=====================================
pom.xml
=====================================
@@ -3,7 +3,7 @@
     <modelVersion>4.0.0</modelVersion>
     <groupId>org.yaml</groupId>
     <artifactId>snakeyaml</artifactId>
-    <version>1.28</version>
+    <version>1.29</version>
     <packaging>bundle</packaging>
     <properties>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@@ -38,7 +38,7 @@
         <connection>scm:git:http://bitbucket.org/asomov/snakeyaml</connection>
         <developerConnection>scm:git:ssh://git@bitbucket.org/asomov/snakeyaml</developerConnection>
         <url>https://bitbucket.org/asomov/snakeyaml/src</url>
-        <tag>snakeyaml-1.28</tag>
+        <tag>snakeyaml-1.29</tag>
     </scm>
     <licenses>
         <license>
@@ -71,18 +71,6 @@
             <version>4.13.1</version>
             <scope>test</scope>
         </dependency>
-        <dependency>
-            <groupId>org.apache.commons</groupId>
-            <artifactId>commons-lang3</artifactId>
-            <version>3.4</version>
-            <scope>test</scope>
-        </dependency>
-        <dependency>
-            <groupId>commons-io</groupId>
-            <artifactId>commons-io</artifactId>
-            <version>2.5</version>
-            <scope>test</scope>
-        </dependency>
         <dependency>
             <groupId>org.apache.velocity</groupId>
             <artifactId>velocity</artifactId>


=====================================
docker-run.sh → run-in-docker.sh
=====================================
@@ -5,6 +5,6 @@ docker run --rm -it               \
     -v ~:/my-home                 \
     -e "HOME=/my-home"            \
     -w /work                      \
-    maven:3.6-jdk-$1            \
+    maven:$1                      \
     mvn -Dmaven.repo.local=/my-home/.m2/repository clean test ${@:2}
 


=====================================
src/changes/changes.xml
=====================================
@@ -5,7 +5,21 @@
         <title>YAML 1.1 parser and emitter</title>
     </properties>
     <body>
-        <release version="1.28" date="in GIT" description="Maintenance">
+        <release version="1.29" date="in GIT" description="Maintenance">
+            <action dev="Wolf2323" type="fix">
+                Fix Events ordering and improve Node-Comment association (2021-05-13)
+            </action>
+            <action dev="asomov" type="fix">
+                Fix corner case for comments where blank lines after plain token  (2021-03-02)
+            </action>
+            <action dev="asomov" type="add">
+                Add a test for issue 500 (2021-03-14)
+            </action>
+            <action dev="asomov" type="remove">
+                Remove unused variable (2021-02-28)
+            </action>
+        </release>
+        <release version="1.28" date="2021-02-22" description="Maintenance">
             <action dev="maslovalex" type="fix">
                 Fix tests to work on Windows (2021-02-19)
             </action>


=====================================
src/main/java/org/yaml/snakeyaml/DumperOptions.java
=====================================
@@ -195,6 +195,7 @@ public class DumperOptions {
     private boolean explicitEnd = false;
     private TimeZone timeZone = null;
     private int maxSimpleKeyLength = 128;
+    private boolean processComments = false;
     private NonPrintableStyle  nonPrintableStyle = NonPrintableStyle.BINARY;
 
     private Version version = null;
@@ -462,6 +463,20 @@ public class DumperOptions {
         this.maxSimpleKeyLength = maxSimpleKeyLength;
     }
 
+    /**
+     * Set the comment processing. By default comments are ignored.
+     *
+     * @param processComments <code>true</code> to process; <code>false</code> to ignore</code>
+     */
+
+    public void setProcessComments(boolean processComments) {
+        this.processComments = processComments;
+    }
+
+    public boolean isProcessComments() {
+        return processComments;
+    }
+
     public NonPrintableStyle getNonPrintableStyle() {
         return this.nonPrintableStyle;
     }


=====================================
src/main/java/org/yaml/snakeyaml/Yaml.java
=====================================
@@ -485,7 +485,8 @@ public class Yaml {
     }
 
     private Object loadFromReader(StreamReader sreader, Class<?> type) {
-        Composer composer = new Composer(new ParserImpl(sreader), resolver, loadingConfig);
+        Composer composer = new Composer(new ParserImpl(sreader,
+                loadingConfig.isProcessComments()), resolver, loadingConfig);
         constructor.setComposer(composer);
         return constructor.getSingleData(type);
     }
@@ -499,7 +500,8 @@ public class Yaml {
      * sequence
      */
     public Iterable<Object> loadAll(Reader yaml) {
-        Composer composer = new Composer(new ParserImpl(new StreamReader(yaml)), resolver, loadingConfig);
+        Composer composer = new Composer(new ParserImpl(new StreamReader(yaml),
+                loadingConfig.isProcessComments()), resolver, loadingConfig);
         constructor.setComposer(composer);
         Iterator<Object> result = new Iterator<Object>() {
             @Override
@@ -667,7 +669,8 @@ public class Yaml {
      * @see <a href="http://yaml.org/spec/1.1/#id859333">Processing Overview</a>
      */
     public Iterable<Event> parse(Reader yaml) {
-        final Parser parser = new ParserImpl(new StreamReader(yaml));
+        final Parser parser = new ParserImpl(new StreamReader(yaml),
+                loadingConfig.isProcessComments());
         Iterator<Event> result = new Iterator<Event>() {
             @Override
             public boolean hasNext() {


=====================================
src/main/java/org/yaml/snakeyaml/composer/Composer.java
=====================================
@@ -112,7 +112,7 @@ public class Composer {
         // Drop the DOCUMENT-START event.
         parser.getEvent();
         // Compose the root node.
-        Node node = composeNode(null, blockCommentsCollector.collectEvents().consume());
+        Node node = composeNode(null);
         // Drop the DOCUMENT-END event.
         blockCommentsCollector.collectEvents();
         if(!blockCommentsCollector.isEmpty()) {
@@ -136,19 +136,11 @@ public class Composer {
     public Node getSingleNode() {
         // Drop the STREAM-START event.
         parser.getEvent();
-        // Drop any leading comments (though should not have run this case with comments on)
-        while (parser.checkEvent(Event.ID.Comment)) {
-            parser.getEvent();
-        }
         // Compose a document if the stream is not empty.
         Node document = null;
         if (!parser.checkEvent(Event.ID.StreamEnd)) {
             document = getNode();
         }
-        // Drop any trailing comments (though should not have run this case with comments on)
-        while (parser.checkEvent(Event.ID.Comment)) {
-            parser.getEvent();
-        }
         // Ensure that the stream contains no more documents.
         if (!parser.checkEvent(Event.ID.StreamEnd)) {
             Event event = parser.getEvent();
@@ -161,7 +153,8 @@ public class Composer {
         return document;
     }
 
-    private Node composeNode(Node parent, List<CommentLine> blockComments) {
+    private Node composeNode(Node parent) {
+        blockCommentsCollector.collectEvents();
         if (parent != null)
             recursiveNodes.add(parent);
         final Node node;
@@ -182,17 +175,17 @@ public class Composer {
             if (recursiveNodes.remove(node)) {
                 node.setTwoStepsConstruction(true);
             }
-            node.setBlockComments(blockComments);
+            node.setBlockComments(blockCommentsCollector.consume());
         } else {
             NodeEvent event = (NodeEvent) parser.peekEvent();
             String anchor = event.getAnchor();
             // the check for duplicate anchors has been removed (issue 174)
             if (parser.checkEvent(Event.ID.Scalar)) {
-                node = composeScalarNode(anchor, blockComments);
+                node = composeScalarNode(anchor, blockCommentsCollector.consume());
             } else if (parser.checkEvent(Event.ID.SequenceStart)) {
-                node = composeSequenceNode(anchor, blockComments);
+                node = composeSequenceNode(anchor);
             } else {
-                node = composeMappingNode(anchor, blockComments);
+                node = composeMappingNode(anchor);
             }
         }
         recursiveNodes.remove(parent);
@@ -222,7 +215,7 @@ public class Composer {
         return node;
     }
 
-    protected Node composeSequenceNode(String anchor, List<CommentLine> blockComments) {
+    protected Node composeSequenceNode(String anchor) {
         SequenceStartEvent startEvent = (SequenceStartEvent) parser.getEvent();
         String tag = startEvent.getTag();
         Tag nodeTag;
@@ -237,18 +230,22 @@ public class Composer {
         final ArrayList<Node> children = new ArrayList<Node>();
         SequenceNode node = new SequenceNode(nodeTag, resolved, children, startEvent.getStartMark(),
                 null, startEvent.getFlowStyle());
+        if (startEvent.isFlow()) {
+            node.setBlockComments(blockCommentsCollector.consume());
+        }
         if (anchor != null) {
             node.setAnchor(anchor);
             anchors.put(anchor, node);
         }
-        node.setBlockComments(blockComments);
-        node.setInLineComments(inlineCommentsCollector.collectEvents().consume());
         while (!parser.checkEvent(Event.ID.SequenceEnd)) {
             blockCommentsCollector.collectEvents();
             if (parser.checkEvent(Event.ID.SequenceEnd)) {
                 break;
             }
-            children.add(composeNode(node, blockCommentsCollector.consume()));
+            children.add(composeNode(node));
+        }
+        if (startEvent.isFlow()) {
+            node.setInLineComments(inlineCommentsCollector.collectEvents().consume());
         }
         Event endEvent = parser.getEvent();
         node.setEndMark(endEvent.getEndMark());
@@ -259,7 +256,7 @@ public class Composer {
         return node;
     }
 
-    protected Node composeMappingNode(String anchor, List<CommentLine> blockComments) {
+    protected Node composeMappingNode(String anchor) {
         MappingStartEvent startEvent = (MappingStartEvent) parser.getEvent();
         String tag = startEvent.getTag();
         Tag nodeTag;
@@ -274,18 +271,22 @@ public class Composer {
         final List<NodeTuple> children = new ArrayList<NodeTuple>();
         MappingNode node = new MappingNode(nodeTag, resolved, children, startEvent.getStartMark(),
                 null, startEvent.getFlowStyle());
+        if (startEvent.isFlow()) {
+            node.setBlockComments(blockCommentsCollector.consume());
+        }
         if (anchor != null) {
             node.setAnchor(anchor);
             anchors.put(anchor, node);
         }
-        node.setBlockComments(blockComments);
-        node.setInLineComments(inlineCommentsCollector.collectEvents().consume());
         while (!parser.checkEvent(Event.ID.MappingEnd)) {
             blockCommentsCollector.collectEvents();
             if (parser.checkEvent(Event.ID.MappingEnd)) {
                 break;
             }
-            composeMappingChildren(children, node, blockCommentsCollector.consume());
+            composeMappingChildren(children, node);
+        }
+        if (startEvent.isFlow()) {
+            node.setInLineComments(inlineCommentsCollector.collectEvents().consume());
         }
         Event endEvent = parser.getEvent();
         node.setEndMark(endEvent.getEndMark());
@@ -296,20 +297,20 @@ public class Composer {
         return node;
     }
 
-    protected void composeMappingChildren(List<NodeTuple> children, MappingNode node, List<CommentLine> keyBlockComments) {
-        Node itemKey = composeKeyNode(node, keyBlockComments);
+    protected void composeMappingChildren(List<NodeTuple> children, MappingNode node) {
+        Node itemKey = composeKeyNode(node);
         if (itemKey.getTag().equals(Tag.MERGE)) {
             node.setMerged(true);
         }
-        Node itemValue = composeValueNode(node, blockCommentsCollector.collectEvents().consume());
+        Node itemValue = composeValueNode(node);
         children.add(new NodeTuple(itemKey, itemValue));
     }
 
-    protected Node composeKeyNode(MappingNode node, List<CommentLine> blockComments) {
-        return composeNode(node, blockComments);
+    protected Node composeKeyNode(MappingNode node) {
+        return composeNode(node);
     }
 
-    protected Node composeValueNode(MappingNode node, List<CommentLine> blockComments) {
-        return composeNode(node, blockComments);
+    protected Node composeValueNode(MappingNode node) {
+        return composeNode(node);
     }
 }


=====================================
src/main/java/org/yaml/snakeyaml/emitter/Emitter.java
=====================================
@@ -161,6 +161,7 @@ public final class Emitter implements Emitable {
     private final char[] bestLineBreak;
     private final boolean splitLines;
     private final int maxSimpleKeyLength;
+    private final boolean emitComments;
 
     // Tag prefixes.
     private Map<String, String> tagPrefixes;
@@ -227,6 +228,7 @@ public final class Emitter implements Emitable {
         this.bestLineBreak = opts.getLineBreak().getString().toCharArray();
         this.splitLines = opts.getSplitLines();
         this.maxSimpleKeyLength = opts.getMaxSimpleKeyLength();
+        this.emitComments = opts.isProcessComments();
 
         // Tag prefixes.
         this.tagPrefixes = new LinkedHashMap<String, String>();
@@ -684,7 +686,6 @@ public final class Emitter implements Emitable {
                 state = states.pop();
             } else if( event instanceof CommentEvent) {
                 blockCommentsCollector.collectEvents(event);
-                writeBlockComment();
             } else {
                 writeIndent();
                 if (!indentWithIndicator || this.first) {
@@ -694,6 +695,17 @@ public final class Emitter implements Emitable {
                 if (indentWithIndicator && this.first) {
                     indent += indicatorIndent;
                 }
+                if (!blockCommentsCollector.isEmpty()) {
+                    increaseIndent(false, false);
+                    writeBlockComment();
+                    if(event instanceof ScalarEvent) {
+                        analysis = analyzeScalar(((ScalarEvent)event).getValue());
+                        if (!analysis.isEmpty()) {
+                            writeIndent();
+                        }
+                    }
+                    indent = indents.pop();
+                }
                 states.push(new ExpectBlockSequenceItem(false));
                 expectNode(false, false, false);
                 inlineCommentsCollector.collectEvents();
@@ -1432,23 +1444,25 @@ public final class Emitter implements Emitable {
     }
     
     private boolean writeCommentLines(List<CommentLine> commentLines) throws IOException {
-        int indentColumns = 0;
-        boolean firstComment = true;
         boolean wroteComment = false;
-        for(CommentLine commentLine : commentLines) {
-            if(commentLine.getCommentType() != CommentType.BLANK_LINE) {
-                if(firstComment) {
-                    firstComment = false;
-                    writeIndicator("#", commentLine.getCommentType() == CommentType.IN_LINE, false, false);
-                    indentColumns = this.column > 0 ? this.column - 1 : 0;
-                } else {
-                    writeWhitespace(indentColumns);
-                    writeIndicator("#", false, false, false);
+        if(emitComments) {
+            int indentColumns = 0;
+            boolean firstComment = true;
+            for (CommentLine commentLine : commentLines) {
+                if (commentLine.getCommentType() != CommentType.BLANK_LINE) {
+                    if (firstComment) {
+                        firstComment = false;
+                        writeIndicator("#", commentLine.getCommentType() == CommentType.IN_LINE, false, false);
+                        indentColumns = this.column > 0 ? this.column - 1 : 0;
+                    } else {
+                        writeWhitespace(indentColumns);
+                        writeIndicator("#", false, false, false);
+                    }
+                    stream.write(commentLine.getValue());
                 }
-                stream.write(commentLine.getValue());
+                writeLineBreak(null);
+                wroteComment = true;
             }
-            writeLineBreak(null);
-            wroteComment = true;
         }
         return wroteComment;
     }
@@ -1456,8 +1470,8 @@ public final class Emitter implements Emitable {
     private void writeBlockComment() throws IOException {
         if(!blockCommentsCollector.isEmpty()) {
             writeIndent();
+            writeCommentLines(blockCommentsCollector.consume());
         }
-        writeCommentLines(blockCommentsCollector.consume());
     }
 
     private boolean writeInlineComments() throws IOException {


=====================================
src/main/java/org/yaml/snakeyaml/parser/ParserImpl.java
=====================================
@@ -185,7 +185,7 @@ public class ParserImpl implements Parser {
         CommentType type = token.getCommentType();
         
         // state = state, that no change in state
-        
+
         return new CommentEvent(type, value, startMark, endMark);
     }
 
@@ -463,7 +463,7 @@ public class ParserImpl implements Parser {
                 endMark = scanner.peekToken().getEndMark();
                 event = new SequenceStartEvent(anchor, tag, implicit, startMark, endMark,
                         DumperOptions.FlowStyle.BLOCK);
-                state = new ParseIndentlessSequenceEntry();
+                state = new ParseIndentlessSequenceEntryKey();
             } else {
                 if (scanner.checkToken(Token.ID.Scalar)) {
                     ScalarToken token = (ScalarToken) scanner.getToken();
@@ -531,24 +531,18 @@ public class ParserImpl implements Parser {
         public Event produce() {
             Token token = scanner.getToken();
             marks.push(token.getStartMark());
-            return new ParseBlockSequenceEntry().produce();
+            return new ParseBlockSequenceEntryKey().produce();
         }
     }
 
-    private class ParseBlockSequenceEntry implements Production {
+    private class ParseBlockSequenceEntryKey implements Production {
         public Event produce() {
             if (scanner.checkToken(Token.ID.Comment)) {
                 return produceCommentEvent((CommentToken) scanner.getToken());
             }
             if (scanner.checkToken(Token.ID.BlockEntry)) {
                 BlockEntryToken token = (BlockEntryToken) scanner.getToken();
-                if (!scanner.checkToken(Token.ID.BlockEntry, Token.ID.BlockEnd)) {
-                    states.push(new ParseBlockSequenceEntry());
-                    return new ParseBlockNode().produce();
-                } else {
-                    state = new ParseBlockSequenceEntry();
-                    return processEmptyScalar(token.getEndMark());
-                }
+                return new ParseBlockSequenceEntryValue(token).produce();
             }
             if (!scanner.checkToken(Token.ID.BlockEnd)) {
                 Token token = scanner.peekToken();
@@ -564,23 +558,38 @@ public class ParserImpl implements Parser {
         }
     }
 
+    private class ParseBlockSequenceEntryValue implements Production {
+        BlockEntryToken token;
+
+        public ParseBlockSequenceEntryValue(final BlockEntryToken token) {
+            this.token = token;
+        }
+
+        public Event produce() {
+            if(scanner.checkToken(Token.ID.Comment)) {
+                state = new ParseBlockSequenceEntryValue(token);
+                return produceCommentEvent((CommentToken) scanner.getToken());
+            }
+            if (!scanner.checkToken(Token.ID.BlockEntry, Token.ID.BlockEnd)) {
+                states.push(new ParseBlockSequenceEntryKey());
+                return new ParseBlockNode().produce();
+            } else {
+                state = new ParseBlockSequenceEntryKey();
+                return processEmptyScalar(token.getEndMark());
+            }
+        }
+    }
+
     // indentless_sequence ::= (BLOCK-ENTRY block_node?)+
 
-    private class ParseIndentlessSequenceEntry implements Production {
+    private class ParseIndentlessSequenceEntryKey implements Production {
         public Event produce() {
             if (scanner.checkToken(Token.ID.Comment)) {
                 return produceCommentEvent((CommentToken) scanner.getToken());
             }
             if (scanner.checkToken(Token.ID.BlockEntry)) {
-                Token token = scanner.getToken();
-                if (!scanner.checkToken(Token.ID.BlockEntry, Token.ID.Key, Token.ID.Value,
-                        Token.ID.BlockEnd)) {
-                    states.push(new ParseIndentlessSequenceEntry());
-                    return new ParseBlockNode().produce();
-                } else {
-                    state = new ParseIndentlessSequenceEntry();
-                    return processEmptyScalar(token.getEndMark());
-                }
+                BlockEntryToken token = (BlockEntryToken) scanner.getToken();
+                return new ParseIndentlessSequenceEntryValue(token).produce();
             }
             Token token = scanner.peekToken();
             Event event = new SequenceEndEvent(token.getStartMark(), token.getEndMark());
@@ -589,6 +598,29 @@ public class ParserImpl implements Parser {
         }
     }
 
+    private class ParseIndentlessSequenceEntryValue implements Production {
+        BlockEntryToken token;
+
+        public ParseIndentlessSequenceEntryValue(final BlockEntryToken token) {
+            this.token = token;
+        }
+
+        public Event produce() {
+            if(scanner.checkToken(Token.ID.Comment)) {
+                state = new ParseIndentlessSequenceEntryValue(token);
+                return produceCommentEvent((CommentToken) scanner.getToken());
+            }
+            if (!scanner.checkToken(Token.ID.BlockEntry, Token.ID.Key, Token.ID.Value,
+                    Token.ID.BlockEnd)) {
+                states.push(new ParseIndentlessSequenceEntryKey());
+                return new ParseBlockNode().produce();
+            } else {
+                state = new ParseIndentlessSequenceEntryKey();
+                return processEmptyScalar(token.getEndMark());
+            }
+        }
+    }
+
     private class ParseBlockMappingFirstKey implements Production {
         public Event produce() {
             Token token = scanner.getToken();


=====================================
src/main/java/org/yaml/snakeyaml/scanner/Constant.java
=====================================
@@ -29,7 +29,6 @@ public final class Constant {
     private final static String URI_CHARS_S = ALPHA_S + "-;/?:@&=+$,_.!~*\'()[]%";
 
     public final static Constant LINEBR = new Constant(LINEBR_S);
-    public final static Constant FULL_LINEBR = new Constant(FULL_LINEBR_S);
     public final static Constant NULL_OR_LINEBR = new Constant(NULL_OR_LINEBR_S);
     public final static Constant NULL_BL_LINEBR = new Constant(NULL_BL_LINEBR_S);
     public final static Constant NULL_BL_T_LINEBR = new Constant(NULL_BL_T_LINEBR_S);


=====================================
src/main/java/org/yaml/snakeyaml/scanner/ScannerImpl.java
=====================================
@@ -1219,7 +1219,7 @@ public final class ScannerImpl implements Scanner {
             if (reader.peek() == '#') {
                 commentSeen = true;
                 CommentType type;
-                if(startMark.getColumn() != 0) {
+                if(startMark.getColumn() != 0 && !(startMark.getColumn() == (indent + ff) && reader.peek(-ff - 1) == '-')) {
                     type = CommentType.IN_LINE;
                     inlineStartColumn = reader.getColumn();
                 } else if(inlineStartColumn == reader.getColumn()) {
@@ -2081,6 +2081,46 @@ public final class ScannerImpl implements Scanner {
         return new ScalarToken(chunks.toString(), startMark, endMark, true);
     }
 
+    // Helper for scanPlainSpaces method when comments are enabled.
+    // The ensures that blank lines and comments following a multi-line plain token are not swallowed up
+    private boolean atEndOfPlain() {
+        // peak ahead to find end of whitespaces and the column at which it occurs
+        int wsLength = 0;
+        int wsColumn = this.reader.getColumn();
+        {
+            int c;
+            while ((c = reader.peek(wsLength)) != '\0' && Constant.NULL_BL_T_LINEBR.has(c)) {
+                wsLength++;
+                if (!Constant.LINEBR.has(c) && (c != '\r' || reader.peek(wsLength + 1) != '\n') && c != 0xFEFF) {
+                    wsColumn++;
+                } else {
+                    wsColumn = 0;
+                }
+            }
+        }
+
+        // if we see, a comment or end of string or change decrease in indent, we are done
+        // Do not chomp end of lines and blanks, they will be handled by the main loop.
+        if (reader.peek(wsLength) == '#' || reader.peek(wsLength + 1) == '\0'
+                || this.flowLevel == 0 && wsColumn < this.indent) {
+            return true;
+        }
+
+        // if we see, after the space, a key-value followed by a ':', we are done
+        // Do not chomp end of lines and blanks, they will be handled by the main loop.
+        if (this.flowLevel == 0) {
+            int c;
+            for(int extra = 1; (c = reader.peek(wsLength + extra)) != 0 && !Constant.NULL_BL_T_LINEBR.has(c); extra++) {
+                if (c == ':' && Constant.NULL_BL_T_LINEBR.has(reader.peek(wsLength + extra + 1))) {
+                    return true;
+                }
+            }
+        }
+
+        // None of the above so safe to chomp the spaces.
+        return false;
+    }
+
     /**
      * See the specification for details. SnakeYAML and libyaml allow tabs
      * inside plain scalar
@@ -2099,6 +2139,9 @@ public final class ScannerImpl implements Scanner {
                     && Constant.NULL_BL_T_LINEBR.has(reader.peek(3))) {
                 return "";
             }
+            if(emitComments && atEndOfPlain()) {
+                return "";
+            }
             StringBuilder breaks = new StringBuilder();
             while (true) {
                 if (reader.peek() == ' ') {
@@ -2241,7 +2284,7 @@ public final class ScannerImpl implements Scanner {
      * 
      * FIXME This method fails for more than 256 bytes' worth of URI-encoded
      * characters in a row. Is this possible? Is this a use-case?
-     * 
+     *
      * @see <a href="http://www.ietf.org/rfc/rfc2396.txt">section 2.4, Escaped Encoding</a>
      */
     private String scanUriEscapes(String name, Mark startMark) {


=====================================
src/test/java/org/yaml/snakeyaml/comment/ComposerWithCommentEnabledTest.java
=====================================
@@ -139,7 +139,7 @@ public class ComposerWithCommentEnabledTest {
         return nodeList;
     }
 
-    private void assertNodesEqual(String[] expecteds, List<Node> nodeList) {
+    private void assertNodesEqual(String[] expected, List<Node> nodeList) {
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         boolean first = true;
         try (PrintStream out = new PrintStream(baos)) {
@@ -154,10 +154,10 @@ public class ComposerWithCommentEnabledTest {
         }
         String actualString = baos.toString();
         String[] actuals = actualString.split("\n");
-        for (int ix = 0; ix < Math.min(expecteds.length, actuals.length); ix++) {
-            assertEquals(expecteds[ix], actuals[ix]);
+        for (int ix = 0; ix < Math.min(expected.length, actuals.length); ix++) {
+            assertEquals(expected[ix], actuals[ix]);
         }
-        assertEquals(expecteds.length, actuals.length);
+        assertEquals(expected.length, actuals.length);
     }
 
     public Composer newComposerWithCommentsEnabled(String data) {
@@ -167,7 +167,7 @@ public class ComposerWithCommentEnabledTest {
     @Test
     public void testEmpty() {
         String data = "";
-        String[] expecteds = new String[]{ //
+        String[] expected = new String[]{ //
                 "" //
         };
 
@@ -175,13 +175,13 @@ public class ComposerWithCommentEnabledTest {
         List<Node> result = getNodeList(sut);
 
         printNodeList(result);
-        assertNodesEqual(expecteds, result);
+        assertNodesEqual(expected, result);
     }
 
     @Test
     public void testParseWithOnlyComment() {
         String data = "# Comment";
-        String[] expecteds = new String[]{ //
+        String[] expected = new String[]{ //
                 "Block Comment", //
                 "MappingNode", //
         };
@@ -190,7 +190,7 @@ public class ComposerWithCommentEnabledTest {
         List<Node> result = getNodeList(sut);
 
         printNodeList(result);
-        assertNodesEqual(expecteds, result);
+        assertNodesEqual(expected, result);
     }
 
     @Test
@@ -199,7 +199,7 @@ public class ComposerWithCommentEnabledTest {
                 "key: # Comment\n" + //
                 "  value\n";
 
-        String[] expecteds = new String[]{ //
+        String[] expected = new String[]{ //
                 "MappingNode", //
                 "    Tuple", //
                 "        ScalarNode: key", //
@@ -211,7 +211,7 @@ public class ComposerWithCommentEnabledTest {
         List<Node> result = getNodeList(sut);
 
         printNodeList(result);
-        assertNodesEqual(expecteds, result);
+        assertNodesEqual(expected, result);
     }
 
     @Test
@@ -222,20 +222,21 @@ public class ComposerWithCommentEnabledTest {
                 "  value\n" + //
                 "\n";
 
-        String[] expecteds = new String[]{ //
+        String[] expected = new String[]{ //
                 "MappingNode", //
                 "    Tuple", //
                 "        ScalarNode: key", //
                 "            InLine Comment", //
                 "            InLine Comment", //
-                "        ScalarNode: value" //
+                "        ScalarNode: value", //
+                "End Comment" //
         };
 
         Composer sut = newComposerWithCommentsEnabled(data);
         List<Node> result = getNodeList(sut);
 
         printNodeList(result);
-        assertNodesEqual(expecteds, result);
+        assertNodesEqual(expected, result);
     }
 
     @Test
@@ -243,7 +244,7 @@ public class ComposerWithCommentEnabledTest {
         String data = "" + //
                 "\n";
 
-        String[] expecteds = new String[]{ //
+        String[] expected = new String[]{ //
                 "Block Comment", //
                 "MappingNode", //
         };
@@ -252,7 +253,7 @@ public class ComposerWithCommentEnabledTest {
         List<Node> result = getNodeList(sut);
 
         printNodeList(result);
-        assertNodesEqual(expecteds, result);
+        assertNodesEqual(expected, result);
     }
 
     @Test
@@ -263,10 +264,10 @@ public class ComposerWithCommentEnabledTest {
                 "\n" + //
                 "\n";
 
-        String[] expecteds = new String[]{ //
-                "Block Comment", //
+        String[] expected = new String[]{ //
                 "MappingNode", //
                 "    Tuple", //
+                "        Block Comment", //
                 "        ScalarNode: abc", //
                 "        ScalarNode: def", //
                 "            InLine Comment", //
@@ -278,7 +279,7 @@ public class ComposerWithCommentEnabledTest {
         List<Node> result = getNodeList(sut);
 
         printNodeList(result);
-        assertNodesEqual(expecteds, result);
+        assertNodesEqual(expected, result);
     }
 
     @Test
@@ -289,7 +290,7 @@ public class ComposerWithCommentEnabledTest {
                 "    hij\n" + //
                 "\n";
 
-        String[] expecteds = new String[]{ //
+        String[] expected = new String[]{ //
                 "MappingNode", //
                 "    Tuple", //
                 "        ScalarNode: abc", //
@@ -301,14 +302,14 @@ public class ComposerWithCommentEnabledTest {
         List<Node> result = getNodeList(sut);
 
         printNodeList(result);
-        assertNodesEqual(expecteds, result);
+        assertNodesEqual(expected, result);
     }
 
     @Test
     public void testDirectiveLineEndComment() {
         String data = "%YAML 1.1 #Comment\n";
 
-        String[] expecteds = new String[]{ //
+        String[] expected = new String[]{ //
                 "" //
         };
 
@@ -316,7 +317,7 @@ public class ComposerWithCommentEnabledTest {
         List<Node> result = getNodeList(sut);
 
         printNodeList(result);
-        assertNodesEqual(expecteds, result);
+        assertNodesEqual(expected, result);
     }
 
     @Test
@@ -328,14 +329,14 @@ public class ComposerWithCommentEnabledTest {
                 "- item # InlineComment2\n" + //
                 "# Comment\n";
 
-        String[] expecteds = new String[]{ //
-                "Block Comment", //
+        String[] expected = new String[]{ //
                 "MappingNode", //
                 "    Tuple", //
+                "        Block Comment", //
                 "        ScalarNode: list", //
                 "            InLine Comment", //
-                "        Block Comment", //
                 "        SequenceNode", //
+                "            Block Comment", //
                 "            ScalarNode: item", //
                 "                InLine Comment", //
                 "End Comment" //
@@ -345,7 +346,7 @@ public class ComposerWithCommentEnabledTest {
         List<Node> result = getNodeList(sut);
 
         printNodeList(result);
-        assertNodesEqual(expecteds, result);
+        assertNodesEqual(expected, result);
     }
 
     @Test
@@ -370,11 +371,11 @@ public class ComposerWithCommentEnabledTest {
                 "# Block Comment7\n" + //
                 "";
 
-        String[] expecteds = new String[]{ //
-                "Block Comment", //
-                "Block Comment", //
+        String[] expected = new String[]{ //
                 "MappingNode", //
                 "    Tuple", //
+                "        Block Comment", //
+                "        Block Comment", //
                 "        ScalarNode: key", //
                 "            InLine Comment", //
                 "            InLine Comment", //
@@ -387,8 +388,8 @@ public class ComposerWithCommentEnabledTest {
                 "        ScalarNode: list", //
                 "            InLine Comment", //
                 "            InLine Comment", //
-                "        Block Comment", //
                 "        SequenceNode", //
+                "            Block Comment", //
                 "            ScalarNode: item1", //
                 "                InLine Comment", //
                 "            MappingNode", //
@@ -421,7 +422,7 @@ public class ComposerWithCommentEnabledTest {
         List<Node> result = getNodeList(sut);
 
         printNodeList(result);
-        assertNodesEqual(expecteds, result);
+        assertNodesEqual(expected, result);
     }
 
     @Test
@@ -437,17 +438,17 @@ public class ComposerWithCommentEnabledTest {
                 "# Block Comment4\n" + //
                 "";
 
-        String[] expecteds = new String[]{ //
-                "Block Comment", //
-                "Block Comment", //
+        String[] expected = new String[]{ //
                 "SequenceNode", //
+                "    Block Comment", //
+                "    Block Comment", //
                 "    ScalarNode: item1", //
                 "        InLine Comment", //
                 "        InLine Comment", //
-                "    Block Comment", //
-                "    Block Comment", //
                 "    MappingNode", //
                 "        Tuple", //
+                "            Block Comment", //
+                "            Block Comment", //
                 "            ScalarNode: item2", //
                 "            ScalarNode: value", //
                 "                InLine Comment", //
@@ -458,7 +459,7 @@ public class ComposerWithCommentEnabledTest {
         List<Node> result = getNodeList(sut);
 
         printNodeList(result);
-        assertNodesEqual(expecteds, result);
+        assertNodesEqual(expected, result);
     }
 
     @Test
@@ -469,7 +470,7 @@ public class ComposerWithCommentEnabledTest {
                 "# Block Comment2\n" + //
                 "";
 
-        String[] expecteds = new String[]{ //
+        String[] expected = new String[]{ //
                 "Block Comment", //
                 "SequenceNode", //
                 "    ScalarNode: item1", //
@@ -489,7 +490,7 @@ public class ComposerWithCommentEnabledTest {
         List<Node> result = getNodeList(sut);
 
         printNodeList(result);
-        assertNodesEqual(expecteds, result);
+        assertNodesEqual(expected, result);
     }
 
     @Test
@@ -499,9 +500,39 @@ public class ComposerWithCommentEnabledTest {
                 "abc: def # commment\n" + //
                 "\n" + //
                 "\n";
-        String[] expecteds = new String[]{ //
+        String[] expected = new String[]{ //
                 "MappingNode", //
                 "    Tuple", //
+                "        Block Comment",
+                "        ScalarNode: abc", //
+                "        ScalarNode: def", //
+                "            InLine Comment", //
+                "End Comment", //
+                "End Comment", //
+        };
+
+        Composer sut = newComposerWithCommentsEnabled(data);
+        List<Node> result = Arrays.asList(sut.getSingleNode());
+
+        printNodeList(result);
+        assertNodesEqual(expected, result);
+    }
+
+    @Test
+    public void testGetSingleNodeHeaderComment() {
+        String data = "" + //
+                "\n" + //
+                "# Block Comment1\n" + //
+                "# Block Comment2\n" + //
+                "abc: def # commment\n" + //
+                "\n" + //
+                "\n";
+        String[] expected = new String[]{ //
+                "MappingNode", //
+                "    Tuple", //
+                "        Block Comment", //
+                "        Block Comment", //
+                "        Block Comment", //
                 "        ScalarNode: abc", //
                 "        ScalarNode: def", //
                 "            InLine Comment", //
@@ -513,7 +544,7 @@ public class ComposerWithCommentEnabledTest {
         List<Node> result = Arrays.asList(sut.getSingleNode());
 
         printNodeList(result);
-        assertNodesEqual(expecteds, result);
+        assertNodesEqual(expected, result);
     }
 
     private static class TestConstructor extends SafeConstructor {


=====================================
src/test/java/org/yaml/snakeyaml/comment/EmitterWithCommentEnabledTest.java
=====================================
@@ -39,6 +39,7 @@ public class EmitterWithCommentEnabledTest {
         DumperOptions options = new DumperOptions();
         options.setDefaultScalarStyle(ScalarStyle.PLAIN);
         options.setDefaultFlowStyle(FlowStyle.BLOCK);
+        options.setProcessComments(true);
         Serializer serializer = new Serializer(new Emitter(output, options), new Resolver(), options, null);
 
         serializer.open();
@@ -142,8 +143,8 @@ public class EmitterWithCommentEnabledTest {
         String data = "" + //
                 "# Comment\n" + //
                 "list: # InlineComment1\n" + //
-                "  # Block Comment\n" + //
-                "  - item # InlineComment2\n" + //
+                "  - # Block Comment\n" + //
+                "    item # InlineComment2\n" + //
                 "# Comment\n";
 
         String result = runEmitterWithCommentsEnabled(data);
@@ -164,8 +165,8 @@ public class EmitterWithCommentEnabledTest {
                 "# Block Comment4\n" + //
                 "list: # InlineComment3a\n" + //
                 "      # InlineComment3b\n" + //
-                "  # Block Comment5\n" + //
-                "  - item1 # InlineComment4\n" + //
+                "  - # Block Comment5\n" + //
+                "    item1 # InlineComment4\n" + //
                 "  - item2: [value2a, value2b] # InlineComment5\n" + //
                 "  - item3: {key3a: [value3a1, value3a2], key3b: value3b} # InlineComment6\n" + //
                 "# Block Comment6\n" + //
@@ -196,14 +197,25 @@ public class EmitterWithCommentEnabledTest {
     @Test
     public void testAllComments2() throws Exception {
         String data = "" + //
-                "# Block Comment1\n" + //
-                "# Block Comment2\n" + //
-                "- item1 # Inline Comment1a\n" + //
+                "key:\n" +
+                "  key:\n" +
+                "    key:\n" +
+                "    - # Block Comment1\n" +
+                "      item1a\n" +
+                "    - # Block Comment2\n" +
+                "    - item1b\n" +
+                "    - # Block Comment3\n" +
+                "      MapKey_1: MapValue1\n" +
+                "      MapKey_2: MapValue2\n" +
+                "key2:\n" +
+                "- # Block Comment4\n" + //
+                "  # Block Comment5\n" + //
+                "  item1 # Inline Comment1a\n" + //
                 "        # Inline Comment1b\n" + //
-                "# Block Comment3a\n" + //
-                "# Block Comment3b\n" + //
-                "- item2: value # Inline Comment2\n" + //
-                "# Block Comment4\n" + //
+                "- # Block Comment6a\n" + //
+                "  # Block Comment6b\n" + //
+                "  item2: value # Inline Comment2\n" + //
+                "# Block Comment7\n" + //
                 "";
 
         String result = runEmitterWithCommentsEnabled(data);
@@ -223,4 +235,72 @@ public class EmitterWithCommentEnabledTest {
 
         assertEquals(data, result);
     }
+    
+    @Test
+    public void testKeepingNewLineInsideSequence() throws Exception {
+        String data = "" +
+                "\n" + 
+                "key:\n" + 
+                //"  \n" + // only supported in a sequence right now
+                "- item1\n" +
+                //"\n" + // Per Spec this is part of plain scalar above
+                "- item2\n" +
+                //"\n" + // Per Spec this is part of plain scalar above
+                "- item3\n" +
+                "\n" +
+                "key2: value2\n" +
+                "\n" +
+                "key3: value3\n" +
+                "\n" +
+                "";
+
+        String result = runEmitterWithCommentsEnabled(data);
+
+        assertEquals(data, result);
+
+    }
+    
+    @Test
+    public void testKeepingNewLineInsideSequence2() throws Exception {
+        String data = "" +
+                "apiVersion: kustomize.config.k8s.io/v1beta1\n" + 
+                "kind: Kustomization\n" + 
+                "\n" + 
+                "namePrefix: acquisition-gateway-\n" + 
+                "\n" + 
+                "bases:\n" +
+                /** Not supported right now
+                "  \n" + 
+                "#- https://github.intuit.com/dev-patterns/intuit-kustomize/intuit-service-appd-noingress-base?ref=v3.1.2\n" + 
+                "# Add the following base and HPA-patch.yaml, fill in correct minReplicas and maxReplcias in Hpa-patch.yaml\n" + 
+                "#- https://github.intuit.com/dev-patterns/intuit-kustomize//intuit-service-hpa-base?ref=v3.1.2\n" +
+                */
+                "- https://github.intuit.com/dev-patterns/intuit-kustomize//intuit-service-canary-appd-noingress-base?ref=v3.2.0\n" +
+                "- https://github.intuit.com/dev-patterns/intuit-kustomize//intuit-service-rollout-hpa-base?ref=v3.2.0\n" +
+                "# resources:\n" + 
+                "# - Nginx-ConfigMap.yaml\n" + 
+                "\n" + 
+                "resources:\n" + 
+                "- ConfigMap-v1-splunk-sidecar-config.yaml\n" + 
+                "- CronJob-patch.yaml\n" + 
+                "\n" + 
+                "patchesStrategicMerge:\n" + 
+                "- app-rollout-patch.yaml\n" + 
+                "- Service-patch.yaml\n" + 
+                "- Service-metrics-patch.yaml\n" + 
+                // "\n" + 
+                "- Hpa-patch.yaml\n" + 
+                "#- SignalSciences-patch.yaml\n" + 
+                "\n" + 
+                "# Uncomment HPA-patch when you need to enable HPA\n" + 
+                "#- Hpa-patch.yaml\n" + 
+                "# Uncomment SignalSciences-patch when you need to enable Signal Sciences\n" + 
+                "#- SignalSciences-patch.yaml\n" + 
+                "";
+
+        String result = runEmitterWithCommentsEnabled(data);
+
+        assertEquals(data, result);
+
+    }
 }


=====================================
src/test/java/org/yaml/snakeyaml/comment/ParserWithCommentEnabledTest.java
=====================================
@@ -32,11 +32,11 @@ public class ParserWithCommentEnabledTest {
     private boolean DEBUG = false;
 
     private void println(String s) {
-        if (DEBUG) println(s);
+        if (DEBUG) System.out.println(s);
     }
 
     private void println() {
-        if (DEBUG) println();
+        if (DEBUG) System.out.println();
     }
 
 
@@ -123,6 +123,7 @@ public class ParserWithCommentEnabledTest {
                 ID.DocumentStart, //
                 ID.MappingStart, //
                 ID.Scalar, ID.Comment, ID.Comment, ID.Scalar, //
+                ID.Comment, //
                 ID.MappingEnd, //
                 ID.DocumentEnd, //
                 ID.StreamEnd);
@@ -379,4 +380,55 @@ public class ParserWithCommentEnabledTest {
 //        printEventList(sut);
         assertEventListEquals(expectedEventIdList, sut);
     }
+    
+    @Test
+    public void testKeepingNewLineInsideSequence() throws Exception {
+        String data = "" +
+                "\n" + 
+                "key:\n" + 
+                "\n" + 
+                "- item1\n" + 
+                "\n" + // Per Spec this is part of plain scalar above
+                "- item2\n" + 
+                "\n" + // Per Spec this is part of plain scalar above
+                "- item3\n" + 
+                "\n" + // FIXME: ?Should be comment?
+                "key2: value2\n" +
+                "\n" + // FIXME: ?Should be comment?
+                "key3: value3\n" +
+                "\n" + // FIXME: ?Should be comment?
+                "";
+        
+        List<ID> expectedEventIdList = Arrays.asList(new ID[] { //
+                ID.StreamStart, //
+                ID.Comment, //
+                ID.DocumentStart, //
+                ID.MappingStart, //
+                ID.Scalar, //
+                ID.Comment, //
+                ID.SequenceStart, //
+                ID.Scalar, //
+                ID.Scalar, //
+                ID.Scalar, //
+                ID.Comment, //
+                ID.SequenceEnd, //
+                ID.Scalar, //
+                ID.Scalar, //
+                ID.Comment, //
+                ID.Scalar, //
+                ID.Scalar, //
+                ID.Comment, //
+                ID.MappingEnd, //
+                ID.DocumentEnd, //
+                ID.StreamEnd //
+        });
+
+        Parser sut = new ParserImpl(new StreamReader(data), true);
+
+        //printEventList(sut);
+        assertEventListEquals(expectedEventIdList, sut);
+
+
+    }
+
 }


=====================================
src/test/java/org/yaml/snakeyaml/comment/ProblematicYamlTest.java
=====================================
@@ -0,0 +1,242 @@
+/**
+ * Copyright (c) 2008, http://www.snakeyaml.org
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.yaml.snakeyaml.comment;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+import java.io.StringReader;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+
+import org.junit.Test;
+import org.yaml.snakeyaml.DumperOptions;
+import org.yaml.snakeyaml.DumperOptions.FlowStyle;
+import org.yaml.snakeyaml.LoaderOptions;
+import org.yaml.snakeyaml.comments.CommentType;
+import org.yaml.snakeyaml.events.CommentEvent;
+import org.yaml.snakeyaml.events.Event;
+import org.yaml.snakeyaml.events.Event.ID;
+import org.yaml.snakeyaml.parser.Parser;
+import org.yaml.snakeyaml.parser.ParserImpl;
+import org.yaml.snakeyaml.reader.StreamReader;
+
+public class ProblematicYamlTest {
+    private boolean DEBUG = false;
+
+    private void println(String s) {
+        if (DEBUG) System.out.println(s);
+    }
+
+    private void println() {
+        if (DEBUG) System.out.println();
+    }
+
+    private static final LoaderOptions LOAD_OPTIONS = new LoaderOptions();
+    static {
+        LOAD_OPTIONS.setProcessComments(true);
+    }
+
+    private void assertEventListEquals(List<ID> expectedEventIdList, List<CommentType> expectedCommentTypeList,
+            Parser parser) {
+        Iterator<CommentType> commentTypeIterator = expectedCommentTypeList.iterator();
+        for (ID expectedEventId : expectedEventIdList) {
+            parser.checkEvent(expectedEventId);
+            Event event = parser.getEvent();
+            println("Expected: " + expectedEventId);
+            if (event == null) {
+                fail("Missing event: " + expectedEventId);
+            }
+            println("Got: " + event
+                    + (event.getEventId() == ID.Comment ? " " + ((CommentEvent) event).getCommentType() : ""));
+            println();
+            if (event.getEventId() == ID.Comment) {
+                assertEquals(commentTypeIterator.next(), ((CommentEvent) event).getCommentType());
+            }
+            assertEquals(expectedEventId, event.getEventId());
+        }
+    }
+
+    @SuppressWarnings("unused")
+    private void printEventList(Parser parser) {
+        for (Event event = parser.getEvent(); event != null; event = parser.getEvent()) {
+            println("Got: " + event
+                    + (event.getEventId() == ID.Comment ? " " + ((CommentEvent) event).getCommentType() : ""));
+            println();
+        }
+    }
+
+    @Test
+    public void testParseProblematicYaml1() {
+        final String yamlString1 = "" + //
+                "key: value\n" + //
+                "  # Comment 1\n" + // s.b BLOCK, classified as INLINE
+                "\n" + //
+                "  # Comment 2\n" + //
+                "";
+
+        List<ID> expectedEventIdList = Arrays.asList(//
+                ID.StreamStart, //
+                ID.DocumentStart, //
+                ID.MappingStart, //
+                ID.Scalar, //
+                ID.Scalar, //
+                ID.Comment, //
+                ID.Comment, //
+                ID.Comment, //
+                ID.MappingEnd, //
+                ID.DocumentEnd, //
+                ID.StreamEnd //
+        );
+
+        List<CommentType> expectedCommentTypeList = Arrays.asList(//
+                CommentType.BLOCK, CommentType.BLANK_LINE, CommentType.BLOCK);
+
+        ParserImpl parser = new ParserImpl(new StreamReader(new StringReader(yamlString1)),
+                LOAD_OPTIONS.isProcessComments());
+
+        assertEventListEquals(expectedEventIdList, expectedCommentTypeList, parser);
+    }
+
+    @Test
+    public void testParseProblematicYaml2() {
+        final String yamlString2 = "" + //
+                "key: value\n" + //
+                "\n" + //
+                "  # Comment 1\n" + // s.b BLOCK, classified as INLINE
+                "\n" + //
+                "  # Comment 2\n" + //
+                "";
+
+        List<ID> expectedEventIdList = Arrays.asList(//
+                ID.StreamStart, //
+                ID.DocumentStart, //
+                ID.MappingStart, //
+                ID.Scalar, //
+                ID.Scalar, //
+                ID.Comment, //
+                ID.Comment, //
+                ID.Comment, //
+                ID.Comment, //
+                ID.MappingEnd, //
+                ID.DocumentEnd, //
+                ID.StreamEnd //
+        );
+
+        List<CommentType> expectedCommentTypeList = Arrays.asList(//
+                CommentType.BLANK_LINE, CommentType.BLOCK, CommentType.BLANK_LINE, CommentType.BLOCK);
+
+        ParserImpl parser = new ParserImpl(new StreamReader(new StringReader(yamlString2)),
+                LOAD_OPTIONS.isProcessComments());
+
+        assertEventListEquals(expectedEventIdList, expectedCommentTypeList, parser);
+    }
+
+    @Test
+    public void testParseProblematicYaml3() {
+        final String yamlString3 = "" + //
+                "key: value\n" + //
+                "\n" + //
+                "key: value\n" + //
+                "";
+
+        List<ID> expectedEventIdList = Arrays.asList(//
+                ID.StreamStart, //
+                ID.DocumentStart, //
+                ID.MappingStart, //
+                ID.Scalar, //
+                ID.Scalar, //
+                ID.Comment, //
+                ID.Scalar, //
+                ID.Scalar, //
+                ID.MappingEnd, //
+                ID.DocumentEnd, //
+                ID.StreamEnd //
+        );
+
+        List<CommentType> expectedCommentTypeList = Arrays.asList(CommentType.BLANK_LINE);
+
+        ParserImpl parser = new ParserImpl(new StreamReader(new StringReader(yamlString3)),
+                LOAD_OPTIONS.isProcessComments());
+
+        assertEventListEquals(expectedEventIdList, expectedCommentTypeList, parser);
+    }
+
+    @Test
+    public void testParseProblematicYaml4() {
+        String yamlString4= "" + //
+                "---\n" + //
+                "in the block context:\n" + //
+                "    indentation should be kept: { \n" + //
+                "    but in the flow context: [\n" + //
+                "it may be violated]\n" + //
+                "}\n" + //
+                "---\n" + //
+                "the parser does not require scalars\n" + //
+                "to be indented with at least one space\n" + //
+                "...\n" + //
+                "---\n" + //
+                "\"the parser does not require scalars\n" + //
+                "to be indented with at least one space\"\n" + //
+                "---\n" + //
+                "foo:\n" + //
+                "    bar: 'quoted scalars\n" + //
+                "may not adhere indentation'\n" + //
+                "";
+
+        List<ID> expectedEventIdList = Arrays.asList(//
+                ID.StreamStart, //
+                ID.DocumentStart, //
+                ID.MappingStart, //
+                ID.Scalar, //
+                ID.MappingStart, //
+                ID.Scalar, //
+                ID.MappingStart, //
+                ID.Scalar, //
+                ID.SequenceStart, //
+                ID.Scalar, //
+                ID.SequenceEnd, //
+                ID.MappingEnd, //
+                ID.MappingEnd, //
+                ID.MappingEnd, //
+                ID.DocumentEnd, //
+                ID.DocumentStart, //
+                ID.Scalar, //
+                ID.DocumentEnd, //
+                ID.DocumentStart, //
+                ID.Scalar, //
+                ID.DocumentEnd, //
+                ID.DocumentStart, //
+                ID.MappingStart, //
+                ID.Scalar, //
+                ID.MappingStart, //
+                ID.Scalar, //
+                ID.Scalar, //
+                ID.MappingEnd, //
+                ID.MappingEnd, //
+                ID.DocumentEnd, //
+                ID.StreamEnd//
+        );
+
+        ParserImpl parser = new ParserImpl(new StreamReader(new StringReader(yamlString4)),
+                LOAD_OPTIONS.isProcessComments());
+
+        assertEventListEquals(expectedEventIdList, new ArrayList<CommentType>(), parser);
+    }
+   
+}


=====================================
src/test/java/org/yaml/snakeyaml/comment/ScannerWithCommentEnabledTest.java
=====================================
@@ -140,6 +140,7 @@ public class ScannerWithCommentEnabledTest {
                 ID.BlockMappingStart, //
                 ID.Key, ID.Scalar, ID.Value, ID.Comment, ID.Comment, //
                 ID.Scalar, //
+                ID.Comment, //
                 ID.BlockEnd, //
                 ID.StreamEnd);
         List<String> expectedScalarValue = Arrays.asList(//


=====================================
src/test/java/org/yaml/snakeyaml/comment/SerializerWithCommentEnabledTest.java
=====================================
@@ -40,11 +40,11 @@ public class SerializerWithCommentEnabledTest {
     private boolean DEBUG = false;
 
     private void println(String s) {
-        if (DEBUG) println(s);
+        if (DEBUG) System.out.println(s);
     }
 
     private void println() {
-        if (DEBUG) println();
+        if (DEBUG) System.out.println();
     }
 
     private void assertEventListEquals(List<ID> expectedEventIdList, List<Event> actualEvents) {
@@ -153,6 +153,7 @@ public class SerializerWithCommentEnabledTest {
                 ID.MappingStart, //
                 ID.Scalar, ID.Comment, ID.Comment, ID.Scalar, //
                 ID.MappingEnd, //
+                ID.Comment, //
                 ID.DocumentEnd, //
                 ID.StreamEnd);
 
@@ -189,8 +190,8 @@ public class SerializerWithCommentEnabledTest {
         List<ID> expectedEventIdList = Arrays.asList(//
                 ID.StreamStart, //
                 ID.DocumentStart, //
-                ID.Comment, //
                 ID.MappingStart, //
+                ID.Comment, //
                 ID.Scalar, ID.Scalar, ID.Comment, //
                 ID.MappingEnd, //
                 ID.Comment, //
@@ -253,10 +254,11 @@ public class SerializerWithCommentEnabledTest {
         List<ID> expectedEventIdList = Arrays.asList(//
                 ID.StreamStart, //
                 ID.DocumentStart, //
-                ID.Comment, //
                 ID.MappingStart, //
-                ID.Scalar, ID.Comment, ID.Comment, //
+                ID.Comment, //
+                ID.Scalar, ID.Comment, //
                 ID.SequenceStart, //
+                ID.Comment, //
                 ID.Scalar, ID.Comment, //
                 ID.SequenceEnd, //
                 ID.MappingEnd, //
@@ -295,9 +297,9 @@ public class SerializerWithCommentEnabledTest {
         List<ID> expectedEventIdList = Arrays.asList(//
                 ID.StreamStart, //
                 ID.DocumentStart, //
+                ID.MappingStart, //
                 ID.Comment, //
                 ID.Comment, //
-                ID.MappingStart, //
                 ID.Scalar, ID.Comment, ID.Comment, //
 
                 ID.Comment, ID.Comment, //
@@ -305,10 +307,12 @@ public class SerializerWithCommentEnabledTest {
 
                 ID.Comment, //
                 ID.Scalar, ID.Comment, ID.Comment, //
-                ID.Comment, //
 
                 ID.SequenceStart, //
-                ID.Scalar, ID.Comment, //
+                ID.Comment, //
+                ID.Scalar, //
+                ID.Comment, //
+
                 ID.MappingStart, //
                 ID.Scalar, ID.SequenceStart, ID.Scalar, ID.Scalar, ID.SequenceEnd, ID.Comment, //
                 ID.MappingEnd,
@@ -359,13 +363,13 @@ public class SerializerWithCommentEnabledTest {
         List<ID> expectedEventIdList = Arrays.asList(//
                 ID.StreamStart, //
                 ID.DocumentStart, //
+                ID.SequenceStart, //
                 ID.Comment, //
                 ID.Comment, //
-                ID.SequenceStart, //
                 ID.Scalar, ID.Comment, ID.Comment, //
+                ID.MappingStart, //
                 ID.Comment, //
                 ID.Comment, //
-                ID.MappingStart, //
                 ID.Scalar, ID.Scalar, ID.Comment, //
                 ID.MappingEnd, //
                 ID.SequenceEnd, //


=====================================
src/test/java/org/yaml/snakeyaml/issues/issue500/FailIteratorOnlyWhenErrorFoundTest.java
=====================================
@@ -0,0 +1,50 @@
+/**
+ * Copyright (c) 2008, http://www.snakeyaml.org
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.yaml.snakeyaml.issues.issue500;
+
+import junit.framework.TestCase;
+import org.yaml.snakeyaml.Yaml;
+
+import java.util.Iterator;
+
+public class FailIteratorOnlyWhenErrorFoundTest extends TestCase {
+
+    public void testFailure() {
+        try {
+            Yaml yamlProcessor = new Yaml();
+            String data = "a: 1\n" +
+                    "---\n" +
+                    "Some comment \n" +
+                    "\n" +
+                    "\n" +
+                    "b: 2\n" +
+                    "\n" +
+                    "c: 3\n" +
+                    "---";
+            Iterable<Object> parsed = yamlProcessor.loadAll(data);
+            Iterator<Object> iterator = parsed.iterator();
+            assertNotNull(iterator.next()); //no failure, first document id valid
+            iterator.next();
+//            for (Object obj : parsed) {
+//                assertNotNull(obj);
+//                System.out.println(obj);
+//            }
+            fail("Should not accept the second document");
+        } catch (Exception e) {
+            assertTrue(e.getMessage(), e.getMessage().contains("in 'reader', line 6, column 2:"));
+        }
+    }
+}



View it on GitLab: https://salsa.debian.org/java-team/snakeyaml/-/commit/ee460f9b93101b5b5bf6b94edc62ea27777a6178

-- 
View it on GitLab: https://salsa.debian.org/java-team/snakeyaml/-/commit/ee460f9b93101b5b5bf6b94edc62ea27777a6178
You're receiving this email because of your account on salsa.debian.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://alioth-lists.debian.net/pipermail/pkg-java-commits/attachments/20210926/dc256daa/attachment.htm>


More information about the pkg-java-commits mailing list