From 2da4c32454e63d52058d54629f8de67c78a85d74 Mon Sep 17 00:00:00 2001 From: Lucas Killgore Date: Tue, 4 Oct 2022 14:14:47 -0400 Subject: [PATCH 1/2] Fix for #729 --- .../Serialization/DeserializerTest.cs | 24 +++++++++++++++++++ .../ScalarNodeDeserializer.cs | 4 ++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/YamlDotNet.Test/Serialization/DeserializerTest.cs b/YamlDotNet.Test/Serialization/DeserializerTest.cs index 30046df7..2b774abc 100644 --- a/YamlDotNet.Test/Serialization/DeserializerTest.cs +++ b/YamlDotNet.Test/Serialization/DeserializerTest.cs @@ -216,6 +216,30 @@ public void NewLinesInKeys() Assert.Equal($"value\na\nb", dictionary.First().Value); } + + [Theory] + [InlineData(".nan", System.Single.NaN)] + [InlineData(".NaN", System.Single.NaN)] + [InlineData(".NAN", System.Single.NaN)] + [InlineData("-.inf", System.Single.NegativeInfinity)] + [InlineData("+.inf", System.Single.PositiveInfinity)] + [InlineData(".inf", System.Single.PositiveInfinity)] + [InlineData("start.nan", "start.nan")] + [InlineData(".nano", ".nano")] + [InlineData(".infinity", ".infinity")] + [InlineData("www.infinitetechnology.com", "www.infinitetechnology.com")] + [InlineData("https://api.inference.azure.com", "https://api.inference.azure.com")] + public void UnquotedStringTypeDeserializationHandlesInfAndNaN(string yamlValue, object expected) + { + var deserializer = new DeserializerBuilder() + .WithAttemptingUnquotedStringTypeDeserialization().Build(); + var yaml = $"Value: {yamlValue}"; + + var resultDict = deserializer.Deserialize>(yaml); + Assert.True(resultDict.ContainsKey("Value")); + Assert.Equal(expected, resultDict["Value"]); + } + public static IEnumerable DeserializeScalarEdgeCases_TestCases { get diff --git a/YamlDotNet/Serialization/NodeDeserializers/ScalarNodeDeserializer.cs b/YamlDotNet/Serialization/NodeDeserializers/ScalarNodeDeserializer.cs index b48bfa81..d455a3fa 100644 --- a/YamlDotNet/Serialization/NodeDeserializers/ScalarNodeDeserializer.cs +++ b/YamlDotNet/Serialization/NodeDeserializers/ScalarNodeDeserializer.cs @@ -381,7 +381,7 @@ private static object CastInteger(ulong number, TypeCode typeCode) result = v; } } - else if (Regex.IsMatch(v, @"[-+]?(\.inf|\.Inf|\.INF)")) //infinities + else if (Regex.IsMatch(v, @"^[-+]?(\.inf|\.Inf|\.INF)$")) //infinities { if (v.StartsWith("-")) { @@ -392,7 +392,7 @@ private static object CastInteger(ulong number, TypeCode typeCode) result = float.PositiveInfinity; } } - else if (Regex.IsMatch(v, @"\.nan|\.NaN|\.NAN")) //not a number + else if (Regex.IsMatch(v, @"^\.nan$|^\.NaN$|^\.NAN$")) //not a number { result = float.NaN; } From b3190e054656b7ff9647c8f98373934f1e9e8820 Mon Sep 17 00:00:00 2001 From: Lucas Killgore Date: Tue, 4 Oct 2022 14:21:01 -0400 Subject: [PATCH 2/2] Clean up regex for NaN --- .../Serialization/NodeDeserializers/ScalarNodeDeserializer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/YamlDotNet/Serialization/NodeDeserializers/ScalarNodeDeserializer.cs b/YamlDotNet/Serialization/NodeDeserializers/ScalarNodeDeserializer.cs index d455a3fa..caccfd2d 100644 --- a/YamlDotNet/Serialization/NodeDeserializers/ScalarNodeDeserializer.cs +++ b/YamlDotNet/Serialization/NodeDeserializers/ScalarNodeDeserializer.cs @@ -392,7 +392,7 @@ private static object CastInteger(ulong number, TypeCode typeCode) result = float.PositiveInfinity; } } - else if (Regex.IsMatch(v, @"^\.nan$|^\.NaN$|^\.NAN$")) //not a number + else if (Regex.IsMatch(v, @"^(\.nan|\.NaN|\.NAN)$")) //not a number { result = float.NaN; }