YamlDotNet icon indicating copy to clipboard operation
YamlDotNet copied to clipboard

Implicit `null` is interpreted as empty string

Open flobernd opened this issue 3 years ago • 2 comments
trafficstars

Hi, I' using version 12.0.0 and have the following YAML:

on:
  implicit:
  explicit: null
  empty-string: ''

During deserialization, the implicit null value of workflow_dispatch gets interpreted as an empty string "":

Deserializer.Deserialize<YamlMappingNode>(reader)
on:
  implicit: ''
  explicit: null
  empty-string: ''

Same thing can be observed during serialization when I manually create a YamlScalarNode with a null value.

For the low level representation as Yaml{*}Node I would expect the original format to get preserved. If there was an explicit null it should re-serialize as explicit null and if there was an implicit null, it should re-serialize as implicit null. In no case should it re-serialize as "" imho.


When using Dictionary<object, object> instead of YamlMappingNode as the target type for the deserialization method, everything seems to work fine. Explicit null values are emitted as implicit ones at the end, but that's fine in this case I guess.

Deserializer.Deserialize<Dictionary<object, object>>(reader)
on:
  implicit: 
  explicit: 
  empty-string: ''

flobernd avatar Aug 25 '22 09:08 flobernd

As for reserializing explicit null to implicit null, once the yaml document is converted to a dot net object we no longer have how it was represented in the yaml document. It's a dot net limitation, we can't safely add arbitrary properties to an object. Not that I am aware of anyways, so we can't re-serialize exactly to what it was, just what is considered valid yaml.

As for deserializing an implicit null to an empty string instead of null, you are correct, that is a bug that needs to be addressed. I'm adding the bug tag so we can look at it. If you want to take a stab at it and submit a PR then that would be great.

EdwardCooke avatar Sep 08 '22 17:09 EdwardCooke

I put in a fix for something similar to this issue in the last release (might have been the one before). Can you confirm if this is still a problem or not? If it is can post a complete code example to reproduce it?

EdwardCooke avatar Sep 01 '23 19:09 EdwardCooke