From 38748b54eb7ef8e7cba2365d0621fcf4826dda4b Mon Sep 17 00:00:00 2001 From: Kirthi Krishnamraju Date: Wed, 1 Jul 2015 16:12:40 -0700 Subject: [PATCH] [JsonPatch]: Added NotNull attribute to parameters and updated comments --- .../Adapters/ObjectAdapter.cs | 43 +-- .../Exceptions/JsonPatchException.cs | 5 +- .../Helpers/JsonPatchProperty.cs | 3 +- .../JsonPatchDocumentOfT.cs | 300 +++++++++--------- .../Operations/Operation.cs | 5 +- .../Operations/OperationBase.cs | 5 +- .../Operations/OperationOfT.cs | 9 +- 7 files changed, 194 insertions(+), 176 deletions(-) diff --git a/src/Microsoft.AspNet.JsonPatch/Adapters/ObjectAdapter.cs b/src/Microsoft.AspNet.JsonPatch/Adapters/ObjectAdapter.cs index 3c472c1a50..42ae4d7222 100644 --- a/src/Microsoft.AspNet.JsonPatch/Adapters/ObjectAdapter.cs +++ b/src/Microsoft.AspNet.JsonPatch/Adapters/ObjectAdapter.cs @@ -22,7 +22,9 @@ namespace Microsoft.AspNet.JsonPatch.Adapters /// /// The . /// The for logging . - public ObjectAdapter(IContractResolver contractResolver, Action> logErrorAction) + public ObjectAdapter( + [NotNull] IContractResolver contractResolver, + Action> logErrorAction) { ContractResolver = contractResolver; LogErrorAction = logErrorAction; @@ -97,8 +99,8 @@ namespace Microsoft.AspNet.JsonPatch.Adapters /// because "a" does not exist. /// /// The add operation. - /// Object to apply the operation to. - public void Add(Operation operation, TModel objectToApplyTo) + /// Object to apply the operation to. + public void Add([NotNull] Operation operation, [NotNull] TModel objectToApplyTo) { 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; /// This method allows code reuse yet reporting the correct operation on error /// - private void Add(string path, object value, TModel objectToApplyTo, Operation operationToReport) + private void Add( + [NotNull] string path, + object value, + [NotNull] TModel objectToApplyTo, + [NotNull] Operation operationToReport) { // add, in this implementation, does not just "add" properties - that's // 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. /// /// The move operation. - /// Object to apply the operation to. - public void Move(Operation operation, TModel objectToApplyTo) + /// Object to apply the operation to. + public void Move([NotNull] Operation operation, [NotNull] TModel objectToApplyTo) { // get value at from location object valueAtFromLocation = null; @@ -322,8 +328,8 @@ namespace Microsoft.AspNet.JsonPatch.Adapters /// specified index are shifted one position to the left. /// /// The remove operation. - /// Object to apply the operation to. - public void Remove(Operation operation, TModel objectToApplyTo) + /// Object to apply the operation to. + public void Remove([NotNull] Operation operation, [NotNull] TModel objectToApplyTo) { 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; /// This method allows code reuse yet reporting the correct operation on error /// - private void Remove(string path, TModel objectToApplyTo, Operation operationToReport) + private void Remove( + [NotNull] string path, + [NotNull] TModel objectToApplyTo, + [NotNull] Operation operationToReport) { var removeFromList = false; var positionAsInteger = -1; @@ -442,8 +451,8 @@ namespace Microsoft.AspNet.JsonPatch.Adapters /// for performance reasons (multiple checks of same requirements). /// /// The replace operation. - /// Object to apply the operation to. - public void Replace(Operation operation, TModel objectToApplyTo) + /// Object to apply the operation to. + public void Replace([NotNull] Operation operation, [NotNull] TModel objectToApplyTo) { Remove(operation.path, 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 copy operation. - /// Object to apply the operation to. - public void Copy(Operation operation, TModel objectToApplyTo) + /// Object to apply the operation to. + public void Copy([NotNull] Operation operation, [NotNull] TModel objectToApplyTo) { // get value at from location object valueAtFromLocation = null; @@ -610,9 +619,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters } } - private JsonPatchProperty FindPropertyAndParent( - object targetObject, - string propertyPath) + private JsonPatchProperty FindPropertyAndParent(object targetObject, string propertyPath) { try { @@ -674,7 +681,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters } } - private Type GetIListType([NotNull] Type type) + private Type GetIListType(Type type) { if (IsGenericListType(type)) { @@ -692,7 +699,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters return null; } - private bool IsGenericListType([NotNull] Type type) + private bool IsGenericListType(Type type) { if (type.GetTypeInfo().IsGenericType && type.GetGenericTypeDefinition() == typeof(IList<>)) diff --git a/src/Microsoft.AspNet.JsonPatch/Exceptions/JsonPatchException.cs b/src/Microsoft.AspNet.JsonPatch/Exceptions/JsonPatchException.cs index 28a1accd05..8fcf2b3e28 100644 --- a/src/Microsoft.AspNet.JsonPatch/Exceptions/JsonPatchException.cs +++ b/src/Microsoft.AspNet.JsonPatch/Exceptions/JsonPatchException.cs @@ -3,6 +3,7 @@ using System; using Microsoft.AspNet.JsonPatch.Operations; +using Microsoft.Framework.Internal; namespace Microsoft.AspNet.JsonPatch.Exceptions { @@ -27,14 +28,14 @@ namespace Microsoft.AspNet.JsonPatch.Exceptions } - public JsonPatchException(JsonPatchError jsonPatchError) + public JsonPatchException([NotNull] JsonPatchError jsonPatchError) { FailedOperation = jsonPatchError.Operation; _message = jsonPatchError.ErrorMessage; AffectedObject = jsonPatchError.AffectedObject; } - public JsonPatchException(JsonPatchError jsonPatchError, Exception innerException) + public JsonPatchException([NotNull] JsonPatchError jsonPatchError, Exception innerException) : this(jsonPatchError) { InnerException = innerException; diff --git a/src/Microsoft.AspNet.JsonPatch/Helpers/JsonPatchProperty.cs b/src/Microsoft.AspNet.JsonPatch/Helpers/JsonPatchProperty.cs index 8e46af37b7..7ba0c047cc 100644 --- a/src/Microsoft.AspNet.JsonPatch/Helpers/JsonPatchProperty.cs +++ b/src/Microsoft.AspNet.JsonPatch/Helpers/JsonPatchProperty.cs @@ -1,6 +1,7 @@ // 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 Microsoft.Framework.Internal; using Newtonsoft.Json.Serialization; namespace Microsoft.AspNet.JsonPatch @@ -13,7 +14,7 @@ namespace Microsoft.AspNet.JsonPatch /// /// Initializes a new instance. /// - public JsonPatchProperty(JsonProperty property, object parent) + public JsonPatchProperty([NotNull] JsonProperty property, [NotNull] object parent) { Property = property; Parent = parent; diff --git a/src/Microsoft.AspNet.JsonPatch/JsonPatchDocumentOfT.cs b/src/Microsoft.AspNet.JsonPatch/JsonPatchDocumentOfT.cs index a8bf5f95a7..0c631461c3 100644 --- a/src/Microsoft.AspNet.JsonPatch/JsonPatchDocumentOfT.cs +++ b/src/Microsoft.AspNet.JsonPatch/JsonPatchDocumentOfT.cs @@ -8,6 +8,7 @@ using Microsoft.AspNet.JsonPatch.Adapters; using Microsoft.AspNet.JsonPatch.Converters; using Microsoft.AspNet.JsonPatch.Helpers; using Microsoft.AspNet.JsonPatch.Operations; +using Microsoft.Framework.Internal; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; @@ -31,8 +32,10 @@ namespace Microsoft.AspNet.JsonPatch ContractResolver = new DefaultContractResolver(); } - // Create from list of operations - public JsonPatchDocument(List> operations, IContractResolver contractResolver) + // Create from list of operations. + public JsonPatchDocument( + [NotNull] List> operations, + [NotNull] IContractResolver contractResolver) { Operations = operations; ContractResolver = contractResolver; @@ -42,11 +45,11 @@ namespace Microsoft.AspNet.JsonPatch /// Add operation. Will result in, for example, /// { "op": "add", "path": "/a/b/c", "value": [ "foo", "bar" ] } /// - /// value type - /// path - /// value - /// - public JsonPatchDocument Add(Expression> path, TProp value) + /// The value type. + /// The path of the property to add. + /// The value to add. + /// The . + public JsonPatchDocument Add([NotNull] Expression> path, TProp value) { Operations.Add(new Operation( "add", @@ -58,16 +61,15 @@ namespace Microsoft.AspNet.JsonPatch } /// - /// Add value to list at given position + /// Add value to list at given position. /// - /// value type - /// path - /// value + /// The value type. + /// The path of the property to add. + /// The value to add. /// position - /// + /// The . public JsonPatchDocument Add( - Expression>> path, + [NotNull] Expression>> path, TProp value, int position) { @@ -81,13 +83,13 @@ namespace Microsoft.AspNet.JsonPatch } /// - /// At value at end of list + /// At value at end of list. /// - /// value type - /// path - /// value - /// - public JsonPatchDocument Add(Expression>> path, TProp value) + /// The value type. + /// The path of the property to add. + /// The value to add. + /// The . + public JsonPatchDocument Add([NotNull] Expression>> path, TProp value) { Operations.Add(new Operation( "add", @@ -102,24 +104,26 @@ namespace Microsoft.AspNet.JsonPatch /// Remove value at target location. Will result in, for example, /// { "op": "remove", "path": "/a/b/c" } /// - /// - /// - /// - public JsonPatchDocument Remove(Expression> path) + /// The path of the property to remove. + /// The . + public JsonPatchDocument Remove([NotNull] Expression> path) { - Operations.Add(new Operation("remove", ExpressionHelpers.GetPath(path).ToLowerInvariant(), from: null)); + Operations.Add( + new Operation("remove", ExpressionHelpers.GetPath(path).ToLowerInvariant(), from: null)); return this; } /// - /// Remove value from list at given position + /// Remove value from list at given position. /// - /// value type - /// target location - /// position - /// - public JsonPatchDocument Remove(Expression>> path, int position) + /// The value type. + /// The path of the property to remove. + /// The position in the list. + /// The . + public JsonPatchDocument Remove( + [NotNull] Expression>> path, + int position) { Operations.Add(new Operation( "remove", @@ -130,12 +134,12 @@ namespace Microsoft.AspNet.JsonPatch } /// - /// Remove value from end of list + /// Remove value from end of list. /// - /// value type - /// target location - /// - public JsonPatchDocument Remove(Expression>> path) + /// The value type. + /// The path of the property to remove. + /// The . + public JsonPatchDocument Remove([NotNull] Expression>> path) { Operations.Add(new Operation( "remove", @@ -149,10 +153,10 @@ namespace Microsoft.AspNet.JsonPatch /// Replace value. Will result in, for example, /// { "op": "replace", "path": "/a/b/c", "value": 42 } /// - /// - /// - /// - public JsonPatchDocument Replace(Expression> path, TProp value) + /// The path of the property to replace. + /// The value to replace. + /// The . + public JsonPatchDocument Replace([NotNull] Expression> path, TProp value) { Operations.Add(new Operation( "replace", @@ -164,15 +168,16 @@ namespace Microsoft.AspNet.JsonPatch } /// - /// Replace value in a list at given position + /// Replace value in a list at given position. /// - /// value type - /// target location - /// position - /// + /// The value type. + /// The path of the property to replace. + /// The position in the list. + /// The . public JsonPatchDocument Replace( - Expression>> path, - TProp value, int position) + [NotNull] Expression>> path, + TProp value, + int position) { Operations.Add(new Operation( "replace", @@ -184,12 +189,14 @@ namespace Microsoft.AspNet.JsonPatch } /// - /// Replace value at end of a list + /// Replace value at end of a list. /// - /// value type - /// target location - /// - public JsonPatchDocument Replace(Expression>> path, TProp value) + /// The value type. + /// The path of the property to replace. + /// The . + public JsonPatchDocument Replace( + [NotNull] Expression>> path, + TProp value) { Operations.Add(new Operation( "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: /// { "op": "move", "from": "/a/b/c", "path": "/a/b/d" } /// - /// - /// - /// + /// The path of the property to remove. + /// The path of the property to add. + /// The . public JsonPatchDocument Move( - Expression> from, - Expression> path) + [NotNull] Expression> from, + [NotNull] Expression> path) { Operations.Add(new Operation( "move", @@ -220,17 +227,17 @@ namespace Microsoft.AspNet.JsonPatch } /// - /// Move from a position in a list to a new location + /// Move from a position in a list to a new location. /// - /// - /// - /// - /// - /// + /// The value type. + /// The path of the property to remove. + /// The position in the list to remove. + /// The path of the property to add. + /// The . public JsonPatchDocument Move( - Expression>> from, + [NotNull] Expression>> from, int positionFrom, - Expression> path) + [NotNull] Expression> path) { Operations.Add(new Operation( "move", @@ -241,16 +248,16 @@ namespace Microsoft.AspNet.JsonPatch } /// - /// Move from a property to a location in a list + /// Move from a property to a location in a list. /// - /// - /// - /// - /// - /// + /// The value type. + /// The path of the property to remove. + /// The path of the property to add. + /// The position in the list to add. + /// The . public JsonPatchDocument Move( - Expression> from, - Expression>> path, + [NotNull] Expression> from, + [NotNull] Expression>> path, int positionTo) { Operations.Add(new Operation( @@ -262,17 +269,18 @@ namespace Microsoft.AspNet.JsonPatch } /// - /// 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. /// - /// - /// - /// - /// - /// + /// The value type. + /// The path of the property to remove. + /// The position in the list to remove. + /// The path of the property to add. + /// The position in the list to add. + /// The . public JsonPatchDocument Move( - Expression>> from, + [NotNull] Expression>> from, int positionFrom, - Expression>> path, + [NotNull] Expression>> path, int positionTo) { Operations.Add(new Operation( @@ -284,17 +292,17 @@ namespace Microsoft.AspNet.JsonPatch } /// - /// 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. /// - /// - /// - /// - /// - /// + /// The value type. + /// The path of the property to remove. + /// The position in the list to remove. + /// The path of the property to add. + /// The . public JsonPatchDocument Move( - Expression>> from, + [NotNull] Expression>> from, int positionFrom, - Expression>> path) + [NotNull] Expression>> path) { Operations.Add(new Operation( "move", @@ -305,16 +313,15 @@ namespace Microsoft.AspNet.JsonPatch } /// - /// Move to the end of a list + /// Move to the end of a list. /// - /// - /// - /// - /// - /// + /// The value type. + /// The path of the property to remove. + /// The path of the property to add. + /// The . public JsonPatchDocument Move( - Expression> from, - Expression>> path) + [NotNull] Expression> from, + [NotNull] Expression>> path) { Operations.Add(new Operation( "move", @@ -325,15 +332,15 @@ namespace Microsoft.AspNet.JsonPatch } /// - /// 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" } /// - /// - /// - /// + /// The path of the property to copy from. + /// The path of the property to copy to. + /// The . public JsonPatchDocument Copy( - Expression> from, - Expression> path) + [NotNull] Expression> from, + [NotNull] Expression> path) { Operations.Add(new Operation( "copy", @@ -344,17 +351,17 @@ namespace Microsoft.AspNet.JsonPatch } /// - /// Copy from a position in a list to a new location + /// Copy from a position in a list to a new location. /// - /// - /// - /// - /// - /// + /// The value type. + /// The path of the property to copy from. + /// The position in the list to copy from. + /// The path of the property to copy to. + /// The . public JsonPatchDocument Copy( - Expression>> from, + [NotNull] Expression>> from, int positionFrom, - Expression> path) + [NotNull] Expression> path) { Operations.Add(new Operation( "copy", @@ -365,16 +372,16 @@ namespace Microsoft.AspNet.JsonPatch } /// - /// Copy from a property to a location in a list + /// Copy from a property to a location in a list. /// - /// - /// - /// - /// - /// + /// The value type. + /// The path of the property to copy from. + /// The path of the property to copy to. + /// The position in the list to copy to. + /// The . public JsonPatchDocument Copy( - Expression> from, - Expression>> path, + [NotNull] Expression> from, + [NotNull] Expression>> path, int positionTo) { Operations.Add(new Operation( @@ -386,17 +393,18 @@ namespace Microsoft.AspNet.JsonPatch } /// - /// 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. /// - /// - /// - /// - /// - /// + /// The value type. + /// The path of the property to copy from. + /// The position in the list to copy from. + /// The path of the property to copy to. + /// The position in the list to copy to. + /// The . public JsonPatchDocument Copy( - Expression>> from, + [NotNull] Expression>> from, int positionFrom, - Expression>> path, + [NotNull] Expression>> path, int positionTo) { Operations.Add(new Operation( @@ -408,18 +416,17 @@ namespace Microsoft.AspNet.JsonPatch } /// - /// 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. /// - /// - /// - /// - /// - /// + /// The value type. + /// The path of the property to copy from. + /// The position in the list to copy from. + /// The path of the property to copy to. + /// The . public JsonPatchDocument Copy( - Expression>> from, + [NotNull] Expression>> from, int positionFrom, - Expression>> path) + [NotNull] Expression>> path) { Operations.Add(new Operation( "copy", @@ -430,16 +437,15 @@ namespace Microsoft.AspNet.JsonPatch } /// - /// Copy to the end of a list + /// Copy to the end of a list. /// - /// - /// - /// - /// - /// + /// The value type. + /// The path of the property to copy from. + /// The path of the property to copy to. + /// The . public JsonPatchDocument Copy( - Expression> from, - Expression>> path) + [NotNull] Expression> from, + [NotNull] Expression>> path) { Operations.Add(new Operation( "copy", @@ -449,17 +455,17 @@ namespace Microsoft.AspNet.JsonPatch return this; } - public void ApplyTo(TModel objectToApplyTo) + public void ApplyTo([NotNull] TModel objectToApplyTo) { ApplyTo(objectToApplyTo, new ObjectAdapter(ContractResolver, logErrorAction: null)); } - public void ApplyTo(TModel objectToApplyTo, Action> logErrorAction) + public void ApplyTo([NotNull] TModel objectToApplyTo, Action> logErrorAction) { ApplyTo(objectToApplyTo, new ObjectAdapter(ContractResolver, logErrorAction)); } - public void ApplyTo(TModel objectToApplyTo, IObjectAdapter adapter) + public void ApplyTo([NotNull] TModel objectToApplyTo, [NotNull] IObjectAdapter adapter) { // apply each operation in order foreach (var op in Operations) diff --git a/src/Microsoft.AspNet.JsonPatch/Operations/Operation.cs b/src/Microsoft.AspNet.JsonPatch/Operations/Operation.cs index 301b705912..f28357ec7d 100644 --- a/src/Microsoft.AspNet.JsonPatch/Operations/Operation.cs +++ b/src/Microsoft.AspNet.JsonPatch/Operations/Operation.cs @@ -1,6 +1,7 @@ // 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 Microsoft.Framework.Internal; using Newtonsoft.Json; 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) { 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) { diff --git a/src/Microsoft.AspNet.JsonPatch/Operations/OperationBase.cs b/src/Microsoft.AspNet.JsonPatch/Operations/OperationBase.cs index 5655268618..05dd590e89 100644 --- a/src/Microsoft.AspNet.JsonPatch/Operations/OperationBase.cs +++ b/src/Microsoft.AspNet.JsonPatch/Operations/OperationBase.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using Microsoft.Framework.Internal; using Newtonsoft.Json; 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.path = path; @@ -40,7 +41,7 @@ namespace Microsoft.AspNet.JsonPatch.Operations public bool ShouldSerializefrom() { - return (OperationType == Operations.OperationType.Move + return (OperationType == OperationType.Move || OperationType == OperationType.Copy); } } diff --git a/src/Microsoft.AspNet.JsonPatch/Operations/OperationOfT.cs b/src/Microsoft.AspNet.JsonPatch/Operations/OperationOfT.cs index 644f44f49c..f28ff7369a 100644 --- a/src/Microsoft.AspNet.JsonPatch/Operations/OperationOfT.cs +++ b/src/Microsoft.AspNet.JsonPatch/Operations/OperationOfT.cs @@ -3,6 +3,7 @@ using System; using Microsoft.AspNet.JsonPatch.Adapters; +using Microsoft.Framework.Internal; 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) { 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) { } - public void Apply(TModel objectToApplyTo, IObjectAdapter adapter) + public void Apply([NotNull] TModel objectToApplyTo, [NotNull] IObjectAdapter adapter) { switch (OperationType) { @@ -53,7 +54,7 @@ namespace Microsoft.AspNet.JsonPatch.Operations public bool ShouldSerializevalue() { - return (OperationType == Operations.OperationType.Add + return (OperationType == OperationType.Add || OperationType == OperationType.Replace || OperationType == OperationType.Test); }