Skip to content

Commit

Permalink
-Fixed handling error when reading metadata property
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK committed Apr 1, 2017
1 parent 33cf71d commit f88ae28
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,54 @@ namespace Newtonsoft.Json.Tests.Serialization
[TestFixture]
public class SerializationErrorHandlingTests : TestFixtureBase
{
[Test]
public void ErrorHandlingMetadata()
{
List<Exception> errors = new List<Exception>();

AAA a2 = JsonConvert.DeserializeObject<AAA>(@"{""MyTest"":{""$type"":""<Namespace>.JsonTest+MyTest2, <Assembly>""}}", new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto,
Error = (object sender, Json.Serialization.ErrorEventArgs e) =>
{
errors.Add(e.ErrorContext.Error);
e.ErrorContext.Handled = true;
}
});

Assert.IsNotNull(a2);
Assert.AreEqual(1, errors.Count);
Assert.AreEqual("Error resolving type specified in JSON '<Namespace>.JsonTest+MyTest2, <Assembly>'. Path 'MyTest.$type', line 1, position 61.", errors[0].Message);
}

[Test]
public void ErrorHandlingMetadata_TopLevel()
{
List<Exception> errors = new List<Exception>();

JObject a2 = (JObject)JsonConvert.DeserializeObject(@"{""$type"":""<Namespace>.JsonTest+MyTest2, <Assembly>""}", new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto,
Error = (object sender, Json.Serialization.ErrorEventArgs e) =>
{
errors.Add(e.ErrorContext.Error);
e.ErrorContext.Handled = true;
}
});

Assert.IsNull(a2);
Assert.AreEqual(1, errors.Count);
Assert.AreEqual("Error resolving type specified in JSON '<Namespace>.JsonTest+MyTest2, <Assembly>'. Path '$type', line 1, position 51.", errors[0].Message);
}

public class AAA
{
public ITest MyTest { get; set; }
}

public interface ITest { }
public class MyTest : ITest { }

public class MyClass1
{
[JsonProperty("myint")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2408,7 +2408,7 @@ private object PopulateObject(object newObject, JsonReader reader, JsonObjectCon
{
if (IsErrorHandled(newObject, contract, memberName, reader as IJsonLineInfo, reader.Path, ex))
{
HandleError(reader, true, initialDepth);
HandleError(reader, true, initialDepth - 1);
}
else
{
Expand Down

0 comments on commit f88ae28

Please sign in to comment.