Skip to content

Commit cb9eed9

Browse files
authored
Fix deserializing via constructor with ignored base type properties (#2711)
1 parent 94ff24f commit cb9eed9

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#region License
2+
// Copyright (c) 2007 James Newton-King
3+
//
4+
// Permission is hereby granted, free of charge, to any person
5+
// obtaining a copy of this software and associated documentation
6+
// files (the "Software"), to deal in the Software without
7+
// restriction, including without limitation the rights to use,
8+
// copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the
10+
// Software is furnished to do so, subject to the following
11+
// conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be
14+
// included in all copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18+
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21+
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
// OTHER DEALINGS IN THE SOFTWARE.
24+
#endregion
25+
26+
#if !NET20
27+
using System;
28+
using System.Collections.Generic;
29+
using System.IO;
30+
using System.Runtime.Serialization;
31+
using System.Text;
32+
using System.Threading;
33+
using Newtonsoft.Json.Linq;
34+
#if DNXCORE50
35+
using System.Reflection;
36+
using Xunit;
37+
using Test = Xunit.FactAttribute;
38+
using Assert = Newtonsoft.Json.Tests.XUnitAssert;
39+
#else
40+
using NUnit.Framework;
41+
#endif
42+
43+
namespace Newtonsoft.Json.Tests.Issues
44+
{
45+
[TestFixture]
46+
public class Issue2708 : TestFixtureBase
47+
{
48+
[Test]
49+
public void Test()
50+
{
51+
string json = @"
52+
{
53+
""Name"": ""MyName"",
54+
""ChildClassProp"": ""MyValue"",
55+
}";
56+
57+
var record = JsonConvert.DeserializeObject<MyRecord>(json);
58+
Assert.AreEqual(null, record.Name); // Not set because doesn't have DataMember
59+
Assert.AreEqual("MyValue", record.ChildClassProp);
60+
}
61+
62+
[DataContract]
63+
public abstract class RecordBase
64+
{
65+
[JsonExtensionData]
66+
protected IDictionary<string, JToken> additionalData;
67+
68+
public string Name { get; set; }
69+
}
70+
71+
[DataContract]
72+
public class MyRecord : RecordBase
73+
{
74+
public MyRecord(string childClassProp) => ChildClassProp = childClassProp;
75+
76+
[DataMember]
77+
public string ChildClassProp { get; set; }
78+
}
79+
}
80+
}
81+
#endif

Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs

+7
Original file line numberDiff line numberDiff line change
@@ -2244,6 +2244,13 @@ private List<CreatorPropertyContext> ResolvePropertyAndCreatorValues(JsonObjectC
22442244

22452245
continue;
22462246
}
2247+
else
2248+
{
2249+
if (!reader.Read())
2250+
{
2251+
throw JsonSerializationException.Create(reader, "Unexpected end when setting {0}'s value.".FormatWith(CultureInfo.InvariantCulture, memberName));
2252+
}
2253+
}
22472254
}
22482255
else
22492256
{

0 commit comments

Comments
 (0)