Skip to content

Commit

Permalink
NewtonsoftJsonSource: fix for evaluating null values (#201)
Browse files Browse the repository at this point in the history
NewtonsoftJsonSource  - fix evaluating null values
  • Loading branch information
axunonb committed Sep 13, 2021
1 parent e7724e8 commit d84a444
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/SmartFormat.Tests/Core/FormatterExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void Formatters_AutoDetection_Should_Not_Throw()
{
foreach (var formatter in GetAllFormatters().Where(f => f.CanAutoDetect))
{
var fi = FormattingInfo.Create("", new List<object> {new()});
var fi = FormattingInfo.Create("", new List<object?> {new()});
Assert.That(() => formatter.TryEvaluateFormat(fi),
Throws.Nothing);
}
Expand Down
6 changes: 3 additions & 3 deletions src/SmartFormat.Tests/Extensions/NewtonsoftJsonSourceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,16 @@ private SmartFormatter GetFormatterWithJsonSource(SmartSettings? settings = null
// NewtonsoftJsonSource MUST be registered before ReflectionSource (which is not required here)
// We also need the ListFormatter to process arrays
smart.AddExtensions(new ListFormatter(), new DefaultSource(), new NewtonsoftJsonSource());
smart.AddExtensions(new ListFormatter(), new DefaultFormatter());
smart.AddExtensions(new ListFormatter(), new NullFormatter(), new DefaultFormatter());
return smart;
}

[Test]
public void Format_Null_Json()
{
var jObject = JObject.Parse(JsonNull);
var result = GetFormatterWithJsonSource().Format("{Name}", jObject);
Assert.AreEqual("", result);
var result = GetFormatterWithJsonSource().Format("{Name:isnull:Value is Null|{}}", jObject);
Assert.AreEqual("Value is Null", result);
}

[Test]
Expand Down
6 changes: 3 additions & 3 deletions src/SmartFormat.Tests/Extensions/SystemTextJsonSourceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,16 @@ private static SmartFormatter GetFormatterWithJsonSource(SmartSettings? settings
// SystemTextJsonSource MUST be registered before ReflectionSource (which is not required here)
// We also need the ListFormatter to process arrays
smart.AddExtensions(new ListFormatter(), new DefaultSource(), new SystemTextJsonSource());
smart.AddExtensions(new ListFormatter(), new DefaultFormatter());
smart.AddExtensions(new ListFormatter(), new NullFormatter(), new DefaultFormatter());
return smart;
}

[Test]
public void Format_Null_Json()
{
var jObject = JsonDocument.Parse(JsonNull.Replace("'", "\"")).RootElement;
var result = GetFormatterWithJsonSource().Format("{Name}", jObject);
Assert.AreEqual("", result);
var result = GetFormatterWithJsonSource().Format("{Name:isnull:Value is Null|{}}", jObject);
Assert.AreEqual("Value is Null", result);
}

[Test]
Expand Down
7 changes: 6 additions & 1 deletion src/SmartFormat/Extensions/NewtonsoftJsonSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ private static bool TryEvaluateJObject(JObject jsonObject, ISelectorInfo selecto
var jToken = jsonObject.GetValue(selectorInfo.SelectorText,
selectorInfo.FormatDetails.Settings.GetCaseSensitivityComparison());

selectorInfo.Result = jToken ?? throw new FormatException($"'{selectorInfo.SelectorText}'");
selectorInfo.Result = jToken?.Type switch {
null => throw new FormatException($"'{selectorInfo.SelectorText}'"),
JTokenType.Null => null,
_ => jToken
};

return true;
}

Expand Down

0 comments on commit d84a444

Please sign in to comment.