Skip to content

Commit

Permalink
Merge pull request #730 from lkillgore/master
Browse files Browse the repository at this point in the history
Fix Inf and NaN regex when using WithAttemptingUnquotedStringTypeDeserialization

+semver:fix
  • Loading branch information
EdwardCooke authored Oct 7, 2022
2 parents 6a7f233 + b3190e0 commit 8821b7f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
24 changes: 24 additions & 0 deletions YamlDotNet.Test/Serialization/DeserializerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IDictionary<string, object>>(yaml);
Assert.True(resultDict.ContainsKey("Value"));
Assert.Equal(expected, resultDict["Value"]);
}

public static IEnumerable<object[]> DeserializeScalarEdgeCases_TestCases
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("-"))
{
Expand All @@ -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;
}
Expand Down

0 comments on commit 8821b7f

Please sign in to comment.