Throw JsonSerializationException from converters (#116)

This commit is contained in:
Jass Bagga 2017-10-19 13:32:04 -07:00 committed by GitHub
parent 8eefe0fdc2
commit e46ba481c8
5 changed files with 27 additions and 12 deletions

View File

@ -57,7 +57,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Converters
} }
catch (Exception ex) catch (Exception ex)
{ {
throw new JsonPatchException(Resources.FormatInvalidJsonPatchDocument(objectType.Name), ex); throw new JsonSerializationException(Resources.InvalidJsonPatchDocument, ex);
} }
} }

View File

@ -4,7 +4,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Reflection; using System.Reflection;
using Microsoft.AspNetCore.JsonPatch.Exceptions;
using Microsoft.AspNetCore.JsonPatch.Operations; using Microsoft.AspNetCore.JsonPatch.Operations;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
@ -58,7 +57,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Converters
} }
catch (Exception ex) catch (Exception ex)
{ {
throw new JsonPatchException(Resources.FormatInvalidJsonPatchDocument(objectType.Name), ex); throw new JsonSerializationException(Resources.InvalidJsonPatchDocument, ex);
} }
} }
} }

View File

@ -123,7 +123,7 @@ namespace Microsoft.AspNetCore.JsonPatch
=> string.Format(CultureInfo.CurrentCulture, GetString("InvalidIndexValue"), p0); => string.Format(CultureInfo.CurrentCulture, GetString("InvalidIndexValue"), p0);
/// <summary> /// <summary>
/// The type '{0}' was malformed and could not be parsed. /// The JSON patch document was malformed and could not be parsed.
/// </summary> /// </summary>
internal static string InvalidJsonPatchDocument internal static string InvalidJsonPatchDocument
{ {
@ -131,10 +131,10 @@ namespace Microsoft.AspNetCore.JsonPatch
} }
/// <summary> /// <summary>
/// The type '{0}' was malformed and could not be parsed. /// The JSON patch document was malformed and could not be parsed.
/// </summary> /// </summary>
internal static string FormatInvalidJsonPatchDocument(object p0) internal static string FormatInvalidJsonPatchDocument()
=> string.Format(CultureInfo.CurrentCulture, GetString("InvalidJsonPatchDocument"), p0); => GetString("InvalidJsonPatchDocument");
/// <summary> /// <summary>
/// Invalid JsonPatch operation '{0}'. /// Invalid JsonPatch operation '{0}'.

View File

@ -142,7 +142,7 @@
<value>The path segment '{0}' is invalid for an array index.</value> <value>The path segment '{0}' is invalid for an array index.</value>
</data> </data>
<data name="InvalidJsonPatchDocument" xml:space="preserve"> <data name="InvalidJsonPatchDocument" xml:space="preserve">
<value>The type '{0}' was malformed and could not be parsed.</value> <value>The JSON patch document was malformed and could not be parsed.</value>
</data> </data>
<data name="InvalidJsonPatchOperation" xml:space="preserve"> <data name="InvalidJsonPatchOperation" xml:space="preserve">
<value>Invalid JsonPatch operation '{0}'.</value> <value>Invalid JsonPatch operation '{0}'.</value>

View File

@ -725,7 +725,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Adapters
} }
[Fact] [Fact]
public void DeserializationMustWorkWithoutEnvelope() public void Deserialization_Successful_ForValidJsonPatchDocument()
{ {
// Arrange // Arrange
var doc = new SimpleObject() var doc = new SimpleObject()
@ -756,19 +756,35 @@ namespace Microsoft.AspNetCore.JsonPatch.Adapters
} }
[Fact] [Fact]
public void DeserializationMustFailWithEnvelope() public void Deserialization_Fails_ForInvalidJsonPatchDocument()
{ {
// Arrange // Arrange
var serialized = "{\"Operations\": [{ \"op\": \"replace\", \"path\": \"/title\", \"value\": \"New Title\"}]}"; var serialized = "{\"Operations\": [{ \"op\": \"replace\", \"path\": \"/title\", \"value\": \"New Title\"}]}";
// Act & Assert // Act & Assert
var exception = Assert.Throws<JsonPatchException>(() => var exception = Assert.Throws<JsonSerializationException>(() =>
{
var deserialized
= JsonConvert.DeserializeObject<JsonPatchDocument>(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<JsonSerializationException>(() =>
{ {
var deserialized var deserialized
= JsonConvert.DeserializeObject<JsonPatchDocument<SimpleObject>>(serialized); = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleObject>>(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] [Fact]