From e46ba481c8b281b04fbb682ca4db92bee6799d24 Mon Sep 17 00:00:00 2001 From: Jass Bagga Date: Thu, 19 Oct 2017 13:32:04 -0700 Subject: [PATCH] Throw JsonSerializationException from converters (#116) --- .../Converters/JsonPatchDocumentConverter.cs | 2 +- .../TypedJsonPatchDocumentConverter.cs | 3 +-- .../Properties/Resources.Designer.cs | 8 +++---- .../Resources.resx | 2 +- .../ObjectAdapterTests.cs | 24 +++++++++++++++---- 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/Microsoft.AspNetCore.JsonPatch/Converters/JsonPatchDocumentConverter.cs b/src/Microsoft.AspNetCore.JsonPatch/Converters/JsonPatchDocumentConverter.cs index e93e84e12a..aed9c48474 100644 --- a/src/Microsoft.AspNetCore.JsonPatch/Converters/JsonPatchDocumentConverter.cs +++ b/src/Microsoft.AspNetCore.JsonPatch/Converters/JsonPatchDocumentConverter.cs @@ -57,7 +57,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Converters } catch (Exception ex) { - throw new JsonPatchException(Resources.FormatInvalidJsonPatchDocument(objectType.Name), ex); + throw new JsonSerializationException(Resources.InvalidJsonPatchDocument, ex); } } diff --git a/src/Microsoft.AspNetCore.JsonPatch/Converters/TypedJsonPatchDocumentConverter.cs b/src/Microsoft.AspNetCore.JsonPatch/Converters/TypedJsonPatchDocumentConverter.cs index 292a3be556..fd779ba4ee 100644 --- a/src/Microsoft.AspNetCore.JsonPatch/Converters/TypedJsonPatchDocumentConverter.cs +++ b/src/Microsoft.AspNetCore.JsonPatch/Converters/TypedJsonPatchDocumentConverter.cs @@ -4,7 +4,6 @@ using System; using System.Collections.Generic; using System.Reflection; -using Microsoft.AspNetCore.JsonPatch.Exceptions; using Microsoft.AspNetCore.JsonPatch.Operations; using Newtonsoft.Json; using Newtonsoft.Json.Linq; @@ -58,7 +57,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Converters } catch (Exception ex) { - throw new JsonPatchException(Resources.FormatInvalidJsonPatchDocument(objectType.Name), ex); + throw new JsonSerializationException(Resources.InvalidJsonPatchDocument, ex); } } } diff --git a/src/Microsoft.AspNetCore.JsonPatch/Properties/Resources.Designer.cs b/src/Microsoft.AspNetCore.JsonPatch/Properties/Resources.Designer.cs index cfb8e087d0..c314465238 100644 --- a/src/Microsoft.AspNetCore.JsonPatch/Properties/Resources.Designer.cs +++ b/src/Microsoft.AspNetCore.JsonPatch/Properties/Resources.Designer.cs @@ -123,7 +123,7 @@ namespace Microsoft.AspNetCore.JsonPatch => string.Format(CultureInfo.CurrentCulture, GetString("InvalidIndexValue"), p0); /// - /// The type '{0}' was malformed and could not be parsed. + /// The JSON patch document was malformed and could not be parsed. /// internal static string InvalidJsonPatchDocument { @@ -131,10 +131,10 @@ namespace Microsoft.AspNetCore.JsonPatch } /// - /// The type '{0}' was malformed and could not be parsed. + /// The JSON patch document was malformed and could not be parsed. /// - internal static string FormatInvalidJsonPatchDocument(object p0) - => string.Format(CultureInfo.CurrentCulture, GetString("InvalidJsonPatchDocument"), p0); + internal static string FormatInvalidJsonPatchDocument() + => GetString("InvalidJsonPatchDocument"); /// /// Invalid JsonPatch operation '{0}'. diff --git a/src/Microsoft.AspNetCore.JsonPatch/Resources.resx b/src/Microsoft.AspNetCore.JsonPatch/Resources.resx index 3763e4a841..87cc399c62 100644 --- a/src/Microsoft.AspNetCore.JsonPatch/Resources.resx +++ b/src/Microsoft.AspNetCore.JsonPatch/Resources.resx @@ -142,7 +142,7 @@ The path segment '{0}' is invalid for an array index. - The type '{0}' was malformed and could not be parsed. + The JSON patch document was malformed and could not be parsed. Invalid JsonPatch operation '{0}'. diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/ObjectAdapterTests.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/ObjectAdapterTests.cs index d35f8eb4e9..c1d005e897 100644 --- a/test/Microsoft.AspNetCore.JsonPatch.Test/ObjectAdapterTests.cs +++ b/test/Microsoft.AspNetCore.JsonPatch.Test/ObjectAdapterTests.cs @@ -725,7 +725,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Adapters } [Fact] - public void DeserializationMustWorkWithoutEnvelope() + public void Deserialization_Successful_ForValidJsonPatchDocument() { // Arrange var doc = new SimpleObject() @@ -756,19 +756,35 @@ namespace Microsoft.AspNetCore.JsonPatch.Adapters } [Fact] - public void DeserializationMustFailWithEnvelope() + public void Deserialization_Fails_ForInvalidJsonPatchDocument() { // Arrange var serialized = "{\"Operations\": [{ \"op\": \"replace\", \"path\": \"/title\", \"value\": \"New Title\"}]}"; // Act & Assert - var exception = Assert.Throws(() => + var exception = Assert.Throws(() => + { + var deserialized + = JsonConvert.DeserializeObject(serialized); + }); + + Assert.Equal("The JSON patch document was malformed and could not be parsed.", exception.Message); + } + + [Fact] + public void Deserialization_Fails_ForInvalidTypedJsonPatchDocument() + { + // Arrange + var serialized = "{\"Operations\": [{ \"op\": \"replace\", \"path\": \"/title\", \"value\": \"New Title\"}]}"; + + // Act & Assert + var exception = Assert.Throws(() => { var deserialized = JsonConvert.DeserializeObject>(serialized); }); - Assert.Equal("The type 'JsonPatchDocument`1' was malformed and could not be parsed.", exception.Message); + Assert.Equal("The JSON patch document was malformed and could not be parsed.", exception.Message); } [Fact]