How to make snakeyaml fail fast when reading multiple yaml documents in one file

Issue #500 invalid
Chris Kelly created an issue

Say I have the following YAML file

a: 1
---
Some comment 


b: 2

c: 3
---

I have a invalid YAML on line 3. It should be a comment i.e. # Some comment

If I use yaml.loadAll, then the parser dosen’t fail but only gives me content it can parse i.e. a: 1. I would like it to fast-fail on any errors but the parser just seems to skip them.

This is a contrived example but I’m using snakeyaml to parse Spring application YAML files to see the YAML is well formed. When I use this, it only gives the part of the yaml up to the first error and nothing else.

I can’t see any a way either in loader options or looking at the code to make the parser fast-fail.

If I use yaml.load with a file that only has a single document and that has a invalid content, then the parser does fail with the expected error.

Comments (1)

  1. Andrey Somov

    One of the important features of streaming is that the error happens only when you process the data.

    If a user does not pull data, no parsing happens (and no error may occur). In your case you have to call the iterator to parsed all the data. I have added an example: https://bitbucket.org/asomov/snakeyaml/src/master/src/test/java/org/yaml/snakeyaml/issues/issue500/FailIteratorOnlyWhenErrorFoundTest.java
    Basically, you need to call next() until it is empty of an error happens.

    No need to change anything in SnakeYAML

  2. Log in to comment