SharpYaml icon indicating copy to clipboard operation
SharpYaml copied to clipboard

Empty node values are resolved to an empty string (`''`) instead of `null`

Open headcr4sh opened this issue 1 year ago • 0 comments
trafficstars

What's my problem?

I just recently stumbled across some weird behavior in bicep, which uses SharpYaml under the hood for it's loadYamlContent() function.

Parsing a YAML file with empty node contents yields empty strings instead of null values, which seems wrong from what I can tell.

Example

The following YAML document

toplevel:
  nested1:
  nested2: null
  nested3: ''

is parsed as:

{
  "toplevel": {
    "nested1": "",
    "nested2":null,
    "nested3":""
  }
}

I expected the value of nested1 to be null as well, though.

Excerpt from the YAML 1.2.2 specification:

YAML allows the node content to be omitted in many cases. Nodes with empty content are interpreted as if they were plain scalars with an empty value. Such nodes are commonly resolved to a “null” value.

Example 7.3: Completely Empty Flow Nodes seems to further clarify how empty node content should be resolved.

Conclusion

I tried different YAML parsers (e.g. yq, pyyaml and Go's yaml/v2 package, which is based on LibYYAML) and it seems as if most resolve empty node values to null, whereas SharpYaml seems indeed to be a notable exception when it comes to handling empty node values.

Links

  • Downstream issue raised in the bicep repo: https://github.com/Azure/bicep/issues/14143

headcr4sh avatar May 24 '24 19:05 headcr4sh