Skip to content

Commit

Permalink
Merge pull request #44 from myquay/patch-fix-value-convert-boolean
Browse files Browse the repository at this point in the history
Fix issue with IConvertable and create unit test to detect regression…
  • Loading branch information
myquay authored Jun 22, 2023
2 parents 1eb648f + 4d3f379 commit 17611a6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/JsonPatch.Tests/CaseInsensitivePathHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using JsonPatch;
using JsonPatch.Paths;
using System.Collections.Generic;
using System.Text.Json;

namespace JsonPatch.Tests
{
Expand Down Expand Up @@ -431,6 +432,21 @@ public void SetValueFromPath_SimplePathAddValueToNull_UpdatesValue()
Assert.AreEqual("New Value", entity.Foo);
}

[TestMethod]
public void SetValueFromPath_NonConvertableToPrimative()
{
//arrange
var entity = new SimpleEntity { BooleanValue = false };

var document = JsonDocument.Parse("true");

//act
resolver.SetValueFromPath(typeof(SimpleEntity), "/booleanValue", entity, document.RootElement, JsonPatchOperationType.add);

//assert
Assert.AreEqual(true, entity.BooleanValue);
}

[TestMethod]
public void SetValueFromPath_SimplePathAddValueToNonNull_UpdatesValue()
{
Expand Down
2 changes: 2 additions & 0 deletions src/JsonPatch.Tests/Entitys/SimpleEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ public class SimpleEntity

[JsonPropertyName("jsonProperty")]
public string Car { get; set; }

public bool BooleanValue { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/JsonPatch/JsonValueConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class JsonValueConverter : IValueConverter
{
public object ConvertTo(object value, Type type)
{
if(type.IsPrimitive)
if(type.IsPrimitive && value is IConvertible)
{
return Convert.ChangeType(value, type, CultureInfo.InvariantCulture);
}
Expand Down
2 changes: 1 addition & 1 deletion src/JsonPatchCore/JsonValueConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class JsonValueConverter : IValueConverter
public object ConvertTo(object value, Type type)
{
#pragma warning disable CS8604, CS8603
if (type.IsPrimitive)
if (type.IsPrimitive && value is IConvertible)
{
return Convert.ChangeType(value, type, CultureInfo.InvariantCulture);
}
Expand Down

0 comments on commit 17611a6

Please sign in to comment.