Refactoring to get ready for JsonPatch (for dynamics) support. Relay generic typing to outer-facing contract.

This commit is contained in:
KevinDockx 2015-07-07 15:39:06 +02:00 committed by Ryan Nowak
parent 46bc7dd219
commit 580914d873
11 changed files with 229 additions and 276 deletions

View File

@ -8,13 +8,12 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
/// <summary> /// <summary>
/// Defines the operations that can be performed on a JSON patch document. /// Defines the operations that can be performed on a JSON patch document.
/// </summary> /// </summary>
/// <typeparam name="TModel">The type of the model.</typeparam> public interface IObjectAdapter
public interface IObjectAdapter<TModel> where TModel : class
{ {
void Add(Operation<TModel> operation, TModel objectToApplyTo); void Add(Operation operation, object objectToApplyTo);
void Copy(Operation<TModel> operation, TModel objectToApplyTo); void Copy(Operation operation, object objectToApplyTo);
void Move(Operation<TModel> operation, TModel objectToApplyTo); void Move(Operation operation, object objectToApplyTo);
void Remove(Operation<TModel> operation, TModel objectToApplyTo); void Remove(Operation operation, object objectToApplyTo);
void Replace(Operation<TModel> operation, TModel objectToApplyTo); void Replace(Operation operation, object objectToApplyTo);
} }
} }

View File

@ -15,16 +15,16 @@ using Newtonsoft.Json.Serialization;
namespace Microsoft.AspNet.JsonPatch.Adapters namespace Microsoft.AspNet.JsonPatch.Adapters
{ {
/// <inheritdoc /> /// <inheritdoc />
public class ObjectAdapter<TModel> : IObjectAdapter<TModel> where TModel : class public class ObjectAdapter : IObjectAdapter
{ {
/// <summary> /// <summary>
/// Initializes a new instance of <see cref="ObjectAdapter{TModel}"/>. /// Initializes a new instance of <see cref="ObjectAdapter"/>.
/// </summary> /// </summary>
/// <param name="contractResolver">The <see cref="IContractResolver"/>.</param> /// <param name="contractResolver">The <see cref="IContractResolver"/>.</param>
/// <param name="logErrorAction">The <see cref="Action"/> for logging <see cref="JsonPatchError{TModel}"/>.</param> /// <param name="logErrorAction">The <see cref="Action"/> for logging <see cref="JsonPatchError"/>.</param>
public ObjectAdapter( public ObjectAdapter(
[NotNull] IContractResolver contractResolver, [NotNull] IContractResolver contractResolver,
Action<JsonPatchError<TModel>> logErrorAction) Action<JsonPatchError> logErrorAction)
{ {
ContractResolver = contractResolver; ContractResolver = contractResolver;
LogErrorAction = logErrorAction; LogErrorAction = logErrorAction;
@ -36,9 +36,9 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
public IContractResolver ContractResolver { get; } public IContractResolver ContractResolver { get; }
/// <summary> /// <summary>
/// Action for logging <see cref="JsonPatchError{TModel}"/>. /// Action for logging <see cref="JsonPatchError"/>.
/// </summary> /// </summary>
public Action<JsonPatchError<TModel>> LogErrorAction { get; } public Action<JsonPatchError> LogErrorAction { get; }
/// <summary> /// <summary>
/// The "add" operation performs one of the following functions, /// The "add" operation performs one of the following functions,
@ -100,7 +100,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
/// </summary> /// </summary>
/// <param name="operation">The add operation.</param> /// <param name="operation">The add operation.</param>
/// <param name="objectToApplyTo">Object to apply the operation to.</param> /// <param name="objectToApplyTo">Object to apply the operation to.</param>
public void Add([NotNull] Operation<TModel> operation, [NotNull] TModel objectToApplyTo) public void Add([NotNull] Operation operation, [NotNull] object objectToApplyTo)
{ {
Add(operation.path, operation.value, objectToApplyTo, operation); Add(operation.path, operation.value, objectToApplyTo, operation);
} }
@ -112,8 +112,8 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
private void Add( private void Add(
[NotNull] string path, [NotNull] string path,
object value, object value,
[NotNull] TModel objectToApplyTo, [NotNull] object objectToApplyTo,
[NotNull] Operation<TModel> operationToReport) [NotNull] Operation operationToReport)
{ {
// add, in this implementation, does not just "add" properties - that's // add, in this implementation, does not just "add" properties - that's
// technically impossible; It can however be used to add items to arrays, // technically impossible; It can however be used to add items to arrays,
@ -184,7 +184,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
} }
else else
{ {
LogError(new JsonPatchError<TModel>( LogError(new JsonPatchError(
objectToApplyTo, objectToApplyTo,
operationToReport, operationToReport,
Resources.FormatInvalidIndexForArrayProperty(operationToReport.op, path))); Resources.FormatInvalidIndexForArrayProperty(operationToReport.op, path)));
@ -195,7 +195,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
} }
else else
{ {
LogError(new JsonPatchError<TModel>( LogError(new JsonPatchError(
objectToApplyTo, objectToApplyTo,
operationToReport, operationToReport,
Resources.FormatInvalidIndexForArrayProperty(operationToReport.op, path))); Resources.FormatInvalidIndexForArrayProperty(operationToReport.op, path)));
@ -244,7 +244,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
/// </summary> /// </summary>
/// <param name="operation">The move operation.</param> /// <param name="operation">The move operation.</param>
/// <param name="objectToApplyTo">Object to apply the operation to.</param> /// <param name="objectToApplyTo">Object to apply the operation to.</param>
public void Move([NotNull] Operation<TModel> operation, [NotNull] TModel objectToApplyTo) public void Move([NotNull] Operation operation, [NotNull] object objectToApplyTo)
{ {
// get value at from location // get value at from location
object valueAtFromLocation = null; object valueAtFromLocation = null;
@ -281,7 +281,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
if (array.Count <= positionAsInteger) if (array.Count <= positionAsInteger)
{ {
LogError(new JsonPatchError<TModel>( LogError(new JsonPatchError(
objectToApplyTo, objectToApplyTo,
operation, operation,
Resources.FormatInvalidIndexForArrayProperty(operation.op, operation.from))); Resources.FormatInvalidIndexForArrayProperty(operation.op, operation.from)));
@ -293,7 +293,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
} }
else else
{ {
LogError(new JsonPatchError<TModel>( LogError(new JsonPatchError(
objectToApplyTo, objectToApplyTo,
operation, operation,
Resources.FormatInvalidPathForArrayProperty(operation.op, operation.from))); Resources.FormatInvalidPathForArrayProperty(operation.op, operation.from)));
@ -329,7 +329,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
/// </summary> /// </summary>
/// <param name="operation">The remove operation.</param> /// <param name="operation">The remove operation.</param>
/// <param name="objectToApplyTo">Object to apply the operation to.</param> /// <param name="objectToApplyTo">Object to apply the operation to.</param>
public void Remove([NotNull] Operation<TModel> operation, [NotNull] TModel objectToApplyTo) public void Remove([NotNull] Operation operation, [NotNull] object objectToApplyTo)
{ {
Remove(operation.path, objectToApplyTo, operation); Remove(operation.path, objectToApplyTo, operation);
} }
@ -340,8 +340,8 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
/// </summary> /// </summary>
private void Remove( private void Remove(
[NotNull] string path, [NotNull] string path,
[NotNull] TModel objectToApplyTo, [NotNull] object objectToApplyTo,
[NotNull] Operation<TModel> operationToReport) [NotNull] Operation operationToReport)
{ {
var removeFromList = false; var removeFromList = false;
var positionAsInteger = -1; var positionAsInteger = -1;
@ -397,7 +397,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
} }
else else
{ {
LogError(new JsonPatchError<TModel>( LogError(new JsonPatchError(
objectToApplyTo, objectToApplyTo,
operationToReport, operationToReport,
Resources.FormatInvalidIndexForArrayProperty(operationToReport.op, path))); Resources.FormatInvalidIndexForArrayProperty(operationToReport.op, path)));
@ -408,7 +408,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
} }
else else
{ {
LogError(new JsonPatchError<TModel>( LogError(new JsonPatchError(
objectToApplyTo, objectToApplyTo,
operationToReport, operationToReport,
Resources.FormatInvalidPathForArrayProperty(operationToReport.op, path))); Resources.FormatInvalidPathForArrayProperty(operationToReport.op, path)));
@ -452,7 +452,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
/// </summary> /// </summary>
/// <param name="operation">The replace operation.</param> /// <param name="operation">The replace operation.</param>
/// <param name="objectToApplyTo">Object to apply the operation to.</param> /// <param name="objectToApplyTo">Object to apply the operation to.</param>
public void Replace([NotNull] Operation<TModel> operation, [NotNull] TModel objectToApplyTo) public void Replace([NotNull] Operation operation, [NotNull] object objectToApplyTo)
{ {
Remove(operation.path, objectToApplyTo, operation); Remove(operation.path, objectToApplyTo, operation);
Add(operation.path, operation.value, objectToApplyTo, operation); Add(operation.path, operation.value, objectToApplyTo, operation);
@ -480,7 +480,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
/// </summary> /// </summary>
/// <param name="operation">The copy operation.</param> /// <param name="operation">The copy operation.</param>
/// <param name="objectToApplyTo">Object to apply the operation to.</param> /// <param name="objectToApplyTo">Object to apply the operation to.</param>
public void Copy([NotNull] Operation<TModel> operation, [NotNull] TModel objectToApplyTo) public void Copy([NotNull] Operation operation, [NotNull] object objectToApplyTo)
{ {
// get value at from location // get value at from location
object valueAtFromLocation = null; object valueAtFromLocation = null;
@ -518,7 +518,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
if (array.Count <= positionAsInteger) if (array.Count <= positionAsInteger)
{ {
LogError(new JsonPatchError<TModel>( LogError(new JsonPatchError(
objectToApplyTo, objectToApplyTo,
operation, operation,
Resources.FormatInvalidIndexForArrayProperty(operation.op, operation.from))); Resources.FormatInvalidIndexForArrayProperty(operation.op, operation.from)));
@ -530,7 +530,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
} }
else else
{ {
LogError(new JsonPatchError<TModel>( LogError(new JsonPatchError(
objectToApplyTo, objectToApplyTo,
operation, operation,
Resources.FormatInvalidPathForArrayProperty(operation.op, operation.from))); Resources.FormatInvalidPathForArrayProperty(operation.op, operation.from)));
@ -551,13 +551,13 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
private bool CheckIfPropertyExists( private bool CheckIfPropertyExists(
JsonPatchProperty patchProperty, JsonPatchProperty patchProperty,
TModel objectToApplyTo, object objectToApplyTo,
Operation<TModel> operation, Operation operation,
string propertyPath) string propertyPath)
{ {
if (patchProperty == null) if (patchProperty == null)
{ {
LogError(new JsonPatchError<TModel>( LogError(new JsonPatchError(
objectToApplyTo, objectToApplyTo,
operation, operation,
Resources.FormatPropertyDoesNotExist(propertyPath))); Resources.FormatPropertyDoesNotExist(propertyPath)));
@ -567,7 +567,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
if (patchProperty.Property.Ignored) if (patchProperty.Property.Ignored)
{ {
LogError(new JsonPatchError<TModel>( LogError(new JsonPatchError(
objectToApplyTo, objectToApplyTo,
operation, operation,
Resources.FormatCannotUpdateProperty(propertyPath))); Resources.FormatCannotUpdateProperty(propertyPath)));
@ -590,13 +590,13 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
private bool CheckIfPropertyCanBeSet( private bool CheckIfPropertyCanBeSet(
ConversionResult result, ConversionResult result,
TModel objectToApplyTo, object objectToApplyTo,
Operation<TModel> operation, Operation operation,
string path) string path)
{ {
if (!result.CanBeConverted) if (!result.CanBeConverted)
{ {
LogError(new JsonPatchError<TModel>( LogError(new JsonPatchError(
objectToApplyTo, objectToApplyTo,
operation, operation,
Resources.FormatInvalidValueForProperty(result.ConvertedInstance, path))); Resources.FormatInvalidValueForProperty(result.ConvertedInstance, path)));
@ -607,7 +607,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
return true; return true;
} }
private void LogError(JsonPatchError<TModel> jsonPatchError) private void LogError(JsonPatchError jsonPatchError)
{ {
if (LogErrorAction != null) if (LogErrorAction != null)
{ {
@ -615,7 +615,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
} }
else else
{ {
throw new JsonPatchException<TModel>(jsonPatchError); throw new JsonPatchException(jsonPatchError);
} }
} }

View File

@ -44,7 +44,7 @@ namespace Microsoft.AspNet.JsonPatch.Converters
var targetOperations = Activator.CreateInstance(concreteList); var targetOperations = Activator.CreateInstance(concreteList);
//Create a new reader for this jObject, and set all properties to match the original reader. //Create a new reader for this jObject, and set all properties to match the original reader.
JsonReader jObjectReader = jObject.CreateReader(); var jObjectReader = jObject.CreateReader();
jObjectReader.Culture = reader.Culture; jObjectReader.Culture = reader.Culture;
jObjectReader.DateParseHandling = reader.DateParseHandling; jObjectReader.DateParseHandling = reader.DateParseHandling;
jObjectReader.DateTimeZoneHandling = reader.DateTimeZoneHandling; jObjectReader.DateTimeZoneHandling = reader.DateTimeZoneHandling;

View File

@ -3,42 +3,36 @@
using System; using System;
using Microsoft.AspNet.JsonPatch.Operations; using Microsoft.AspNet.JsonPatch.Operations;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.JsonPatch.Exceptions namespace Microsoft.AspNet.JsonPatch.Exceptions
{ {
public class JsonPatchException<TModel> : JsonPatchException where TModel : class public class JsonPatchException : Exception
{ {
public Operation<TModel> FailedOperation { get; private set; } public Operation FailedOperation { get; private set; }
public new TModel AffectedObject { get; private set; } public object AffectedObject { get; private set; }
private string _message = string.Empty;
public override string Message
{
get
{
return _message;
}
}
public JsonPatchException() public JsonPatchException()
{ {
} }
public JsonPatchException([NotNull] JsonPatchError<TModel> jsonPatchError) public JsonPatchException(JsonPatchError jsonPatchError, Exception innerException)
: base(jsonPatchError.ErrorMessage, innerException)
{ {
FailedOperation = jsonPatchError.Operation; FailedOperation = jsonPatchError.Operation;
_message = jsonPatchError.ErrorMessage;
AffectedObject = jsonPatchError.AffectedObject; AffectedObject = jsonPatchError.AffectedObject;
} }
public JsonPatchException([NotNull] JsonPatchError<TModel> jsonPatchError, Exception innerException) public JsonPatchException(JsonPatchError jsonPatchError)
: this(jsonPatchError) : this(jsonPatchError, null)
{ {
InnerException = innerException; }
public JsonPatchException(string message, Exception innerException)
: base (message, innerException)
{
} }
} }
} }

View File

@ -1,34 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
namespace Microsoft.AspNet.JsonPatch.Exceptions
{
public class JsonPatchException : Exception
{
public new Exception InnerException { get; internal set; }
public object AffectedObject { get; private set; }
private string _message = string.Empty;
public override string Message
{
get
{
return _message;
}
}
public JsonPatchException()
{
}
public JsonPatchException(string message, Exception innerException)
{
_message = message;
InnerException = innerException;
}
}
}

View File

@ -32,10 +32,8 @@ namespace Microsoft.AspNet.JsonPatch
ContractResolver = new DefaultContractResolver(); ContractResolver = new DefaultContractResolver();
} }
// Create from list of operations. // Create from list of operations
public JsonPatchDocument( public JsonPatchDocument([NotNull] List<Operation<TModel>> operations, [NotNull] IContractResolver contractResolver)
[NotNull] List<Operation<TModel>> operations,
[NotNull] IContractResolver contractResolver)
{ {
Operations = operations; Operations = operations;
ContractResolver = contractResolver; ContractResolver = contractResolver;
@ -45,10 +43,10 @@ namespace Microsoft.AspNet.JsonPatch
/// Add operation. Will result in, for example, /// Add operation. Will result in, for example,
/// { "op": "add", "path": "/a/b/c", "value": [ "foo", "bar" ] } /// { "op": "add", "path": "/a/b/c", "value": [ "foo", "bar" ] }
/// </summary> /// </summary>
/// <typeparam name="TProp">The value type.</typeparam> /// <typeparam name="TProp">value type</typeparam>
/// <param name="path">The path of the property to add.</param> /// <param name="path">path</param>
/// <param name="value">The value to add.</param> /// <param name="value">value</param>
/// <returns>The <see cref="JsonPatchDocument{TModel}"/>.</returns> /// <returns></returns>
public JsonPatchDocument<TModel> Add<TProp>([NotNull] Expression<Func<TModel, TProp>> path, TProp value) public JsonPatchDocument<TModel> Add<TProp>([NotNull] Expression<Func<TModel, TProp>> path, TProp value)
{ {
Operations.Add(new Operation<TModel>( Operations.Add(new Operation<TModel>(
@ -61,13 +59,13 @@ namespace Microsoft.AspNet.JsonPatch
} }
/// <summary> /// <summary>
/// Add value to list at given position. /// Add value to list at given position
/// </summary> /// </summary>
/// <typeparam name="TProp">The value type.</typeparam> /// <typeparam name="TProp">value type</typeparam>
/// <param name="path">The path of the property to add.</param> /// <param name="path">path</param>
/// <param name="value">The value to add.</param> /// <param name="value">value</param>
/// <param name="position">position</param> /// <param name="position">position</param>
/// <returns>The <see cref="JsonPatchDocument{TModel}"/>.</returns> /// <returns></returns>
public JsonPatchDocument<TModel> Add<TProp>( public JsonPatchDocument<TModel> Add<TProp>(
[NotNull] Expression<Func<TModel, IList<TProp>>> path, [NotNull] Expression<Func<TModel, IList<TProp>>> path,
TProp value, TProp value,
@ -83,12 +81,12 @@ namespace Microsoft.AspNet.JsonPatch
} }
/// <summary> /// <summary>
/// At value at end of list. /// At value at end of list
/// </summary> /// </summary>
/// <typeparam name="TProp">The value type.</typeparam> /// <typeparam name="TProp">value type</typeparam>
/// <param name="path">The path of the property to add.</param> /// <param name="path">path</param>
/// <param name="value">The value to add.</param> /// <param name="value">value</param>
/// <returns>The <see cref="JsonPatchDocument{TModel}"/>.</returns> /// <returns></returns>
public JsonPatchDocument<TModel> Add<TProp>([NotNull] Expression<Func<TModel, IList<TProp>>> path, TProp value) public JsonPatchDocument<TModel> Add<TProp>([NotNull] Expression<Func<TModel, IList<TProp>>> path, TProp value)
{ {
Operations.Add(new Operation<TModel>( Operations.Add(new Operation<TModel>(
@ -104,26 +102,24 @@ namespace Microsoft.AspNet.JsonPatch
/// Remove value at target location. Will result in, for example, /// Remove value at target location. Will result in, for example,
/// { "op": "remove", "path": "/a/b/c" } /// { "op": "remove", "path": "/a/b/c" }
/// </summary> /// </summary>
/// <param name="path">The path of the property to remove.</param> /// <param name="remove"></param>
/// <returns>The <see cref="JsonPatchDocument{TModel}"/>.</returns> /// <param name="path"></param>
/// <returns></returns>
public JsonPatchDocument<TModel> Remove<TProp>([NotNull] Expression<Func<TModel, TProp>> path) public JsonPatchDocument<TModel> Remove<TProp>([NotNull] Expression<Func<TModel, TProp>> path)
{ {
Operations.Add( Operations.Add(new Operation<TModel>("remove", ExpressionHelpers.GetPath(path).ToLowerInvariant(), from: null));
new Operation<TModel>("remove", ExpressionHelpers.GetPath(path).ToLowerInvariant(), from: null));
return this; return this;
} }
/// <summary> /// <summary>
/// Remove value from list at given position. /// Remove value from list at given position
/// </summary> /// </summary>
/// <typeparam name="TProp">The value type.</typeparam> /// <typeparam name="TProp">value type</typeparam>
/// <param name="path">The path of the property to remove.</param> /// <param name="path">target location</param>
/// <param name="position">The position in the list.</param> /// <param name="position">position</param>
/// <returns>The <see cref="JsonPatchDocument{TModel}"/>.</returns> /// <returns></returns>
public JsonPatchDocument<TModel> Remove<TProp>( public JsonPatchDocument<TModel> Remove<TProp>([NotNull] Expression<Func<TModel, IList<TProp>>> path, int position)
[NotNull] Expression<Func<TModel, IList<TProp>>> path,
int position)
{ {
Operations.Add(new Operation<TModel>( Operations.Add(new Operation<TModel>(
"remove", "remove",
@ -134,11 +130,11 @@ namespace Microsoft.AspNet.JsonPatch
} }
/// <summary> /// <summary>
/// Remove value from end of list. /// Remove value from end of list
/// </summary> /// </summary>
/// <typeparam name="TProp">The value type.</typeparam> /// <typeparam name="TProp">value type</typeparam>
/// <param name="path">The path of the property to remove.</param> /// <param name="path">target location</param>
/// <returns>The <see cref="JsonPatchDocument{TModel}"/>.</returns> /// <returns></returns>
public JsonPatchDocument<TModel> Remove<TProp>([NotNull] Expression<Func<TModel, IList<TProp>>> path) public JsonPatchDocument<TModel> Remove<TProp>([NotNull] Expression<Func<TModel, IList<TProp>>> path)
{ {
Operations.Add(new Operation<TModel>( Operations.Add(new Operation<TModel>(
@ -153,9 +149,9 @@ namespace Microsoft.AspNet.JsonPatch
/// Replace value. Will result in, for example, /// Replace value. Will result in, for example,
/// { "op": "replace", "path": "/a/b/c", "value": 42 } /// { "op": "replace", "path": "/a/b/c", "value": 42 }
/// </summary> /// </summary>
/// <param name="path">The path of the property to replace.</param> /// <param name="path"></param>
/// <param name="value">The value to replace.</param> /// <param name="value"></param>
/// <returns>The <see cref="JsonPatchDocument{TModel}"/>.</returns> /// <returns></returns>
public JsonPatchDocument<TModel> Replace<TProp>([NotNull] Expression<Func<TModel, TProp>> path, TProp value) public JsonPatchDocument<TModel> Replace<TProp>([NotNull] Expression<Func<TModel, TProp>> path, TProp value)
{ {
Operations.Add(new Operation<TModel>( Operations.Add(new Operation<TModel>(
@ -168,16 +164,14 @@ namespace Microsoft.AspNet.JsonPatch
} }
/// <summary> /// <summary>
/// Replace value in a list at given position. /// Replace value in a list at given position
/// </summary> /// </summary>
/// <typeparam name="TProp">The value type.</typeparam> /// <typeparam name="TProp">value type</typeparam>
/// <param name="path">The path of the property to replace.</param> /// <param name="path">target location</param>
/// <param name="position">The position in the list.</param> /// <param name="position">position</param>
/// <returns>The <see cref="JsonPatchDocument{TModel}"/>.</returns> /// <returns></returns>
public JsonPatchDocument<TModel> Replace<TProp>( public JsonPatchDocument<TModel> Replace<TProp>([NotNull] Expression<Func<TModel, IList<TProp>>> path,
[NotNull] Expression<Func<TModel, IList<TProp>>> path, TProp value, int position)
TProp value,
int position)
{ {
Operations.Add(new Operation<TModel>( Operations.Add(new Operation<TModel>(
"replace", "replace",
@ -189,14 +183,12 @@ namespace Microsoft.AspNet.JsonPatch
} }
/// <summary> /// <summary>
/// Replace value at end of a list. /// Replace value at end of a list
/// </summary> /// </summary>
/// <typeparam name="TProp">The value type.</typeparam> /// <typeparam name="TProp">value type</typeparam>
/// <param name="path">The path of the property to replace.</param> /// <param name="path">target location</param>
/// <returns>The <see cref="JsonPatchDocument{TModel}"/>.</returns> /// <returns></returns>
public JsonPatchDocument<TModel> Replace<TProp>( public JsonPatchDocument<TModel> Replace<TProp>([NotNull] Expression<Func<TModel, IList<TProp>>> path, TProp value)
[NotNull] Expression<Func<TModel, IList<TProp>>> path,
TProp value)
{ {
Operations.Add(new Operation<TModel>( Operations.Add(new Operation<TModel>(
"replace", "replace",
@ -211,9 +203,9 @@ namespace Microsoft.AspNet.JsonPatch
/// Removes value at specified location and add it to the target location. Will result in, for example: /// Removes value at specified location and add it to the target location. Will result in, for example:
/// { "op": "move", "from": "/a/b/c", "path": "/a/b/d" } /// { "op": "move", "from": "/a/b/c", "path": "/a/b/d" }
/// </summary> /// </summary>
/// <param name="from">The path of the property to remove.</param> /// <param name="from"></param>
/// <param name="path">The path of the property to add.</param> /// <param name="path"></param>
/// <returns>The <see cref="JsonPatchDocument{TModel}"/>.</returns> /// <returns></returns>
public JsonPatchDocument<TModel> Move<TProp>( public JsonPatchDocument<TModel> Move<TProp>(
[NotNull] Expression<Func<TModel, TProp>> from, [NotNull] Expression<Func<TModel, TProp>> from,
[NotNull] Expression<Func<TModel, TProp>> path) [NotNull] Expression<Func<TModel, TProp>> path)
@ -227,13 +219,13 @@ namespace Microsoft.AspNet.JsonPatch
} }
/// <summary> /// <summary>
/// Move from a position in a list to a new location. /// Move from a position in a list to a new location
/// </summary> /// </summary>
/// <typeparam name="TProp">The value type.</typeparam> /// <typeparam name="TProp"></typeparam>
/// <param name="from">The path of the property to remove.</param> /// <param name="from"></param>
/// <param name="positionFrom">The position in the list to remove.</param> /// <param name="positionFrom"></param>
/// <param name="path">The path of the property to add.</param> /// <param name="path"></param>
/// <returns>The <see cref="JsonPatchDocument{TModel}"/>.</returns> /// <returns></returns>
public JsonPatchDocument<TModel> Move<TProp>( public JsonPatchDocument<TModel> Move<TProp>(
[NotNull] Expression<Func<TModel, IList<TProp>>> from, [NotNull] Expression<Func<TModel, IList<TProp>>> from,
int positionFrom, int positionFrom,
@ -248,13 +240,13 @@ namespace Microsoft.AspNet.JsonPatch
} }
/// <summary> /// <summary>
/// Move from a property to a location in a list. /// Move from a property to a location in a list
/// </summary> /// </summary>
/// <typeparam name="TProp">The value type.</typeparam> /// <typeparam name="TProp"></typeparam>
/// <param name="from">The path of the property to remove.</param> /// <param name="from"></param>
/// <param name="path">The path of the property to add.</param> /// <param name="positionFrom"></param>
/// <param name="positionTo">The position in the list to add.</param> /// <param name="path"></param>
/// <returns>The <see cref="JsonPatchDocument{TModel}"/>.</returns> /// <returns></returns>
public JsonPatchDocument<TModel> Move<TProp>( public JsonPatchDocument<TModel> Move<TProp>(
[NotNull] Expression<Func<TModel, TProp>> from, [NotNull] Expression<Func<TModel, TProp>> from,
[NotNull] Expression<Func<TModel, IList<TProp>>> path, [NotNull] Expression<Func<TModel, IList<TProp>>> path,
@ -269,14 +261,13 @@ namespace Microsoft.AspNet.JsonPatch
} }
/// <summary> /// <summary>
/// Move from a position in a list to another location in a list. /// Move from a position in a list to another location in a list
/// </summary> /// </summary>
/// <typeparam name="TProp">The value type.</typeparam> /// <typeparam name="TProp"></typeparam>
/// <param name="from">The path of the property to remove.</param> /// <param name="from"></param>
/// <param name="positionFrom">The position in the list to remove.</param> /// <param name="positionFrom"></param>
/// <param name="path">The path of the property to add.</param> /// <param name="path"></param>
/// <param name="positionTo">The position in the list to add.</param> /// <returns></returns>
/// <returns>The <see cref="JsonPatchDocument{TModel}"/>.</returns>
public JsonPatchDocument<TModel> Move<TProp>( public JsonPatchDocument<TModel> Move<TProp>(
[NotNull] Expression<Func<TModel, IList<TProp>>> from, [NotNull] Expression<Func<TModel, IList<TProp>>> from,
int positionFrom, int positionFrom,
@ -292,13 +283,13 @@ namespace Microsoft.AspNet.JsonPatch
} }
/// <summary> /// <summary>
/// Move from a position in a list to the end of another list. /// Move from a position in a list to the end of another list
/// </summary> /// </summary>
/// <typeparam name="TProp">The value type.</typeparam> /// <typeparam name="TProp"></typeparam>
/// <param name="from">The path of the property to remove.</param> /// <param name="from"></param>
/// <param name="positionFrom">The position in the list to remove.</param> /// <param name="positionFrom"></param>
/// <param name="path">The path of the property to add.</param> /// <param name="path"></param>
/// <returns>The <see cref="JsonPatchDocument{TModel}"/>.</returns> /// <returns></returns>
public JsonPatchDocument<TModel> Move<TProp>( public JsonPatchDocument<TModel> Move<TProp>(
[NotNull] Expression<Func<TModel, IList<TProp>>> from, [NotNull] Expression<Func<TModel, IList<TProp>>> from,
int positionFrom, int positionFrom,
@ -313,15 +304,16 @@ namespace Microsoft.AspNet.JsonPatch
} }
/// <summary> /// <summary>
/// Move to the end of a list. /// Move to the end of a list
/// </summary> /// </summary>
/// <typeparam name="TProp">The value type.</typeparam> /// <typeparam name="TProp"></typeparam>
/// <param name="from">The path of the property to remove.</param> /// <param name="from"></param>
/// <param name="path">The path of the property to add.</param> /// <param name="positionFrom"></param>
/// <returns>The <see cref="JsonPatchDocument{TModel}"/>.</returns> /// <param name="path"></param>
/// <returns></returns>
public JsonPatchDocument<TModel> Move<TProp>( public JsonPatchDocument<TModel> Move<TProp>(
[NotNull] Expression<Func<TModel, TProp>> from, [NotNull] Expression<Func<TModel, TProp>> from,
[NotNull] Expression<Func<TModel, IList<TProp>>> path) [NotNull] Expression<Func<TModel, IList<TProp>>> path)
{ {
Operations.Add(new Operation<TModel>( Operations.Add(new Operation<TModel>(
"move", "move",
@ -332,15 +324,15 @@ namespace Microsoft.AspNet.JsonPatch
} }
/// <summary> /// <summary>
/// Copy the value at specified location to the target location. Will result in, for example: /// Copy the value at specified location to the target location. Willr esult in, for example:
/// { "op": "copy", "from": "/a/b/c", "path": "/a/b/e" } /// { "op": "copy", "from": "/a/b/c", "path": "/a/b/e" }
/// </summary> /// </summary>
/// <param name="from">The path of the property to copy from.</param> /// <param name="from"></param>
/// <param name="path">The path of the property to copy to.</param> /// <param name="path"></param>
/// <returns>The <see cref="JsonPatchDocument{TModel}"/>.</returns> /// <returns></returns>
public JsonPatchDocument<TModel> Copy<TProp>( public JsonPatchDocument<TModel> Copy<TProp>(
[NotNull] Expression<Func<TModel, TProp>> from, [NotNull] Expression<Func<TModel, TProp>> from,
[NotNull] Expression<Func<TModel, TProp>> path) [NotNull] Expression<Func<TModel, TProp>> path)
{ {
Operations.Add(new Operation<TModel>( Operations.Add(new Operation<TModel>(
"copy", "copy",
@ -351,17 +343,17 @@ namespace Microsoft.AspNet.JsonPatch
} }
/// <summary> /// <summary>
/// Copy from a position in a list to a new location. /// Copy from a position in a list to a new location
/// </summary> /// </summary>
/// <typeparam name="TProp">The value type.</typeparam> /// <typeparam name="TProp"></typeparam>
/// <param name="from">The path of the property to copy from.</param> /// <param name="from"></param>
/// <param name="positionFrom">The position in the list to copy from.</param> /// <param name="positionFrom"></param>
/// <param name="path">The path of the property to copy to.</param> /// <param name="path"></param>
/// <returns>The <see cref="JsonPatchDocument{TModel}"/>.</returns> /// <returns></returns>
public JsonPatchDocument<TModel> Copy<TProp>( public JsonPatchDocument<TModel> Copy<TProp>(
[NotNull] Expression<Func<TModel, IList<TProp>>> from, [NotNull] Expression<Func<TModel, IList<TProp>>> from,
int positionFrom, int positionFrom,
[NotNull] Expression<Func<TModel, TProp>> path) [NotNull] Expression<Func<TModel, TProp>> path)
{ {
Operations.Add(new Operation<TModel>( Operations.Add(new Operation<TModel>(
"copy", "copy",
@ -372,13 +364,13 @@ namespace Microsoft.AspNet.JsonPatch
} }
/// <summary> /// <summary>
/// Copy from a property to a location in a list. /// Copy from a property to a location in a list
/// </summary> /// </summary>
/// <typeparam name="TProp">The value type.</typeparam> /// <typeparam name="TProp"></typeparam>
/// <param name="from">The path of the property to copy from.</param> /// <param name="from"></param>
/// <param name="path">The path of the property to copy to.</param> /// <param name="positionFrom"></param>
/// <param name="positionTo">The position in the list to copy to.</param> /// <param name="path"></param>
/// <returns>The <see cref="JsonPatchDocument{TModel}"/>.</returns> /// <returns></returns>
public JsonPatchDocument<TModel> Copy<TProp>( public JsonPatchDocument<TModel> Copy<TProp>(
[NotNull] Expression<Func<TModel, TProp>> from, [NotNull] Expression<Func<TModel, TProp>> from,
[NotNull] Expression<Func<TModel, IList<TProp>>> path, [NotNull] Expression<Func<TModel, IList<TProp>>> path,
@ -393,14 +385,13 @@ namespace Microsoft.AspNet.JsonPatch
} }
/// <summary> /// <summary>
/// Copy from a position in a list to a new location in a list. /// Copy from a position in a list to a new location in a list
/// </summary> /// </summary>
/// <typeparam name="TProp">The value type.</typeparam> /// <typeparam name="TProp"></typeparam>
/// <param name="from">The path of the property to copy from.</param> /// <param name="from"></param>
/// <param name="positionFrom">The position in the list to copy from.</param> /// <param name="positionFrom"></param>
/// <param name="path">The path of the property to copy to.</param> /// <param name="path"></param>
/// <param name="positionTo">The position in the list to copy to.</param> /// <returns></returns>
/// <returns>The <see cref="JsonPatchDocument{TModel}"/>.</returns>
public JsonPatchDocument<TModel> Copy<TProp>( public JsonPatchDocument<TModel> Copy<TProp>(
[NotNull] Expression<Func<TModel, IList<TProp>>> from, [NotNull] Expression<Func<TModel, IList<TProp>>> from,
int positionFrom, int positionFrom,
@ -416,13 +407,13 @@ namespace Microsoft.AspNet.JsonPatch
} }
/// <summary> /// <summary>
/// Copy from a position in a list to the end of another list. /// Copy from a position in a list to the end of another list
/// </summary> /// </summary>
/// <typeparam name="TProp">The value type.</typeparam> /// <typeparam name="TProp"></typeparam>
/// <param name="from">The path of the property to copy from.</param> /// <param name="from"></param>
/// <param name="positionFrom">The position in the list to copy from.</param> /// <param name="positionFrom"></param>
/// <param name="path">The path of the property to copy to.</param> /// <param name="path"></param>
/// <returns>The <see cref="JsonPatchDocument{TModel}"/>.</returns> /// <returns></returns>
public JsonPatchDocument<TModel> Copy<TProp>( public JsonPatchDocument<TModel> Copy<TProp>(
[NotNull] Expression<Func<TModel, IList<TProp>>> from, [NotNull] Expression<Func<TModel, IList<TProp>>> from,
int positionFrom, int positionFrom,
@ -437,12 +428,13 @@ namespace Microsoft.AspNet.JsonPatch
} }
/// <summary> /// <summary>
/// Copy to the end of a list. /// Copy to the end of a list
/// </summary> /// </summary>
/// <typeparam name="TProp">The value type.</typeparam> /// <typeparam name="TProp"></typeparam>
/// <param name="from">The path of the property to copy from.</param> /// <param name="from"></param>
/// <param name="path">The path of the property to copy to.</param> /// <param name="positionFrom"></param>
/// <returns>The <see cref="JsonPatchDocument{TModel}"/>.</returns> /// <param name="path"></param>
/// <returns></returns>
public JsonPatchDocument<TModel> Copy<TProp>( public JsonPatchDocument<TModel> Copy<TProp>(
[NotNull] Expression<Func<TModel, TProp>> from, [NotNull] Expression<Func<TModel, TProp>> from,
[NotNull] Expression<Func<TModel, IList<TProp>>> path) [NotNull] Expression<Func<TModel, IList<TProp>>> path)
@ -455,17 +447,17 @@ namespace Microsoft.AspNet.JsonPatch
return this; return this;
} }
public void ApplyTo([NotNull] TModel objectToApplyTo) public void ApplyTo(TModel objectToApplyTo)
{ {
ApplyTo(objectToApplyTo, new ObjectAdapter<TModel>(ContractResolver, logErrorAction: null)); ApplyTo(objectToApplyTo, new ObjectAdapter(ContractResolver, logErrorAction: null));
} }
public void ApplyTo([NotNull] TModel objectToApplyTo, Action<JsonPatchError<TModel>> logErrorAction) public void ApplyTo(TModel objectToApplyTo, Action<JsonPatchError> logErrorAction)
{ {
ApplyTo(objectToApplyTo, new ObjectAdapter<TModel>(ContractResolver, logErrorAction)); ApplyTo(objectToApplyTo, new ObjectAdapter(ContractResolver, logErrorAction));
} }
public void ApplyTo([NotNull] TModel objectToApplyTo, [NotNull] IObjectAdapter<TModel> adapter) public void ApplyTo(TModel objectToApplyTo, IObjectAdapter adapter)
{ {
// apply each operation in order // apply each operation in order
foreach (var op in Operations) foreach (var op in Operations)

View File

@ -9,17 +9,17 @@ namespace Microsoft.AspNet.JsonPatch
/// <summary> /// <summary>
/// Captures error message and the related entity and the operation that caused it. /// Captures error message and the related entity and the operation that caused it.
/// </summary> /// </summary>
public class JsonPatchError<TModel> where TModel : class public class JsonPatchError
{ {
/// <summary> /// <summary>
/// Initializes a new instance of <see cref="JsonPatchError{TModel}"/>. /// Initializes a new instance of <see cref="JsonPatchError"/>.
/// </summary> /// </summary>
/// <param name="affectedObject">The object that is affected by the error.</param> /// <param name="affectedObject">The object that is affected by the error.</param>
/// <param name="operation">The <see cref="Operation{TModel}"/> that caused the error.</param> /// <param name="operation">The <see cref="Operation"/> that caused the error.</param>
/// <param name="errorMessage">The error message.</param> /// <param name="errorMessage">The error message.</param>
public JsonPatchError( public JsonPatchError(
[NotNull] TModel affectedObject, object affectedObject,
[NotNull] Operation<TModel> operation, Operation operation,
[NotNull] string errorMessage) [NotNull] string errorMessage)
{ {
AffectedObject = affectedObject; AffectedObject = affectedObject;
@ -30,12 +30,12 @@ namespace Microsoft.AspNet.JsonPatch
/// <summary> /// <summary>
/// Gets the object that is affected by the error. /// Gets the object that is affected by the error.
/// </summary> /// </summary>
public TModel AffectedObject { get; } public object AffectedObject { get; }
/// <summary> /// <summary>
/// Gets the <see cref="Operation{TModel}"/> that caused the error. /// Gets the <see cref="Operation"/> that caused the error.
/// </summary> /// </summary>
public Operation<TModel> Operation { get; } public Operation Operation { get; }
/// <summary> /// <summary>
/// Gets the error message. /// Gets the error message.

View File

@ -26,7 +26,7 @@ namespace Microsoft.AspNet.JsonPatch.Operations
} }
public void Apply([NotNull] TModel objectToApplyTo, [NotNull] IObjectAdapter<TModel> adapter) public void Apply([NotNull] TModel objectToApplyTo, [NotNull] IObjectAdapter adapter)
{ {
switch (OperationType) switch (OperationType)
{ {

View File

@ -385,7 +385,7 @@ namespace Microsoft.AspNet.JsonPatch.Test
patchDoc.Add<int>(o => o.SimpleDTO.IntegerList, 4, 4); patchDoc.Add<int>(o => o.SimpleDTO.IntegerList, 4, 4);
// Act & Assert // Act & Assert
var exception = Assert.Throws<JsonPatchException<SimpleDTOWithNestedDTO>>(() => { patchDoc.ApplyTo(doc); }); var exception = Assert.Throws<JsonPatchException>(() => { patchDoc.ApplyTo(doc); });
Assert.Equal( Assert.Equal(
"For operation 'add' on array property at path '/simpledto/integerlist/4', the index is " + "For operation 'add' on array property at path '/simpledto/integerlist/4', the index is " +
"larger than the array size.", "larger than the array size.",
@ -413,7 +413,7 @@ namespace Microsoft.AspNet.JsonPatch.Test
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTOWithNestedDTO>>(serialized); var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTOWithNestedDTO>>(serialized);
// Act & Assert // Act & Assert
var exception = Assert.Throws<JsonPatchException<SimpleDTOWithNestedDTO>>(() => var exception = Assert.Throws<JsonPatchException>(() =>
{ {
deserialized.ApplyTo(doc); deserialized.ApplyTo(doc);
}); });
@ -441,9 +441,9 @@ namespace Microsoft.AspNet.JsonPatch.Test
var logger = new TestErrorLogger<SimpleDTOWithNestedDTO>(); var logger = new TestErrorLogger<SimpleDTOWithNestedDTO>();
// Act
patchDoc.ApplyTo(doc, logger.LogErrorMessage); patchDoc.ApplyTo(doc, logger.LogErrorMessage);
//Assert //Assert
Assert.Equal( Assert.Equal(
"For operation 'add' on array property at path '/simpledto/integerlist/4', the index is larger than " + "For operation 'add' on array property at path '/simpledto/integerlist/4', the index is larger than " +
@ -469,7 +469,7 @@ namespace Microsoft.AspNet.JsonPatch.Test
patchDoc.Add<int>(o => o.SimpleDTO.IntegerList, 4, -1); patchDoc.Add<int>(o => o.SimpleDTO.IntegerList, 4, -1);
// Act & Assert // Act & Assert
var exception = Assert.Throws<JsonPatchException<SimpleDTOWithNestedDTO>>(() => { patchDoc.ApplyTo(doc); }); var exception = Assert.Throws<JsonPatchException>(() => { patchDoc.ApplyTo(doc); });
Assert.Equal("Property does not exist at path '/simpledto/integerlist/-1'.", exception.Message); Assert.Equal("Property does not exist at path '/simpledto/integerlist/-1'.", exception.Message);
} }
@ -493,7 +493,7 @@ namespace Microsoft.AspNet.JsonPatch.Test
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTOWithNestedDTO>>(serialized); var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTOWithNestedDTO>>(serialized);
// Act & Assert // Act & Assert
var exception = Assert.Throws<JsonPatchException<SimpleDTOWithNestedDTO>>(() => var exception = Assert.Throws<JsonPatchException>(() =>
{ {
deserialized.ApplyTo(doc); deserialized.ApplyTo(doc);
}); });
@ -518,9 +518,10 @@ namespace Microsoft.AspNet.JsonPatch.Test
var logger = new TestErrorLogger<SimpleDTOWithNestedDTO>(); var logger = new TestErrorLogger<SimpleDTOWithNestedDTO>();
// Act
patchDoc.ApplyTo(doc, logger.LogErrorMessage); patchDoc.ApplyTo(doc, logger.LogErrorMessage);
//Assert //Assert
Assert.Equal("Property does not exist at path '/simpledto/integerlist/-1'.", logger.ErrorMessage); Assert.Equal("Property does not exist at path '/simpledto/integerlist/-1'.", logger.ErrorMessage);
} }
@ -689,7 +690,7 @@ namespace Microsoft.AspNet.JsonPatch.Test
patchDoc.Remove<int>(o => o.SimpleDTO.IntegerList, 3); patchDoc.Remove<int>(o => o.SimpleDTO.IntegerList, 3);
// Act & Assert // Act & Assert
var exception = Assert.Throws<JsonPatchException<SimpleDTOWithNestedDTO>>(() => { patchDoc.ApplyTo(doc); }); var exception = Assert.Throws<JsonPatchException>(() => { patchDoc.ApplyTo(doc); });
Assert.Equal( Assert.Equal(
"For operation 'remove' on array property at path '/simpledto/integerlist/3', the index is " + "For operation 'remove' on array property at path '/simpledto/integerlist/3', the index is " +
"larger than the array size.", "larger than the array size.",
@ -716,7 +717,7 @@ namespace Microsoft.AspNet.JsonPatch.Test
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTOWithNestedDTO>>(serialized); var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTOWithNestedDTO>>(serialized);
// Act & Assert // Act & Assert
var exception = Assert.Throws<JsonPatchException<SimpleDTOWithNestedDTO>>(() => var exception = Assert.Throws<JsonPatchException>(() =>
{ {
deserialized.ApplyTo(doc); deserialized.ApplyTo(doc);
}); });
@ -744,7 +745,6 @@ namespace Microsoft.AspNet.JsonPatch.Test
var logger = new TestErrorLogger<SimpleDTOWithNestedDTO>(); var logger = new TestErrorLogger<SimpleDTOWithNestedDTO>();
// Act
patchDoc.ApplyTo(doc, logger.LogErrorMessage); patchDoc.ApplyTo(doc, logger.LogErrorMessage);
// Assert // Assert
@ -771,7 +771,7 @@ namespace Microsoft.AspNet.JsonPatch.Test
patchDoc.Remove<int>(o => o.SimpleDTO.IntegerList, -1); patchDoc.Remove<int>(o => o.SimpleDTO.IntegerList, -1);
// Act & Assert // Act & Assert
var exception = Assert.Throws<JsonPatchException<SimpleDTOWithNestedDTO>>(() => { patchDoc.ApplyTo(doc); }); var exception = Assert.Throws<JsonPatchException>(() => { patchDoc.ApplyTo(doc); });
Assert.Equal("Property does not exist at path '/simpledto/integerlist/-1'.", exception.Message); Assert.Equal("Property does not exist at path '/simpledto/integerlist/-1'.", exception.Message);
} }
@ -795,7 +795,7 @@ namespace Microsoft.AspNet.JsonPatch.Test
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTOWithNestedDTO>>(serialized); var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTOWithNestedDTO>>(serialized);
// Act & Assert // Act & Assert
var exception = Assert.Throws<JsonPatchException<SimpleDTOWithNestedDTO>>(() => var exception = Assert.Throws<JsonPatchException>(() =>
{ {
deserialized.ApplyTo(doc); deserialized.ApplyTo(doc);
}); });
@ -820,7 +820,7 @@ namespace Microsoft.AspNet.JsonPatch.Test
var logger = new TestErrorLogger<SimpleDTOWithNestedDTO>(); var logger = new TestErrorLogger<SimpleDTOWithNestedDTO>();
// Act
patchDoc.ApplyTo(doc, logger.LogErrorMessage); patchDoc.ApplyTo(doc, logger.LogErrorMessage);
// Assert // Assert
@ -1232,7 +1232,7 @@ namespace Microsoft.AspNet.JsonPatch.Test
patchDoc.Replace<int>(o => o.SimpleDTO.IntegerList, 5, 3); patchDoc.Replace<int>(o => o.SimpleDTO.IntegerList, 5, 3);
// Act & Assert // Act & Assert
var exception = Assert.Throws<JsonPatchException<SimpleDTOWithNestedDTO>>(() => { patchDoc.ApplyTo(doc); }); var exception = Assert.Throws<JsonPatchException>(() => { patchDoc.ApplyTo(doc); });
Assert.Equal( Assert.Equal(
"For operation 'replace' on array property at path '/simpledto/integerlist/3', the index is " + "For operation 'replace' on array property at path '/simpledto/integerlist/3', the index is " +
"larger than the array size.", "larger than the array size.",
@ -1259,7 +1259,7 @@ namespace Microsoft.AspNet.JsonPatch.Test
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTOWithNestedDTO>>(serialized); var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTOWithNestedDTO>>(serialized);
// Act & Assert // Act & Assert
var exception = Assert.Throws<JsonPatchException<SimpleDTOWithNestedDTO>>(() => var exception = Assert.Throws<JsonPatchException>(() =>
{ {
deserialized.ApplyTo(doc); deserialized.ApplyTo(doc);
}); });
@ -1287,9 +1287,10 @@ namespace Microsoft.AspNet.JsonPatch.Test
var logger = new TestErrorLogger<SimpleDTOWithNestedDTO>(); var logger = new TestErrorLogger<SimpleDTOWithNestedDTO>();
// Act
patchDoc.ApplyTo(doc, logger.LogErrorMessage); patchDoc.ApplyTo(doc, logger.LogErrorMessage);
// Assert // Assert
Assert.Equal( Assert.Equal(
"For operation 'replace' on array property at path '/simpledto/integerlist/3', the index is " + "For operation 'replace' on array property at path '/simpledto/integerlist/3', the index is " +
@ -1314,7 +1315,7 @@ namespace Microsoft.AspNet.JsonPatch.Test
patchDoc.Replace<int>(o => o.SimpleDTO.IntegerList, 5, -1); patchDoc.Replace<int>(o => o.SimpleDTO.IntegerList, 5, -1);
// Act & Assert // Act & Assert
var exception = Assert.Throws<JsonPatchException<SimpleDTOWithNestedDTO>>(() => { patchDoc.ApplyTo(doc); }); var exception = Assert.Throws<JsonPatchException>(() => { patchDoc.ApplyTo(doc); });
Assert.Equal("Property does not exist at path '/simpledto/integerlist/-1'.", exception.Message); Assert.Equal("Property does not exist at path '/simpledto/integerlist/-1'.", exception.Message);
} }
@ -1338,7 +1339,7 @@ namespace Microsoft.AspNet.JsonPatch.Test
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTOWithNestedDTO>>(serialized); var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTOWithNestedDTO>>(serialized);
// Act & Assert // Act & Assert
var exception = Assert.Throws<JsonPatchException<SimpleDTOWithNestedDTO>>(() => { deserialized.ApplyTo(doc); }); var exception = Assert.Throws<JsonPatchException>(() => { deserialized.ApplyTo(doc); });
Assert.Equal("Property does not exist at path '/simpledto/integerlist/-1'.", exception.Message); Assert.Equal("Property does not exist at path '/simpledto/integerlist/-1'.", exception.Message);
} }
@ -1360,8 +1361,9 @@ namespace Microsoft.AspNet.JsonPatch.Test
var logger = new TestErrorLogger<SimpleDTOWithNestedDTO>(); var logger = new TestErrorLogger<SimpleDTOWithNestedDTO>();
// Act
patchDoc.ApplyTo(doc, logger.LogErrorMessage); patchDoc.ApplyTo(doc, logger.LogErrorMessage);
// Assert // Assert
Assert.Equal("Property does not exist at path '/simpledto/integerlist/-1'.", logger.ErrorMessage); Assert.Equal("Property does not exist at path '/simpledto/integerlist/-1'.", logger.ErrorMessage);

View File

@ -155,7 +155,7 @@ namespace Microsoft.AspNet.JsonPatch.Test
patchDoc.Add<int>(o => o.IntegerList, 4, 4); patchDoc.Add<int>(o => o.IntegerList, 4, 4);
// Act & Assert // Act & Assert
var exception = Assert.Throws<JsonPatchException<SimpleDTO>>(() => { patchDoc.ApplyTo(doc); }); var exception = Assert.Throws<JsonPatchException>(() => { patchDoc.ApplyTo(doc); });
Assert.Equal( Assert.Equal(
"For operation 'add' on array property at path '/integerlist/4', the index is " + "For operation 'add' on array property at path '/integerlist/4', the index is " +
"larger than the array size.", "larger than the array size.",
@ -179,7 +179,7 @@ namespace Microsoft.AspNet.JsonPatch.Test
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTO>>(serialized); var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTO>>(serialized);
// Act & Assert // Act & Assert
var exception = Assert.Throws<JsonPatchException<SimpleDTO>>(() => { deserialized.ApplyTo(doc); }); var exception = Assert.Throws<JsonPatchException>(() => { deserialized.ApplyTo(doc); });
Assert.Equal( Assert.Equal(
"For operation 'add' on array property at path '/integerlist/4', the index is " + "For operation 'add' on array property at path '/integerlist/4', the index is " +
"larger than the array size.", "larger than the array size.",
@ -201,9 +201,9 @@ namespace Microsoft.AspNet.JsonPatch.Test
var logger = new TestErrorLogger<SimpleDTO>(); var logger = new TestErrorLogger<SimpleDTO>();
// Act
patchDoc.ApplyTo(doc, logger.LogErrorMessage); patchDoc.ApplyTo(doc, logger.LogErrorMessage);
// Assert // Assert
Assert.Equal( Assert.Equal(
"For operation 'add' on array property at path '/integerlist/4', the index is " + "For operation 'add' on array property at path '/integerlist/4', the index is " +
@ -311,7 +311,7 @@ namespace Microsoft.AspNet.JsonPatch.Test
patchDoc.Add<int>(o => o.IntegerList, 4, -1); patchDoc.Add<int>(o => o.IntegerList, 4, -1);
// Act & Assert // Act & Assert
var exception = Assert.Throws<JsonPatchException<SimpleDTO>>(() => { patchDoc.ApplyTo(doc); }); var exception = Assert.Throws<JsonPatchException>(() => { patchDoc.ApplyTo(doc); });
Assert.Equal("Property does not exist at path '/integerlist/-1'.", exception.Message); Assert.Equal("Property does not exist at path '/integerlist/-1'.", exception.Message);
} }
@ -332,7 +332,7 @@ namespace Microsoft.AspNet.JsonPatch.Test
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTO>>(serialized); var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTO>>(serialized);
// Act & Assert // Act & Assert
var exception = Assert.Throws<JsonPatchException<SimpleDTO>>(() => { deserialized.ApplyTo(doc); }); var exception = Assert.Throws<JsonPatchException>(() => { deserialized.ApplyTo(doc); });
Assert.Equal("Property does not exist at path '/integerlist/-1'.", exception.Message); Assert.Equal("Property does not exist at path '/integerlist/-1'.", exception.Message);
} }
@ -351,7 +351,6 @@ namespace Microsoft.AspNet.JsonPatch.Test
var logger = new TestErrorLogger<SimpleDTO>(); var logger = new TestErrorLogger<SimpleDTO>();
// Act
patchDoc.ApplyTo(doc, logger.LogErrorMessage); patchDoc.ApplyTo(doc, logger.LogErrorMessage);
// Assert // Assert
@ -501,7 +500,7 @@ namespace Microsoft.AspNet.JsonPatch.Test
patchDoc.Remove<int>(o => o.IntegerList, 3); patchDoc.Remove<int>(o => o.IntegerList, 3);
// Act & Assert // Act & Assert
var exception = Assert.Throws<JsonPatchException<SimpleDTO>>(() => { patchDoc.ApplyTo(doc); }); var exception = Assert.Throws<JsonPatchException>(() => { patchDoc.ApplyTo(doc); });
Assert.Equal( Assert.Equal(
"For operation 'remove' on array property at path '/integerlist/3', the index is " + "For operation 'remove' on array property at path '/integerlist/3', the index is " +
"larger than the array size.", "larger than the array size.",
@ -525,7 +524,7 @@ namespace Microsoft.AspNet.JsonPatch.Test
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTO>>(serialized); var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTO>>(serialized);
// Act & Assert // Act & Assert
var exception = Assert.Throws<JsonPatchException<SimpleDTO>>(() => { deserialized.ApplyTo(doc); }); var exception = Assert.Throws<JsonPatchException>(() => { deserialized.ApplyTo(doc); });
Assert.Equal( Assert.Equal(
"For operation 'remove' on array property at path '/integerlist/3', the index is " + "For operation 'remove' on array property at path '/integerlist/3', the index is " +
"larger than the array size.", "larger than the array size.",
@ -547,9 +546,9 @@ namespace Microsoft.AspNet.JsonPatch.Test
var logger = new TestErrorLogger<SimpleDTO>(); var logger = new TestErrorLogger<SimpleDTO>();
// Act
patchDoc.ApplyTo(doc, logger.LogErrorMessage); patchDoc.ApplyTo(doc, logger.LogErrorMessage);
// Assert // Assert
Assert.Equal( Assert.Equal(
"For operation 'remove' on array property at path '/integerlist/3', the index is " + "For operation 'remove' on array property at path '/integerlist/3', the index is " +
@ -571,7 +570,7 @@ namespace Microsoft.AspNet.JsonPatch.Test
patchDoc.Remove<int>(o => o.IntegerList, -1); patchDoc.Remove<int>(o => o.IntegerList, -1);
// Act & Assert // Act & Assert
var exception = Assert.Throws<JsonPatchException<SimpleDTO>>(() => { patchDoc.ApplyTo(doc); }); var exception = Assert.Throws<JsonPatchException>(() => { patchDoc.ApplyTo(doc); });
Assert.Equal("Property does not exist at path '/integerlist/-1'.", exception.Message); Assert.Equal("Property does not exist at path '/integerlist/-1'.", exception.Message);
} }
@ -592,7 +591,7 @@ namespace Microsoft.AspNet.JsonPatch.Test
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTO>>(serialized); var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTO>>(serialized);
// Act & Assert // Act & Assert
var exception = Assert.Throws<JsonPatchException<SimpleDTO>>(() => { deserialized.ApplyTo(doc); }); var exception = Assert.Throws<JsonPatchException>(() => { deserialized.ApplyTo(doc); });
Assert.Equal("Property does not exist at path '/integerlist/-1'.", exception.Message); Assert.Equal("Property does not exist at path '/integerlist/-1'.", exception.Message);
} }
@ -611,9 +610,10 @@ namespace Microsoft.AspNet.JsonPatch.Test
var logger = new TestErrorLogger<SimpleDTO>(); var logger = new TestErrorLogger<SimpleDTO>();
// Act
patchDoc.ApplyTo(doc, logger.LogErrorMessage); patchDoc.ApplyTo(doc, logger.LogErrorMessage);
// Assert // Assert
Assert.Equal("Property does not exist at path '/integerlist/-1'.", logger.ErrorMessage); Assert.Equal("Property does not exist at path '/integerlist/-1'.", logger.ErrorMessage);
} }
@ -1112,7 +1112,7 @@ namespace Microsoft.AspNet.JsonPatch.Test
patchDoc.Replace<int>(o => o.IntegerList, 5, 3); patchDoc.Replace<int>(o => o.IntegerList, 5, 3);
// Act & Assert // Act & Assert
var exception = Assert.Throws<JsonPatchException<SimpleDTO>>(() => var exception = Assert.Throws<JsonPatchException>(() =>
{ {
patchDoc.ApplyTo(doc); patchDoc.ApplyTo(doc);
}); });
@ -1138,7 +1138,7 @@ namespace Microsoft.AspNet.JsonPatch.Test
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTO>>(serialized); var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTO>>(serialized);
// Act & Assert // Act & Assert
var exception = Assert.Throws<JsonPatchException<SimpleDTO>>(() => var exception = Assert.Throws<JsonPatchException>(() =>
{ {
deserialized.ApplyTo(doc); deserialized.ApplyTo(doc);
}); });
@ -1162,7 +1162,7 @@ namespace Microsoft.AspNet.JsonPatch.Test
patchDoc.Replace<int>(o => o.IntegerList, 5, -1); patchDoc.Replace<int>(o => o.IntegerList, 5, -1);
// Act & Assert // Act & Assert
var exception = Assert.Throws<JsonPatchException<SimpleDTO>>(() => var exception = Assert.Throws<JsonPatchException>(() =>
{ {
patchDoc.ApplyTo(doc); patchDoc.ApplyTo(doc);
}); });
@ -1186,7 +1186,7 @@ namespace Microsoft.AspNet.JsonPatch.Test
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTO>>(serialized); var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTO>>(serialized);
// Act & Assert // Act & Assert
var exception = Assert.Throws<JsonPatchException<SimpleDTO>>(() => var exception = Assert.Throws<JsonPatchException>(() =>
{ {
deserialized.ApplyTo(doc); deserialized.ApplyTo(doc);
}); });

View File

@ -7,7 +7,7 @@ namespace Microsoft.AspNet.JsonPatch.Test
{ {
public string ErrorMessage { get; set; } public string ErrorMessage { get; set; }
public void LogErrorMessage(JsonPatchError<T> patchError) public void LogErrorMessage(JsonPatchError patchError)
{ {
ErrorMessage = patchError.ErrorMessage; ErrorMessage = patchError.ErrorMessage;
} }