Valid YAML fails to parse when comment handling is enabled
Given the input:
# on list
[
# first
'first entry',
# second
'second entry'
]
the parser will fail when ScannerImpl.emitComments
is true
, with the following error message:
<org.yaml.snakeyaml.events.StreamStartEvent()>
<org.yaml.snakeyaml.events.CommentEvent(type=BLOCK, value= on list)>
<org.yaml.snakeyaml.events.DocumentStartEvent()>
<org.yaml.snakeyaml.events.SequenceStartEvent(anchor=null, tag=null, implicit=true)>
<org.yaml.snakeyaml.events.CommentEvent(type=BLOCK, value= first)>
Caught: while parsing a flow node
in 'string', line 4, column 18:
'first entry',
^
expected the node content, but found ','
in 'string', line 4, column 18:
'first entry',
^
while parsing a flow node
in 'string', line 4, column 18:
'first entry',
^
expected the node content, but found ','
in 'string', line 4, column 18:
'first entry',
^
at org.yaml.snakeyaml.parser.ParserImpl.parseNode(ParserImpl.java:521)
at org.yaml.snakeyaml.parser.ParserImpl.parseFlowNode(ParserImpl.java:401)
at org.yaml.snakeyaml.parser.ParserImpl.access$2600(ParserImpl.java:121)
at org.yaml.snakeyaml.parser.ParserImpl$ParseFlowSequenceEntry.produce(ParserImpl.java:769)
at org.yaml.snakeyaml.parser.ParserImpl$ParseFlowSequenceFirstEntry.produce(ParserImpl.java:738)
at org.yaml.snakeyaml.parser.ParserImpl.peekEvent(ParserImpl.java:166)
at org.yaml.snakeyaml.parser.Parser$peekEvent$0.call(Unknown Source)
at test.run(test.groovy:29)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
I’ve tested this building off of your commit c42e19bcec6c003befa1dc05ac21eab5f92b13d6, using the following groovy script (run with groovy -cp snakeyaml/target/snakeyaml-1.30-SNAPSHOT.jar test.groovy
:
import org.yaml.snakeyaml.parser.ParserImpl
import org.yaml.snakeyaml.reader.StreamReader
import org.yaml.snakeyaml.scanner.ScannerImpl
def contents = """\
# on list
[
# first
'first entry',
# second
'second entry'
]
"""
// Print events
def scanner = new ScannerImpl(new StreamReader(contents))
scanner.emitComments = true // Causes a failure
def parser = new ParserImpl(scanner)
do {
println parser.getEvent()
} while (parser.peekEvent())
When removing the scanner.emitComments = true
line, this text parses fine but comments are not handled (as expected).
I’ve been experimenting with applying the new comment handling to my application – some earlier work suggests https://github.com/SpongePowered/Configurate/blob/1d788e89b799c129df6ba8c1c66edde183a1a188/vendor/snakeyaml-patches/0001-parser-More-tests-for-comment-reading.patch will resolve the issue, but I’m not sure if my fix was the “correct” way. Feel free to use anything there (or in any of my other patches) that is useful to you.
With that patch applied, the event stream looks like this:
<org.yaml.snakeyaml.events.StreamStartEvent()>
<org.yaml.snakeyaml.events.CommentEvent(type=BLOCK, value= on list)>
<org.yaml.snakeyaml.events.DocumentStartEvent()>
<org.yaml.snakeyaml.events.SequenceStartEvent(anchor=null, tag=null, implicit=true)>
<org.yaml.snakeyaml.events.CommentEvent(type=BLOCK, value= first)>
<org.yaml.snakeyaml.events.ScalarEvent(anchor=null, tag=null, implicit=[false, true], value=first entry)>
<org.yaml.snakeyaml.events.CommentEvent(type=BLOCK, value= second)>
<org.yaml.snakeyaml.events.ScalarEvent(anchor=null, tag=null, implicit=[false, true], value=second entry)>
<org.yaml.snakeyaml.events.SequenceEndEvent()>
<org.yaml.snakeyaml.events.DocumentEndEvent()>
<org.yaml.snakeyaml.events.StreamEndEvent()>
This matches what I would expect.
Comments (6)
-
-
reporter The original test in the issue was performed on the latest SNAPSHOT:
I’ve tested this building off of your commit c42e19bcec6c003befa1dc05ac21eab5f92b13d6, using the following groovy script (run with
groovy -cp snakeyaml/target/snakeyaml-1.30-SNAPSHOT.jar test.groovy
:
-
@Wolf2323 I added the failing test for this issue:
https://bitbucket.org/asomov/snakeyaml/commits/25121115e317c4a5a3999cc095d50404369e1c7c -
i will look at this hopefully this week. At the moment i do not have much time.
-
There is now a PR for this issue: https://bitbucket.org/asomov/snakeyaml/pull-requests/25
-
- changed status to resolved
Fixed in version 1.30
- Log in to comment
Please test if it is already fixed in the latest SNAPSHOT