Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

YamlStream.Load(TextReader) interprets an empty value as an empty string instead of null. #810

Closed
sfwester opened this issue May 30, 2023 · 2 comments · Fixed by #834
Closed
Labels

Comments

@sfwester
Copy link

sfwester commented May 30, 2023

Describe the bug

Version: 13.1.0

According to:

annotations:

generally expected to be equivalent to

annotations: ~

However, YamlStream.Load(TextReader) defaults annotations to the empty string. i.e.,

annotations: ''

To Reproduce

using System;
using System.IO;
using YamlDotNet.RepresentationModel;
					
public class Program
{
	public static void Main()
	{
		var inYaml = "annotations:";

                using var sr = new StringReader(inYaml);

                var yamlStream = new YamlStream();
                yamlStream.Load(sr);

                using var writer = new StringWriter();
                new YamlStream(yamlStream.Documents).Save(writer);
                var outYaml = writer.ToString();
		
		Console.Write(outYaml);
	}
}

Actual

annotations: ''
...

Expected

annotations: ~
...

or

annotations: null
...

@sfwester sfwester changed the title YamlStream.Load(TextReader) interpret and empty value as an empty string instead of null. YamlStream.Load(TextReader) interprets an empty value as an empty string instead of null. May 30, 2023
@EdwardCooke EdwardCooke added the bug label Jul 4, 2023
@EdwardCooke
Copy link
Collaborator

Ok, so I finally got some time to look at this. It isn't that it's interpreting it as an empty string, but rather an empty scalar object. This is by design and changing the way it's handled and converting it to a null would be a breaking change that could easily cause a lot of problems for anybody using this.

What it does is reads the yaml node explicitly. Setting the scalar object with an empty string and no tag, since one doesn't exist on that node and marks it as a plain formatted scalar (as opposed to a single, double quoted, etc). Then, when it serializes it, since there is no tag, it writes out a quoted empty string. If there is a tag, like !!null it works as expected, which also outputs the tag in the object, which is not ideal since it doesn't round trip the YAML the way that would be expected.

I have some code ready for it, just want to run some additional tests and validate it a little more to make sure it's most likely not going to inadvertently break some consumer of the library.

@EdwardCooke
Copy link
Collaborator

I have a PR ready with good tests around it to make sure it won't break existing users (in theory). I am going to give the current version another week before putting out a new one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants