From 1ba5c13233400a49b84f52b9a5532427ade2c60e Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Sat, 11 Feb 2023 21:57:16 +1100 Subject: [PATCH] Fix deserializing via constructor with ignored base type properties https://github.com/JamesNK/Newtonsoft.Json/pull/2711 --- .../JsonSerializerInternalReader.cs | 5 +++ src/Tests/Issues/Issue2708.cs | 38 +++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 src/Tests/Issues/Issue2708.cs diff --git a/src/Argon/Serialization/JsonSerializerInternalReader.cs b/src/Argon/Serialization/JsonSerializerInternalReader.cs index 1a321b2ba..28f84c9f3 100644 --- a/src/Argon/Serialization/JsonSerializerInternalReader.cs +++ b/src/Argon/Serialization/JsonSerializerInternalReader.cs @@ -1961,6 +1961,11 @@ List ResolvePropertyAndCreatorValues(JsonObjectContract continue; } + + if (!reader.Read()) + { + throw JsonSerializationException.Create(reader, $"Unexpected end when setting {memberName}'s value."); + } } else { diff --git a/src/Tests/Issues/Issue2708.cs b/src/Tests/Issues/Issue2708.cs new file mode 100644 index 000000000..26dbfad96 --- /dev/null +++ b/src/Tests/Issues/Issue2708.cs @@ -0,0 +1,38 @@ +// Copyright (c) 2007 James Newton-King. All rights reserved. +// Use of this source code is governed by The MIT License, +// as found in the license.md file. + +public class Issue2708 : TestFixtureBase +{ + [Fact] + public void Test() + { + string json = @" +{ + ""Name"": ""MyName"", + ""ChildClassProp"": ""MyValue"", +}"; + + var record = JsonConvert.DeserializeObject(json); + Assert.Equal(null, record.Name); // Not set because doesn't have DataMember + Assert.Equal("MyValue", record.ChildClassProp); + } + + [DataContract] + public abstract class RecordBase + { + [JsonExtensionData] + protected IDictionary additionalData; + + public string Name { get; set; } + } + + [DataContract] + public class MyRecord : RecordBase + { + public MyRecord(string childClassProp) => ChildClassProp = childClassProp; + + [DataMember] + public string ChildClassProp { get; set; } + } +} \ No newline at end of file