[JsonPatch]: Added NotNull attribute to parameters and updated comments

This commit is contained in:
Kirthi Krishnamraju 2015-07-01 16:12:40 -07:00
parent c2952f26fa
commit 38748b54eb
7 changed files with 194 additions and 176 deletions

View File

@ -22,7 +22,9 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
/// </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{TModel}"/>.</param>
public ObjectAdapter(IContractResolver contractResolver, Action<JsonPatchError<TModel>> logErrorAction) public ObjectAdapter(
[NotNull] IContractResolver contractResolver,
Action<JsonPatchError<TModel>> logErrorAction)
{ {
ContractResolver = contractResolver; ContractResolver = contractResolver;
LogErrorAction = logErrorAction; LogErrorAction = logErrorAction;
@ -97,8 +99,8 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
/// because "a" does not exist. /// because "a" does not exist.
/// </summary> /// </summary>
/// <param name="operation">The add operation.</param> /// <param name="operation">The add operation.</param>
/// <param name="objectApplyTo">Object to apply the operation to.</param> /// <param name="objectToApplyTo">Object to apply the operation to.</param>
public void Add(Operation<TModel> operation, TModel objectToApplyTo) public void Add([NotNull] Operation<TModel> operation, [NotNull] TModel objectToApplyTo)
{ {
Add(operation.path, operation.value, objectToApplyTo, operation); Add(operation.path, operation.value, objectToApplyTo, operation);
} }
@ -107,7 +109,11 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
/// Add is used by various operations (eg: add, copy, ...), yet through different operations; /// Add is used by various operations (eg: add, copy, ...), yet through different operations;
/// This method allows code reuse yet reporting the correct operation on error /// This method allows code reuse yet reporting the correct operation on error
/// </summary> /// </summary>
private void Add(string path, object value, TModel objectToApplyTo, Operation<TModel> operationToReport) private void Add(
[NotNull] string path,
object value,
[NotNull] TModel objectToApplyTo,
[NotNull] Operation<TModel> 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,
@ -237,8 +243,8 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
/// location; i.e., a location cannot be moved into one of its children. /// location; i.e., a location cannot be moved into one of its children.
/// </summary> /// </summary>
/// <param name="operation">The move operation.</param> /// <param name="operation">The move operation.</param>
/// <param name="objectApplyTo">Object to apply the operation to.</param> /// <param name="objectToApplyTo">Object to apply the operation to.</param>
public void Move(Operation<TModel> operation, TModel objectToApplyTo) public void Move([NotNull] Operation<TModel> operation, [NotNull] TModel objectToApplyTo)
{ {
// get value at from location // get value at from location
object valueAtFromLocation = null; object valueAtFromLocation = null;
@ -322,8 +328,8 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
/// specified index are shifted one position to the left. /// specified index are shifted one position to the left.
/// </summary> /// </summary>
/// <param name="operation">The remove operation.</param> /// <param name="operation">The remove operation.</param>
/// <param name="objectApplyTo">Object to apply the operation to.</param> /// <param name="objectToApplyTo">Object to apply the operation to.</param>
public void Remove(Operation<TModel> operation, TModel objectToApplyTo) public void Remove([NotNull] Operation<TModel> operation, [NotNull] TModel objectToApplyTo)
{ {
Remove(operation.path, objectToApplyTo, operation); Remove(operation.path, objectToApplyTo, operation);
} }
@ -332,7 +338,10 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
/// Remove is used by various operations (eg: remove, move, ...), yet through different operations; /// Remove is used by various operations (eg: remove, move, ...), yet through different operations;
/// This method allows code reuse yet reporting the correct operation on error /// This method allows code reuse yet reporting the correct operation on error
/// </summary> /// </summary>
private void Remove(string path, TModel objectToApplyTo, Operation<TModel> operationToReport) private void Remove(
[NotNull] string path,
[NotNull] TModel objectToApplyTo,
[NotNull] Operation<TModel> operationToReport)
{ {
var removeFromList = false; var removeFromList = false;
var positionAsInteger = -1; var positionAsInteger = -1;
@ -442,8 +451,8 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
/// for performance reasons (multiple checks of same requirements). /// for performance reasons (multiple checks of same requirements).
/// </summary> /// </summary>
/// <param name="operation">The replace operation.</param> /// <param name="operation">The replace operation.</param>
/// <param name="objectApplyTo">Object to apply the operation to.</param> /// <param name="objectToApplyTo">Object to apply the operation to.</param>
public void Replace(Operation<TModel> operation, TModel objectToApplyTo) public void Replace([NotNull] Operation<TModel> operation, [NotNull] TModel 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);
@ -470,8 +479,8 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
/// the value specified in from for performance reasons (multiple checks of same requirements). /// the value specified in from for performance reasons (multiple checks of same requirements).
/// </summary> /// </summary>
/// <param name="operation">The copy operation.</param> /// <param name="operation">The copy operation.</param>
/// <param name="objectApplyTo">Object to apply the operation to.</param> /// <param name="objectToApplyTo">Object to apply the operation to.</param>
public void Copy(Operation<TModel> operation, TModel objectToApplyTo) public void Copy([NotNull] Operation<TModel> operation, [NotNull] TModel objectToApplyTo)
{ {
// get value at from location // get value at from location
object valueAtFromLocation = null; object valueAtFromLocation = null;
@ -610,9 +619,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
} }
} }
private JsonPatchProperty FindPropertyAndParent( private JsonPatchProperty FindPropertyAndParent(object targetObject, string propertyPath)
object targetObject,
string propertyPath)
{ {
try try
{ {
@ -674,7 +681,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
} }
} }
private Type GetIListType([NotNull] Type type) private Type GetIListType(Type type)
{ {
if (IsGenericListType(type)) if (IsGenericListType(type))
{ {
@ -692,7 +699,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
return null; return null;
} }
private bool IsGenericListType([NotNull] Type type) private bool IsGenericListType(Type type)
{ {
if (type.GetTypeInfo().IsGenericType && if (type.GetTypeInfo().IsGenericType &&
type.GetGenericTypeDefinition() == typeof(IList<>)) type.GetGenericTypeDefinition() == typeof(IList<>))

View File

@ -3,6 +3,7 @@
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
{ {
@ -27,14 +28,14 @@ namespace Microsoft.AspNet.JsonPatch.Exceptions
} }
public JsonPatchException(JsonPatchError<TModel> jsonPatchError) public JsonPatchException([NotNull] JsonPatchError<TModel> jsonPatchError)
{ {
FailedOperation = jsonPatchError.Operation; FailedOperation = jsonPatchError.Operation;
_message = jsonPatchError.ErrorMessage; _message = jsonPatchError.ErrorMessage;
AffectedObject = jsonPatchError.AffectedObject; AffectedObject = jsonPatchError.AffectedObject;
} }
public JsonPatchException(JsonPatchError<TModel> jsonPatchError, Exception innerException) public JsonPatchException([NotNull] JsonPatchError<TModel> jsonPatchError, Exception innerException)
: this(jsonPatchError) : this(jsonPatchError)
{ {
InnerException = innerException; InnerException = innerException;

View File

@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved. // 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. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.Framework.Internal;
using Newtonsoft.Json.Serialization; using Newtonsoft.Json.Serialization;
namespace Microsoft.AspNet.JsonPatch namespace Microsoft.AspNet.JsonPatch
@ -13,7 +14,7 @@ namespace Microsoft.AspNet.JsonPatch
/// <summary> /// <summary>
/// Initializes a new instance. /// Initializes a new instance.
/// </summary> /// </summary>
public JsonPatchProperty(JsonProperty property, object parent) public JsonPatchProperty([NotNull] JsonProperty property, [NotNull] object parent)
{ {
Property = property; Property = property;
Parent = parent; Parent = parent;

View File

@ -8,6 +8,7 @@ using Microsoft.AspNet.JsonPatch.Adapters;
using Microsoft.AspNet.JsonPatch.Converters; using Microsoft.AspNet.JsonPatch.Converters;
using Microsoft.AspNet.JsonPatch.Helpers; using Microsoft.AspNet.JsonPatch.Helpers;
using Microsoft.AspNet.JsonPatch.Operations; using Microsoft.AspNet.JsonPatch.Operations;
using Microsoft.Framework.Internal;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Serialization; using Newtonsoft.Json.Serialization;
@ -31,8 +32,10 @@ namespace Microsoft.AspNet.JsonPatch
ContractResolver = new DefaultContractResolver(); ContractResolver = new DefaultContractResolver();
} }
// Create from list of operations // Create from list of operations.
public JsonPatchDocument(List<Operation<TModel>> operations, IContractResolver contractResolver) public JsonPatchDocument(
[NotNull] List<Operation<TModel>> operations,
[NotNull] IContractResolver contractResolver)
{ {
Operations = operations; Operations = operations;
ContractResolver = contractResolver; ContractResolver = contractResolver;
@ -42,11 +45,11 @@ 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">value type</typeparam> /// <typeparam name="TProp">The value type.</typeparam>
/// <param name="path">path</param> /// <param name="path">The path of the property to add.</param>
/// <param name="value">value</param> /// <param name="value">The value to add.</param>
/// <returns></returns> /// <returns>The <see cref="JsonPatchDocument{TModel}"/>.</returns>
public JsonPatchDocument<TModel> Add<TProp>(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>(
"add", "add",
@ -58,16 +61,15 @@ 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">value type</typeparam> /// <typeparam name="TProp">The value type.</typeparam>
/// <param name="path">path</param> /// <param name="path">The path of the property to add.</param>
/// <param name="value">value</param> /// <param name="value">The value to add.</param>
/// <param name="position">position</param> /// <param name="position">position</param>
/// <returns></returns> /// <returns>The <see cref="JsonPatchDocument{TModel}"/>.</returns>
public JsonPatchDocument<TModel> Add<TProp>( public JsonPatchDocument<TModel> Add<TProp>(
Expression<Func<TModel, [NotNull] Expression<Func<TModel, IList<TProp>>> path,
IList<TProp>>> path,
TProp value, TProp value,
int position) int position)
{ {
@ -81,13 +83,13 @@ namespace Microsoft.AspNet.JsonPatch
} }
/// <summary> /// <summary>
/// At value at end of list /// At value at end of list.
/// </summary> /// </summary>
/// <typeparam name="TProp">value type</typeparam> /// <typeparam name="TProp">The value type.</typeparam>
/// <param name="path">path</param> /// <param name="path">The path of the property to add.</param>
/// <param name="value">value</param> /// <param name="value">The value to add.</param>
/// <returns></returns> /// <returns>The <see cref="JsonPatchDocument{TModel}"/>.</returns>
public JsonPatchDocument<TModel> Add<TProp>(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>(
"add", "add",
@ -102,24 +104,26 @@ 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="remove"></param> /// <param name="path">The path of the property to remove.</param>
/// <param name="path"></param> /// <returns>The <see cref="JsonPatchDocument{TModel}"/>.</returns>
/// <returns></returns> public JsonPatchDocument<TModel> Remove<TProp>([NotNull] Expression<Func<TModel, TProp>> path)
public JsonPatchDocument<TModel> Remove<TProp>(Expression<Func<TModel, TProp>> path)
{ {
Operations.Add(new Operation<TModel>("remove", ExpressionHelpers.GetPath(path).ToLowerInvariant(), from: null)); Operations.Add(
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">value type</typeparam> /// <typeparam name="TProp">The value type.</typeparam>
/// <param name="path">target location</param> /// <param name="path">The path of the property to remove.</param>
/// <param name="position">position</param> /// <param name="position">The position in the list.</param>
/// <returns></returns> /// <returns>The <see cref="JsonPatchDocument{TModel}"/>.</returns>
public JsonPatchDocument<TModel> Remove<TProp>(Expression<Func<TModel, IList<TProp>>> path, int position) public JsonPatchDocument<TModel> Remove<TProp>(
[NotNull] Expression<Func<TModel, IList<TProp>>> path,
int position)
{ {
Operations.Add(new Operation<TModel>( Operations.Add(new Operation<TModel>(
"remove", "remove",
@ -130,12 +134,12 @@ namespace Microsoft.AspNet.JsonPatch
} }
/// <summary> /// <summary>
/// Remove value from end of list /// Remove value from end of list.
/// </summary> /// </summary>
/// <typeparam name="TProp">value type</typeparam> /// <typeparam name="TProp">The value type.</typeparam>
/// <param name="path">target location</param> /// <param name="path">The path of the property to remove.</param>
/// <returns></returns> /// <returns>The <see cref="JsonPatchDocument{TModel}"/>.</returns>
public JsonPatchDocument<TModel> Remove<TProp>(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>(
"remove", "remove",
@ -149,10 +153,10 @@ 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"></param> /// <param name="path">The path of the property to replace.</param>
/// <param name="value"></param> /// <param name="value">The value to replace.</param>
/// <returns></returns> /// <returns>The <see cref="JsonPatchDocument{TModel}"/>.</returns>
public JsonPatchDocument<TModel> Replace<TProp>(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>(
"replace", "replace",
@ -164,15 +168,16 @@ 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">value type</typeparam> /// <typeparam name="TProp">The value type.</typeparam>
/// <param name="path">target location</param> /// <param name="path">The path of the property to replace.</param>
/// <param name="position">position</param> /// <param name="position">The position in the list.</param>
/// <returns></returns> /// <returns>The <see cref="JsonPatchDocument{TModel}"/>.</returns>
public JsonPatchDocument<TModel> Replace<TProp>( public JsonPatchDocument<TModel> Replace<TProp>(
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",
@ -184,12 +189,14 @@ 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">value type</typeparam> /// <typeparam name="TProp">The value type.</typeparam>
/// <param name="path">target location</param> /// <param name="path">The path of the property to replace.</param>
/// <returns></returns> /// <returns>The <see cref="JsonPatchDocument{TModel}"/>.</returns>
public JsonPatchDocument<TModel> Replace<TProp>(Expression<Func<TModel, IList<TProp>>> path, TProp value) public JsonPatchDocument<TModel> Replace<TProp>(
[NotNull] Expression<Func<TModel, IList<TProp>>> path,
TProp value)
{ {
Operations.Add(new Operation<TModel>( Operations.Add(new Operation<TModel>(
"replace", "replace",
@ -204,12 +211,12 @@ 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"></param> /// <param name="from">The path of the property to remove.</param>
/// <param name="path"></param> /// <param name="path">The path of the property to add.</param>
/// <returns></returns> /// <returns>The <see cref="JsonPatchDocument{TModel}"/>.</returns>
public JsonPatchDocument<TModel> Move<TProp>( public JsonPatchDocument<TModel> Move<TProp>(
Expression<Func<TModel, TProp>> from, [NotNull] Expression<Func<TModel, TProp>> from,
Expression<Func<TModel, TProp>> path) [NotNull] Expression<Func<TModel, TProp>> path)
{ {
Operations.Add(new Operation<TModel>( Operations.Add(new Operation<TModel>(
"move", "move",
@ -220,17 +227,17 @@ 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"></typeparam> /// <typeparam name="TProp">The value type.</typeparam>
/// <param name="from"></param> /// <param name="from">The path of the property to remove.</param>
/// <param name="positionFrom"></param> /// <param name="positionFrom">The position in the list to remove.</param>
/// <param name="path"></param> /// <param name="path">The path of the property to add.</param>
/// <returns></returns> /// <returns>The <see cref="JsonPatchDocument{TModel}"/>.</returns>
public JsonPatchDocument<TModel> Move<TProp>( public JsonPatchDocument<TModel> Move<TProp>(
Expression<Func<TModel, IList<TProp>>> from, [NotNull] Expression<Func<TModel, IList<TProp>>> from,
int positionFrom, int positionFrom,
Expression<Func<TModel, TProp>> path) [NotNull] Expression<Func<TModel, TProp>> path)
{ {
Operations.Add(new Operation<TModel>( Operations.Add(new Operation<TModel>(
"move", "move",
@ -241,16 +248,16 @@ 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"></typeparam> /// <typeparam name="TProp">The value type.</typeparam>
/// <param name="from"></param> /// <param name="from">The path of the property 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>(
Expression<Func<TModel, TProp>> from, [NotNull] Expression<Func<TModel, TProp>> from,
Expression<Func<TModel, IList<TProp>>> path, [NotNull] Expression<Func<TModel, IList<TProp>>> path,
int positionTo) int positionTo)
{ {
Operations.Add(new Operation<TModel>( Operations.Add(new Operation<TModel>(
@ -262,17 +269,18 @@ 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"></typeparam> /// <typeparam name="TProp">The value type.</typeparam>
/// <param name="from"></param> /// <param name="from">The path of the property to remove.</param>
/// <param name="positionFrom"></param> /// <param name="positionFrom">The position in the list to remove.</param>
/// <param name="path"></param> /// <param name="path">The path of the property to add.</param>
/// <returns></returns> /// <param name="positionTo">The position in the list to add.</param>
/// <returns>The <see cref="JsonPatchDocument{TModel}"/>.</returns>
public JsonPatchDocument<TModel> Move<TProp>( public JsonPatchDocument<TModel> Move<TProp>(
Expression<Func<TModel, IList<TProp>>> from, [NotNull] Expression<Func<TModel, IList<TProp>>> from,
int positionFrom, int positionFrom,
Expression<Func<TModel, IList<TProp>>> path, [NotNull] Expression<Func<TModel, IList<TProp>>> path,
int positionTo) int positionTo)
{ {
Operations.Add(new Operation<TModel>( Operations.Add(new Operation<TModel>(
@ -284,17 +292,17 @@ 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"></typeparam> /// <typeparam name="TProp">The value type.</typeparam>
/// <param name="from"></param> /// <param name="from">The path of the property to remove.</param>
/// <param name="positionFrom"></param> /// <param name="positionFrom">The position in the list to remove.</param>
/// <param name="path"></param> /// <param name="path">The path of the property to add.</param>
/// <returns></returns> /// <returns>The <see cref="JsonPatchDocument{TModel}"/>.</returns>
public JsonPatchDocument<TModel> Move<TProp>( public JsonPatchDocument<TModel> Move<TProp>(
Expression<Func<TModel, IList<TProp>>> from, [NotNull] Expression<Func<TModel, IList<TProp>>> from,
int positionFrom, int positionFrom,
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",
@ -305,16 +313,15 @@ 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"></typeparam> /// <typeparam name="TProp">The value type.</typeparam>
/// <param name="from"></param> /// <param name="from">The path of the property 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>(
Expression<Func<TModel, TProp>> from, [NotNull] Expression<Func<TModel, TProp>> from,
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",
@ -325,15 +332,15 @@ namespace Microsoft.AspNet.JsonPatch
} }
/// <summary> /// <summary>
/// Copy the value at specified location to the target location. Willr esult in, for example: /// Copy the value at specified location to the target location. Will result 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"></param> /// <param name="from">The path of the property to copy from.</param>
/// <param name="path"></param> /// <param name="path">The path of the property to copy to.</param>
/// <returns></returns> /// <returns>The <see cref="JsonPatchDocument{TModel}"/>.</returns>
public JsonPatchDocument<TModel> Copy<TProp>( public JsonPatchDocument<TModel> Copy<TProp>(
Expression<Func<TModel, TProp>> from, [NotNull] Expression<Func<TModel, TProp>> from,
Expression<Func<TModel, TProp>> path) [NotNull] Expression<Func<TModel, TProp>> path)
{ {
Operations.Add(new Operation<TModel>( Operations.Add(new Operation<TModel>(
"copy", "copy",
@ -344,17 +351,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"></typeparam> /// <typeparam name="TProp">The value type.</typeparam>
/// <param name="from"></param> /// <param name="from">The path of the property to copy from.</param>
/// <param name="positionFrom"></param> /// <param name="positionFrom">The position in the list to copy from.</param>
/// <param name="path"></param> /// <param name="path">The path of the property to copy to.</param>
/// <returns></returns> /// <returns>The <see cref="JsonPatchDocument{TModel}"/>.</returns>
public JsonPatchDocument<TModel> Copy<TProp>( public JsonPatchDocument<TModel> Copy<TProp>(
Expression<Func<TModel, IList<TProp>>> from, [NotNull] Expression<Func<TModel, IList<TProp>>> from,
int positionFrom, int positionFrom,
Expression<Func<TModel, TProp>> path) [NotNull] Expression<Func<TModel, TProp>> path)
{ {
Operations.Add(new Operation<TModel>( Operations.Add(new Operation<TModel>(
"copy", "copy",
@ -365,16 +372,16 @@ 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"></typeparam> /// <typeparam name="TProp">The value type.</typeparam>
/// <param name="from"></param> /// <param name="from">The path of the property 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>(
Expression<Func<TModel, TProp>> from, [NotNull] Expression<Func<TModel, TProp>> from,
Expression<Func<TModel, IList<TProp>>> path, [NotNull] Expression<Func<TModel, IList<TProp>>> path,
int positionTo) int positionTo)
{ {
Operations.Add(new Operation<TModel>( Operations.Add(new Operation<TModel>(
@ -386,17 +393,18 @@ 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"></typeparam> /// <typeparam name="TProp">The value type.</typeparam>
/// <param name="from"></param> /// <param name="from">The path of the property to copy from.</param>
/// <param name="positionFrom"></param> /// <param name="positionFrom">The position in the list to copy from.</param>
/// <param name="path"></param> /// <param name="path">The path of the property to copy to.</param>
/// <returns></returns> /// <param name="positionTo">The position in the list to copy to.</param>
/// <returns>The <see cref="JsonPatchDocument{TModel}"/>.</returns>
public JsonPatchDocument<TModel> Copy<TProp>( public JsonPatchDocument<TModel> Copy<TProp>(
Expression<Func<TModel, IList<TProp>>> from, [NotNull] Expression<Func<TModel, IList<TProp>>> from,
int positionFrom, int positionFrom,
Expression<Func<TModel, IList<TProp>>> path, [NotNull] Expression<Func<TModel, IList<TProp>>> path,
int positionTo) int positionTo)
{ {
Operations.Add(new Operation<TModel>( Operations.Add(new Operation<TModel>(
@ -408,18 +416,17 @@ 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"></typeparam> /// <typeparam name="TProp">The value type.</typeparam>
/// <param name="from"></param> /// <param name="from">The path of the property to copy from.</param>
/// <param name="positionFrom"></param> /// <param name="positionFrom">The position in the list to copy from.</param>
/// <param name="path"></param> /// <param name="path">The path of the property to copy to.</param>
/// <returns></returns> /// <returns>The <see cref="JsonPatchDocument{TModel}"/>.</returns>
public JsonPatchDocument<TModel> Copy<TProp>( public JsonPatchDocument<TModel> Copy<TProp>(
Expression<Func<TModel, [NotNull] Expression<Func<TModel, IList<TProp>>> from,
IList<TProp>>> from,
int positionFrom, int positionFrom,
Expression<Func<TModel, IList<TProp>>> path) [NotNull] Expression<Func<TModel, IList<TProp>>> path)
{ {
Operations.Add(new Operation<TModel>( Operations.Add(new Operation<TModel>(
"copy", "copy",
@ -430,16 +437,15 @@ 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"></typeparam> /// <typeparam name="TProp">The value type.</typeparam>
/// <param name="from"></param> /// <param name="from">The path of the property 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>(
Expression<Func<TModel, TProp>> from, [NotNull] Expression<Func<TModel, TProp>> from,
Expression<Func<TModel, IList<TProp>>> path) [NotNull] Expression<Func<TModel, IList<TProp>>> path)
{ {
Operations.Add(new Operation<TModel>( Operations.Add(new Operation<TModel>(
"copy", "copy",
@ -449,17 +455,17 @@ namespace Microsoft.AspNet.JsonPatch
return this; return this;
} }
public void ApplyTo(TModel objectToApplyTo) public void ApplyTo([NotNull] TModel objectToApplyTo)
{ {
ApplyTo(objectToApplyTo, new ObjectAdapter<TModel>(ContractResolver, logErrorAction: null)); ApplyTo(objectToApplyTo, new ObjectAdapter<TModel>(ContractResolver, logErrorAction: null));
} }
public void ApplyTo(TModel objectToApplyTo, Action<JsonPatchError<TModel>> logErrorAction) public void ApplyTo([NotNull] TModel objectToApplyTo, Action<JsonPatchError<TModel>> logErrorAction)
{ {
ApplyTo(objectToApplyTo, new ObjectAdapter<TModel>(ContractResolver, logErrorAction)); ApplyTo(objectToApplyTo, new ObjectAdapter<TModel>(ContractResolver, logErrorAction));
} }
public void ApplyTo(TModel objectToApplyTo, IObjectAdapter<TModel> adapter) public void ApplyTo([NotNull] TModel objectToApplyTo, [NotNull] IObjectAdapter<TModel> adapter)
{ {
// apply each operation in order // apply each operation in order
foreach (var op in Operations) foreach (var op in Operations)

View File

@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved. // 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. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.Framework.Internal;
using Newtonsoft.Json; using Newtonsoft.Json;
namespace Microsoft.AspNet.JsonPatch.Operations namespace Microsoft.AspNet.JsonPatch.Operations
@ -15,13 +16,13 @@ namespace Microsoft.AspNet.JsonPatch.Operations
} }
public Operation(string op, string path, string from, object value) public Operation([NotNull] string op, [NotNull] string path, string from, object value)
: base(op, path, from) : base(op, path, from)
{ {
this.value = value; this.value = value;
} }
public Operation(string op, string path, string from) public Operation([NotNull] string op, [NotNull] string path, string from)
: base(op, path, from) : base(op, path, from)
{ {

View File

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using Microsoft.Framework.Internal;
using Newtonsoft.Json; using Newtonsoft.Json;
namespace Microsoft.AspNet.JsonPatch.Operations namespace Microsoft.AspNet.JsonPatch.Operations
@ -31,7 +32,7 @@ namespace Microsoft.AspNet.JsonPatch.Operations
} }
public OperationBase(string op, string path, string from) public OperationBase([NotNull] string op, [NotNull] string path, string from)
{ {
this.op = op; this.op = op;
this.path = path; this.path = path;
@ -40,7 +41,7 @@ namespace Microsoft.AspNet.JsonPatch.Operations
public bool ShouldSerializefrom() public bool ShouldSerializefrom()
{ {
return (OperationType == Operations.OperationType.Move return (OperationType == OperationType.Move
|| OperationType == OperationType.Copy); || OperationType == OperationType.Copy);
} }
} }

View File

@ -3,6 +3,7 @@
using System; using System;
using Microsoft.AspNet.JsonPatch.Adapters; using Microsoft.AspNet.JsonPatch.Adapters;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.JsonPatch.Operations namespace Microsoft.AspNet.JsonPatch.Operations
{ {
@ -13,19 +14,19 @@ namespace Microsoft.AspNet.JsonPatch.Operations
} }
public Operation(string op, string path, string from, object value) public Operation([NotNull] string op, [NotNull] string path, string from, object value)
: base(op, path, from) : base(op, path, from)
{ {
this.value = value; this.value = value;
} }
public Operation(string op, string path, string from) public Operation([NotNull] string op, [NotNull] string path, string from)
: base(op, path, from) : base(op, path, from)
{ {
} }
public void Apply(TModel objectToApplyTo, IObjectAdapter<TModel> adapter) public void Apply([NotNull] TModel objectToApplyTo, [NotNull] IObjectAdapter<TModel> adapter)
{ {
switch (OperationType) switch (OperationType)
{ {
@ -53,7 +54,7 @@ namespace Microsoft.AspNet.JsonPatch.Operations
public bool ShouldSerializevalue() public bool ShouldSerializevalue()
{ {
return (OperationType == Operations.OperationType.Add return (OperationType == OperationType.Add
|| OperationType == OperationType.Replace || OperationType == OperationType.Replace
|| OperationType == OperationType.Test); || OperationType == OperationType.Test);
} }