Strings with spaces are incorrectly divided into more lines

Issue #47 resolved
Piotr Krzemiński created an issue

Example code

import org.snakeyaml.engine.v2.api.DumpSettings
import org.snakeyaml.engine.v2.api.StreamDataWriter
import org.snakeyaml.engine.v2.common.ScalarStyle
import org.snakeyaml.engine.v2.emitter.Emitter
import org.snakeyaml.engine.v2.events.*
import java.io.StringWriter
import java.util.*

fun main() {
    val settings = DumpSettings.builder()
        .setWidth(80) // Intentionally limited.
        .build()
    val writer = object : StringWriter(), StreamDataWriter {
        override fun flush() {
            // no-op
        }
    }
    val emitter = Emitter(settings, writer)
    emitter.emit(StreamStartEvent())
    emitter.emit(DocumentStartEvent(false, Optional.empty(), emptyMap()))

    val stringToSerialize = "arn:aws:iam::12345678901234567890:foobarbaz:testing:testing2:role/github-actions-role/${'$'}{{ github.token }}"
    emitter.emit(ScalarEvent(Optional.empty(), Optional.empty(), ImplicitTuple(true, true), stringToSerialize, ScalarStyle.PLAIN))

    emitter.emit(DocumentEndEvent(false))
    emitter.emit(StreamEndEvent())
    val yaml = writer.toString()
    println(yaml)
}

Actual

arn:aws:iam::12345678901234567890:foobarbaz:testing:testing2:role/github-actions-role/${{
  github.token }}

Expected

arn:aws:iam::12345678901234567890:foobarbaz:testing:testing2:role/github-actions-role/${{
  \ github.token }}

Description

There’s a problem when the line break falls around a place which includes a whitespace. It turned out that in my use cases, this space is important and SnakeYaml produced something equivalent to:

arn:aws:iam::12345678901234567890:foobarbaz:testing:testing2:role/github-actions-role/${{github.token }}
                                                                                        /\
                                                                                        missing space

instead what was expected (see above). The result was that the emitted YAML was incorrectly parsed on the receiver side.

Workaround

I currently configure the dumper to have no line length limit:

val settings = DumpSettings.builder()
    // Otherwise line breaks appear in places that create an incorrect YAML, e.g. in the middle of GitHub
    // expressions.
    .setWidth(Int.MAX_VALUE)
    .build()

but I don’t really like it. It would be good to go back to line lengths that can fit on a regular display’s width.

Comments (16)

  1. Piotr Krzemiński reporter
         String yaml = writer.toString();
         String expected =
    -        "arn:aws:iam::12345678901234567890:foobarbaz:testing:testing2:role/github-actions-role/${{ github.token }}";
    +        "arn:aws:iam::12345678901234567890:foobarbaz:testing:testing2:role/github-actions-role/${{\n  \\ github.token }}";
         assertEquals(expected, yaml);
    

  2. Andrey Somov

    I changed. Can you please check again ? Do you mean that a backslash is expected and missing ?

  3. Piotr Krzemiński reporter

    Yes, now it looks fine, I believe it should work fine. If you have dev snapshots for the lib, I can test a fix.

  4. Piotr Krzemiński reporter

    Short answer: I checked and indeed everything works fine. Either my original bug report was incorrect (=it wasn’t really a problematic behavior), or GitHub (where I use YAMLs produced with snakeyaml-engine) fixed something in parsing YAML in GitHub Actions workflow files.

    Long answer:

    Some details in case needed: this bug report was a result of this bug report in my library: https://github.com/typesafegithub/github-workflows-kt/issues/616, which made me add this workaround in my lib: https://github.com/typesafegithub/github-workflows-kt/commit/276c94f5b2375ac68222fb40af0e145d9eca4bc0. The report mentioned an invalid YAML. I checked with GitHub Actions (https://github.com/krzema12/test-repo/blob/ac462316f50a2cffa36246af83ce9eb264cfbfc3/.github/workflows/test.yaml) and when the strings aren’t quoted, things work across the board. If the strings are quoted, it works correctly in runtime, but the editor shows an error:

    Looks like there is some discrepancy on the GitHub’s side, but TBH, I don’t have resources to chase it down.

  5. Andrey Somov

    Well, when YAML is created in SnakeYAML Engine but consumed (parsed) by another parser - unexpected things may come. Do they use Ruby to parse YAML?

  6. Log in to comment