Skip to content

Commit

Permalink
Fix missing error when deserializing JToken with a contract type mism…
Browse files Browse the repository at this point in the history
…atch (#2494)
  • Loading branch information
JamesNK authored Mar 11, 2021
1 parent b6dc05b commit 583eb12
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
55 changes: 55 additions & 0 deletions Src/Newtonsoft.Json.Tests/Issues/Issue2484.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

#if (NET45 || NET50)
#if DNXCORE50
using Xunit;
using Test = Xunit.FactAttribute;
using Assert = Newtonsoft.Json.Tests.XUnitAssert;
#else
using NUnit.Framework;
#endif
using System.Collections.Generic;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json.Converters;
using System.Collections;
using System;
using Newtonsoft.Json.Linq;

namespace Newtonsoft.Json.Tests.Issues
{
[TestFixture]
public class Issue2484
{
[Test]
public void Test()
{
var json = "[]";
var ex = ExceptionAssert.Throws<JsonSerializationException>(() => JsonConvert.DeserializeObject(json, typeof(JObject)));
Assert.AreEqual("Deserialized JSON type 'Newtonsoft.Json.Linq.JArray' is not compatible with expected type 'Newtonsoft.Json.Linq.JObject'. Path '', line 1, position 2.", ex.Message);
}
}
}
#endif
8 changes: 2 additions & 6 deletions Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3515,13 +3515,9 @@ public void CannotDeserializeArrayIntoLinqToJson()
{
string json = @"[]";

ExceptionAssert.Throws<InvalidCastException>(
ExceptionAssert.Throws<JsonSerializationException>(
() => { JsonConvert.DeserializeObject<JObject>(json); },
new[]
{
"Unable to cast object of type 'Newtonsoft.Json.Linq.JArray' to type 'Newtonsoft.Json.Linq.JObject'.",
"Cannot cast from source type to destination type." // mono
});
"Deserialized JSON type 'Newtonsoft.Json.Linq.JArray' is not compatible with expected type 'Newtonsoft.Json.Linq.JObject'. Path '', line 1, position 2.");
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,15 @@ private JsonSerializerProxy GetInternalSerializer()
token = writer.Token;
}

if (contract != null && token != null)
{
if (!contract.UnderlyingType.IsAssignableFrom(token.GetType()))
{
throw JsonSerializationException.Create(reader, "Deserialized JSON type '{0}' is not compatible with expected type '{1}'."
.FormatWith(CultureInfo.InvariantCulture, token.GetType().FullName, contract.UnderlyingType.FullName));
}
}

return token;
}

Expand Down

0 comments on commit 583eb12

Please sign in to comment.