From 7644768580a1397b3fd0f353c18f2ec78b7a61dd Mon Sep 17 00:00:00 2001 From: stakx Date: Mon, 26 Dec 2016 12:15:34 +0100 Subject: [PATCH] Make JsonValidatingReader close underlying reader Add `Close` method override in `JsonValidatingReader` so that the `CloseInput` property is observed properly. Also add a corresponding unit test. --- .../JsonValidatingReaderTests.cs | 11 +++++ .../Newtonsoft.Json.Tests.Net20.csproj | 1 + .../Newtonsoft.Json.Tests.Net35.csproj | 1 + .../Newtonsoft.Json.Tests.Net40.csproj | 1 + .../Newtonsoft.Json.Tests.Portable.csproj | 1 + .../Newtonsoft.Json.Tests.Portable40.csproj | 1 + .../Newtonsoft.Json.Tests.csproj | 1 + .../TestObjects/JsonReaderStubWithIsClosed.cs | 44 +++++++++++++++++++ Src/Newtonsoft.Json/JsonValidatingReader.cs | 13 ++++++ 9 files changed, 74 insertions(+) create mode 100644 Src/Newtonsoft.Json.Tests/TestObjects/JsonReaderStubWithIsClosed.cs diff --git a/Src/Newtonsoft.Json.Tests/JsonValidatingReaderTests.cs b/Src/Newtonsoft.Json.Tests/JsonValidatingReaderTests.cs index 68002707..89fcd431 100644 --- a/Src/Newtonsoft.Json.Tests/JsonValidatingReaderTests.cs +++ b/Src/Newtonsoft.Json.Tests/JsonValidatingReaderTests.cs @@ -1747,6 +1747,17 @@ namespace Newtonsoft.Json.Tests Assert.AreEqual(JsonToken.None, reader.TokenType); Assert.AreEqual(null, validationEventArgs); } + + [Test] + public void CloseAlsoClosesUnderlyingReader() + { + var underlyingReader = new TestObjects.JsonReaderStubWithIsClosed(); + var validatingReader = new JsonValidatingReader(underlyingReader) { CloseInput = true }; + + validatingReader.Close(); + + Assert.IsTrue(underlyingReader.IsClosed); + } } } diff --git a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Net20.csproj b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Net20.csproj index aa044cb3..97cb0241 100644 --- a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Net20.csproj +++ b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Net20.csproj @@ -171,6 +171,7 @@ + diff --git a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Net35.csproj b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Net35.csproj index b9753446..f123654c 100644 --- a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Net35.csproj +++ b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Net35.csproj @@ -208,6 +208,7 @@ + diff --git a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Net40.csproj b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Net40.csproj index 0dc294d1..4f038671 100644 --- a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Net40.csproj +++ b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Net40.csproj @@ -329,6 +329,7 @@ + diff --git a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Portable.csproj b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Portable.csproj index abe4d8c9..206e25dc 100644 --- a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Portable.csproj +++ b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Portable.csproj @@ -201,6 +201,7 @@ + diff --git a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Portable40.csproj b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Portable40.csproj index bf37a445..13e3d10b 100644 --- a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Portable40.csproj +++ b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Portable40.csproj @@ -205,6 +205,7 @@ + diff --git a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj index a3f3819d..70194a20 100644 --- a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj +++ b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj @@ -224,6 +224,7 @@ + diff --git a/Src/Newtonsoft.Json.Tests/TestObjects/JsonReaderStubWithIsClosed.cs b/Src/Newtonsoft.Json.Tests/TestObjects/JsonReaderStubWithIsClosed.cs new file mode 100644 index 00000000..d3bcd17b --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/TestObjects/JsonReaderStubWithIsClosed.cs @@ -0,0 +1,44 @@ +#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 + +using System; + +namespace Newtonsoft.Json.Tests.TestObjects +{ + public sealed class JsonReaderStubWithIsClosed : JsonReader + { + public bool IsClosed { get; private set; } + + public override void Close() + { + IsClosed = true; + } + + public override bool Read() + { + throw new NotSupportedException(); + } + } +} diff --git a/Src/Newtonsoft.Json/JsonValidatingReader.cs b/Src/Newtonsoft.Json/JsonValidatingReader.cs index d2e5b862..5cc1b7ca 100644 --- a/Src/Newtonsoft.Json/JsonValidatingReader.cs +++ b/Src/Newtonsoft.Json/JsonValidatingReader.cs @@ -351,6 +351,19 @@ namespace Newtonsoft.Json get { return _reader; } } + /// + /// Changes the reader's state to . + /// If is set to true, the underlying is also closed. + /// + public override void Close() + { + base.Close(); + if (CloseInput && _reader != null) + { + _reader.Close(); + } + } + private void ValidateNotDisallowed(JsonSchemaModel schema) { if (schema == null)