xoofx / SharpYaml

SharpYaml is a .NET library for YAML compatible with CoreCLR
Other
336 stars 61 forks source link

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

Open headcr4sh opened 5 months ago

headcr4sh commented 5 months ago

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