Skip to content

A single string scalar as output is not quoted if necessary (breaking roundtrip safety) #2608

@sebastianriese

Description

@sebastianriese

Describe the bug

If the output from yq is a single string, then it will not be quoted properly in all cases.

If the string's content is the valid YAML encoding of something else (e.g. an integer, a map or a sequence), the output YAML will not encode that string, but the data encoded by that string when interpreted as a YAML value.

In other words: yq fails to detect if encoding a single string scalar as output, whether this string needs to be quoted. You could also say it treats a string scalar output as if you have given the -r flag in all cases.

Version of yq: 4.53.2
Operating system: linux
Installed via: docker

Input Yaml
Concise yaml document(s) (as simple as possible to show the bug, please keep it to 10 lines or less)
data1.yml:

"this: should really work"

Command
The command you ran:

yq eval . data1.yaml

Actual behaviour

this: should really work

This should only happen, if we called yq with the -r option to enable raw output of scalars.

Expected behaviour

"this: should really work"

Additional context

The problem does not depend on the input being top-level, but only on the output being a string scalar. It occurs the same for:

data2.yaml

foo: |
  a: a
  b: b
yq .foo data2.yaml

Which yields the output

a: a
b: b

It also happens if the output is a stream documents (if one of the documents is just a string scalar).

I could not reproduce this behaviour for other cases (such as when returning a sequence or map) and I haven't looked at the code yet to see whether this could be triggered in other cases.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions