YamlDotNet icon indicating copy to clipboard operation
YamlDotNet copied to clipboard

String starting with tab and comma is incorrectly serialized leading to SemanticErrorException when that yaml is deserialized

Open gb6sthingz opened this issue 1 year ago • 2 comments

The following code is the full code to reproduce the bug. (Its commented.)

var serializer = new SerializerBuilder().Build(); var yaml = serializer.Serialize(new List() { "\t," }); // Fails to decorate the yaml string, which causes the exception when deserializing - see below var deserializer = new DeserializerBuilder().Build(); deserializer.Deserialize<List>(yaml); // SemanticErrorException : (Line: 1, Col: 4, Idx: 3) - (Line: 1, Col: 5, Idx: 4): While parsing a node, did not find expected node

Project settings C# <TargetFramework>net6.0-windows10.0.22000.0</TargetFramework> <PackageReference Include="YamlDotNet" Version="12.0.2" />

Thanks

gb6sthingz avatar Oct 21 '22 14:10 gb6sthingz

Thank you for the bug report. I'll take a look at it in the next few days. I suspect it's in the node serializer and it's not quoting strings that start with whitespace, it'd be interesting to know if " ," also throws an error or is incorrect when deserializing. I suspect it will be missing the leading space.

EdwardCooke avatar Oct 21 '22 19:10 EdwardCooke

I bet if we modify this regex to include a whitespace check at the beginning of the string it would fix it.

https://github.com/aaubry/YamlDotNet/blob/02685df84cb2e0b81f327e890968267e4b60e0f0/YamlDotNet/Serialization/EventEmitters/TypeAssigningEventEmitter.cs#L35

After doing that, it would work by calling withquotenecessarystrings on the serializer builder. It'll be a few days before I can test this out and get a fix in though.

EdwardCooke avatar Oct 21 '22 19:10 EdwardCooke

This bug also happens to me. After I serialize an object with "\t- a" string, I can't deserialize the yaml. See https://github.com/stakira/OpenUtau/issues/1040

oxygen-dioxide avatar Feb 25 '24 01:02 oxygen-dioxide

As soon as the next release goes out (in about 10-15 minutes), this will be fixed. You'll need to make sure to include the .WithQuotingNecessaryStrings() is called on your SerializerBuilder.

EdwardCooke avatar Feb 26 '24 15:02 EdwardCooke

A fix for this issue has been released in version 15.1.2.

aaubry avatar Feb 26 '24 16:02 aaubry