From b6363c70e6dd3aa9113b264c4944d94385765bb6 Mon Sep 17 00:00:00 2001 From: Tyler Brinkley Date: Wed, 3 May 2017 03:37:51 -0500 Subject: [PATCH] Stops requiring JsonIgnore properties with ItemRequired (#1302) * Made JsonIgnore properties not required when using ItemRequired. * Update JsonSerializerInternalReader.cs * Update JsonSerializerInternalReader.cs * Update JsonSerializerInternalReader.cs * Remove end of file newline --- Src/Newtonsoft.Json.Tests/JsonConvertTest.cs | 37 +++++++++++++++++++ .../JsonSerializerInternalReader.cs | 2 +- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/Src/Newtonsoft.Json.Tests/JsonConvertTest.cs b/Src/Newtonsoft.Json.Tests/JsonConvertTest.cs index bd73abfa..bde20add 100644 --- a/Src/Newtonsoft.Json.Tests/JsonConvertTest.cs +++ b/Src/Newtonsoft.Json.Tests/JsonConvertTest.cs @@ -1761,5 +1761,42 @@ namespace Newtonsoft.Json.Tests IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } + + [Test] + public void ShouldNotRequireIgnoredPropertiesWithItemsRequired() + { + string json = @"{ + ""exp"": 1483228800, + ""active"": true +}"; + ItemsRequiredObjectWithIgnoredProperty value = JsonConvert.DeserializeObject(json); + Assert.IsNotNull(value); + Assert.AreEqual(value.Expiration, new DateTime(2017, 1, 1, 0, 0, 0, DateTimeKind.Utc)); + Assert.AreEqual(value.Active, true); + } + + [JsonObject(ItemRequired = Required.Always)] + public sealed class ItemsRequiredObjectWithIgnoredProperty + { + private static readonly DateTime s_unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); + + [JsonProperty("exp")] + private int _expiration + { + get + { + return (int)((Expiration - s_unixEpoch).TotalSeconds); + } + set + { + Expiration = s_unixEpoch.AddSeconds(value); + } + } + + public bool Active { get; set; } + + [JsonIgnore] + public DateTime Expiration { get; set; } + } } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs index 0c64e0f2..9cb8f7d2 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs @@ -2523,7 +2523,7 @@ namespace Newtonsoft.Json.Serialization { try { - Required resolvedRequired = property._required ?? contract.ItemRequired ?? Required.Default; + Required resolvedRequired = property.Ignored ? Required.Default : property._required ?? contract.ItemRequired ?? Required.Default; switch (presence) {