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)
{
throw new JsonPatchException(Resources.FormatInvalidJsonPatchDocument(objectType.Name), ex);
throw new JsonSerializationException(Resources.InvalidJsonPatchDocument, ex);
}
}

View File

@ -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);
}
}
}

View File

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

View File

@ -142,7 +142,7 @@
<value>The path segment '{0}' is invalid for an array index.</value>
</data>
<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 name="InvalidJsonPatchOperation" xml:space="preserve">
<value>Invalid JsonPatch operation '{0}'.</value>

View File

@ -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<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
= 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]