From 3042a62ba95c014179dd5604dc10fecfbd4c924a Mon Sep 17 00:00:00 2001 From: Kirthi Krishnamraju Date: Mon, 20 Apr 2015 12:50:02 -0700 Subject: [PATCH] Removed Test operation code and changed T to TModel --- .../Adapters/IObjectAdapter.cs | 15 +- .../Adapters/ObjectAdapter.cs | 207 +++----------- .../Exceptions/JsonPatchException.cs | 10 +- .../Helpers/ExpressionHelpers.cs | 2 +- ...tchDocument.cs => JsonPatchDocumentOfT.cs} | 253 ++++++++++++------ .../JsonPatchError.cs | 16 +- .../{TypedOperation.cs => OperationOfT.cs} | 8 +- .../Properties/Resources.Designer.cs | 16 ++ src/Microsoft.AspNet.JsonPatch/Resources.resx | 3 + 9 files changed, 259 insertions(+), 271 deletions(-) rename src/Microsoft.AspNet.JsonPatch/{TypedJsonPatchDocument.cs => JsonPatchDocumentOfT.cs} (53%) rename src/Microsoft.AspNet.JsonPatch/Operations/{TypedOperation.cs => OperationOfT.cs} (86%) diff --git a/src/Microsoft.AspNet.JsonPatch/Adapters/IObjectAdapter.cs b/src/Microsoft.AspNet.JsonPatch/Adapters/IObjectAdapter.cs index 76e11d7cb9..c7928079da 100644 --- a/src/Microsoft.AspNet.JsonPatch/Adapters/IObjectAdapter.cs +++ b/src/Microsoft.AspNet.JsonPatch/Adapters/IObjectAdapter.cs @@ -8,14 +8,13 @@ namespace Microsoft.AspNet.JsonPatch.Adapters /// /// Defines the operations that can be performed on a JSON patch document. /// - /// - public interface IObjectAdapter where T : class + /// The type of the model. + public interface IObjectAdapter where TModel : class { - void Add(Operation operation, T objectToApplyTo); - void Copy(Operation operation, T objectToApplyTo); - void Move(Operation operation, T objectToApplyTo); - void Remove(Operation operation, T objectToApplyTo); - void Replace(Operation operation, T objectToApplyTo); - void Test(Operation operation, T objectToApplyTo); + void Add(Operation operation, TModel objectToApplyTo); + void Copy(Operation operation, TModel objectToApplyTo); + void Move(Operation operation, TModel objectToApplyTo); + void Remove(Operation operation, TModel objectToApplyTo); + void Replace(Operation operation, TModel objectToApplyTo); } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.JsonPatch/Adapters/ObjectAdapter.cs b/src/Microsoft.AspNet.JsonPatch/Adapters/ObjectAdapter.cs index 25075d47dc..e22967e8da 100644 --- a/src/Microsoft.AspNet.JsonPatch/Adapters/ObjectAdapter.cs +++ b/src/Microsoft.AspNet.JsonPatch/Adapters/ObjectAdapter.cs @@ -15,14 +15,14 @@ using Newtonsoft.Json.Serialization; namespace Microsoft.AspNet.JsonPatch.Adapters { /// - public class ObjectAdapter : IObjectAdapter where T : class + public class ObjectAdapter : IObjectAdapter where TModel : class { /// - /// Initializes a new instance of . + /// Initializes a new instance of . /// /// The . - /// The for logging . - public ObjectAdapter(IContractResolver contractResolver, Action> logErrorAction) + /// The for logging . + public ObjectAdapter(IContractResolver contractResolver, Action> logErrorAction) { ContractResolver = contractResolver; LogErrorAction = logErrorAction; @@ -34,9 +34,9 @@ namespace Microsoft.AspNet.JsonPatch.Adapters public IContractResolver ContractResolver { get; } /// - /// Action for logging . + /// Action for logging . /// - public Action> LogErrorAction { get; } + public Action> LogErrorAction { get; } /// /// The "add" operation performs one of the following functions, @@ -96,9 +96,9 @@ namespace Microsoft.AspNet.JsonPatch.Adapters /// /// because "a" does not exist. /// - /// The add operation - /// Object to apply the operation to - public void Add(Operation operation, T objectToApplyTo) + /// The add operation. + /// Object to apply the operation to. + public void Add(Operation operation, TModel objectToApplyTo) { Add(operation.path, operation.value, objectToApplyTo, operation); } @@ -107,7 +107,7 @@ 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, T objectToApplyTo, Operation operationToReport) + private void Add(string path, object value, TModel objectToApplyTo, 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, @@ -178,7 +178,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters } else { - LogError(new JsonPatchError( + LogError(new JsonPatchError( objectToApplyTo, operationToReport, Resources.FormatInvalidIndexForArrayProperty(operationToReport.op, path))); @@ -189,7 +189,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters } else { - LogError(new JsonPatchError( + LogError(new JsonPatchError( objectToApplyTo, operationToReport, Resources.FormatInvalidIndexForArrayProperty(operationToReport.op, path))); @@ -236,9 +236,9 @@ namespace Microsoft.AspNet.JsonPatch.Adapters /// The "from" location MUST NOT be a proper prefix of the "path" /// 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, T objectToApplyTo) + /// The move operation. + /// Object to apply the operation to. + public void Move(Operation operation, TModel objectToApplyTo) { // get value at from location object valueAtFromLocation = null; @@ -275,7 +275,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters if (array.Count <= positionAsInteger) { - LogError(new JsonPatchError( + LogError(new JsonPatchError( objectToApplyTo, operation, Resources.FormatInvalidIndexForArrayProperty(operation.op, operation.from))); @@ -287,7 +287,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters } else { - LogError(new JsonPatchError( + LogError(new JsonPatchError( objectToApplyTo, operation, Resources.FormatInvalidPathForArrayProperty(operation.op, operation.from))); @@ -321,9 +321,9 @@ namespace Microsoft.AspNet.JsonPatch.Adapters /// If removing an element from an array, any elements above the /// specified index are shifted one position to the left. /// - /// The remove operation - /// Object to apply the operation to - public void Remove(Operation operation, T objectToApplyTo) + /// The remove operation. + /// Object to apply the operation to. + public void Remove(Operation operation, TModel objectToApplyTo) { Remove(operation.path, objectToApplyTo, operation); } @@ -332,7 +332,7 @@ 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, T objectToApplyTo, Operation operationToReport) + private void Remove(string path, TModel objectToApplyTo, Operation operationToReport) { var removeFromList = false; var positionAsInteger = -1; @@ -388,7 +388,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters } else { - LogError(new JsonPatchError( + LogError(new JsonPatchError( objectToApplyTo, operationToReport, Resources.FormatInvalidIndexForArrayProperty(operationToReport.op, path))); @@ -399,7 +399,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters } else { - LogError(new JsonPatchError( + LogError(new JsonPatchError( objectToApplyTo, operationToReport, Resources.FormatInvalidPathForArrayProperty(operationToReport.op, path))); @@ -423,133 +423,6 @@ namespace Microsoft.AspNet.JsonPatch.Adapters } } - /// - /// The "test" operation tests that a value at the target location is - /// equal to a specified value. - /// - /// The operation object MUST contain a "value" member that conveys the - /// value to be compared to the target location's value. - /// - /// The target location MUST be equal to the "value" value for the - /// operation to be considered successful. - /// - /// Here, "equal" means that the value at the target location and the - /// value conveyed by "value" are of the same JSON type, and that they - /// are considered equal by the following rules for that type: - /// - /// o strings: are considered equal if they contain the same number of - /// Unicode characters and their code points are byte-by-byte equal. - /// - /// o numbers: are considered equal if their values are numerically - /// equal. - /// - /// o arrays: are considered equal if they contain the same number of - /// values, and if each value can be considered equal to the value at - /// the corresponding position in the other array, using this list of - /// type-specific rules. - /// - /// o objects: are considered equal if they contain the same number of - /// members, and if each member can be considered equal to a member in - /// the other object, by comparing their keys (as strings) and their - /// values (using this list of type-specific rules). - /// - /// o literals (false, true, and null): are considered equal if they are - /// the same. - /// - /// Note that the comparison that is done is a logical comparison; e.g., - /// whitespace between the member values of an array is not significant. - /// - /// Also, note that ordering of the serialization of object members is - /// not significant. - /// - /// Note that we divert from the rules here - we use .NET's comparison, - /// not the one above. In a future version, a "strict" setting might - /// be added (configurable), that takes into account above rules. - /// - /// For example: - /// - /// { "op": "test", "path": "/a/b/c", "value": "foo" } - /// - /// The test operation - /// Object to apply the operation to - public void Test(Operation operation, T objectToApplyTo) - { - // get value at path location - object valueAtPathLocation = null; - var positionInPathAsInteger = -1; - var actualPathProperty = operation.path; - - positionInPathAsInteger = GetNumericEnd(operation.path); - - if (positionInPathAsInteger > -1) - { - actualPathProperty = operation.path.Substring(0, - operation.path.IndexOf('/' + positionInPathAsInteger.ToString())); - } - - var patchProperty = FindPropertyAndParent(objectToApplyTo, actualPathProperty); - - // does property at path exist? - if (!CheckIfPropertyExists(patchProperty, objectToApplyTo, operation, operation.path)) - { - return; - } - - // get the property path - Type typeOfFinalPropertyAtPathLocation = null; - - // is the path an array (but not a string (= char[]))? In this case, - // the path must end with "/position" or "/-", which we already determined before. - if (positionInPathAsInteger > -1) - { - if (IsNonStringArray(patchProperty.Property.PropertyType)) - { - // now, get the generic type of the IList<> from Property type. - typeOfFinalPropertyAtPathLocation = GetIListType(patchProperty.Property.PropertyType); - - // get value (it can be cast, we just checked that) - var array = (IList)patchProperty.Property.ValueProvider.GetValue(patchProperty.Parent); - - if (array.Count <= positionInPathAsInteger) - { - LogError(new JsonPatchError( - objectToApplyTo, - operation, - Resources.FormatInvalidIndexForArrayProperty(operation.op, operation.path))); - - return; - } - - valueAtPathLocation = array[positionInPathAsInteger]; - } - else - { - LogError(new JsonPatchError( - objectToApplyTo, - operation, - Resources.FormatInvalidPathForArrayProperty(operation.op, operation.path))); - - return; - } - } - else - { - // no list, just get the value - valueAtPathLocation = patchProperty.Property.ValueProvider.GetValue(patchProperty.Parent); - typeOfFinalPropertyAtPathLocation = patchProperty.Property.PropertyType; - } - - var conversionResultTuple = ConvertToActualType(typeOfFinalPropertyAtPathLocation, operation.value); - - // Is conversion successful - if (!CheckIfPropertyCanBeSet(conversionResultTuple, objectToApplyTo, operation, operation.path)) - { - return; - } - - //Compare - } - /// /// The "replace" operation replaces the value at the target location /// with a new value. The operation object MUST contain a "value" member @@ -568,9 +441,9 @@ namespace Microsoft.AspNet.JsonPatch.Adapters /// Note: even though it's the same functionally, we do not call remove + add /// for performance reasons (multiple checks of same requirements). /// - /// The replace operation - /// Object to apply the operation to - public void Replace(Operation operation, T objectToApplyTo) + /// The replace operation. + /// Object to apply the operation to. + public void Replace(Operation operation, TModel objectToApplyTo) { Remove(operation.path, objectToApplyTo, operation); Add(operation.path, operation.value, objectToApplyTo, operation); @@ -596,9 +469,9 @@ namespace Microsoft.AspNet.JsonPatch.Adapters /// Note: even though it's the same functionally, we do not call add with /// 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, T objectToApplyTo) + /// The copy operation. + /// Object to apply the operation to. + public void Copy(Operation operation, TModel objectToApplyTo) { // get value at from location object valueAtFromLocation = null; @@ -636,7 +509,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters if (array.Count <= positionAsInteger) { - LogError(new JsonPatchError( + LogError(new JsonPatchError( objectToApplyTo, operation, Resources.FormatInvalidIndexForArrayProperty(operation.op, operation.from))); @@ -648,7 +521,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters } else { - LogError(new JsonPatchError( + LogError(new JsonPatchError( objectToApplyTo, operation, Resources.FormatInvalidPathForArrayProperty(operation.op, operation.from))); @@ -669,13 +542,13 @@ namespace Microsoft.AspNet.JsonPatch.Adapters private bool CheckIfPropertyExists( JsonPatchProperty patchProperty, - T objectToApplyTo, - Operation operation, + TModel objectToApplyTo, + Operation operation, string propertyPath) { if (patchProperty == null) { - LogError(new JsonPatchError( + LogError(new JsonPatchError( objectToApplyTo, operation, Resources.FormatPropertyDoesNotExist(propertyPath))); @@ -685,7 +558,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters if (patchProperty.Property.Ignored) { - LogError(new JsonPatchError( + LogError(new JsonPatchError( objectToApplyTo, operation, Resources.FormatCannotUpdateProperty(propertyPath))); @@ -708,13 +581,13 @@ namespace Microsoft.AspNet.JsonPatch.Adapters private bool CheckIfPropertyCanBeSet( ConversionResult result, - T objectToApplyTo, - Operation operation, + TModel objectToApplyTo, + Operation operation, string path) { if (!result.CanBeConverted) { - LogError(new JsonPatchError( + LogError(new JsonPatchError( objectToApplyTo, operation, Resources.FormatInvalidValueForProperty(result.ConvertedInstance, path))); @@ -725,7 +598,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters return true; } - private void LogError(JsonPatchError jsonPatchError) + private void LogError(JsonPatchError jsonPatchError) { if (LogErrorAction != null) { @@ -733,7 +606,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters } else { - throw new JsonPatchException(jsonPatchError); + throw new JsonPatchException(jsonPatchError); } } diff --git a/src/Microsoft.AspNet.JsonPatch/Exceptions/JsonPatchException.cs b/src/Microsoft.AspNet.JsonPatch/Exceptions/JsonPatchException.cs index bca6d2b5a9..be412f2dfc 100644 --- a/src/Microsoft.AspNet.JsonPatch/Exceptions/JsonPatchException.cs +++ b/src/Microsoft.AspNet.JsonPatch/Exceptions/JsonPatchException.cs @@ -6,10 +6,10 @@ using Microsoft.AspNet.JsonPatch.Operations; namespace Microsoft.AspNet.JsonPatch.Exceptions { - public class JsonPatchException : JsonPatchException where T : class + public class JsonPatchException : JsonPatchException where TModel : class { - public Operation FailedOperation { get; private set; } - public new T AffectedObject { get; private set; } + public Operation FailedOperation { get; private set; } + public new TModel AffectedObject { get; private set; } private string _message = ""; public override string Message @@ -26,14 +26,14 @@ namespace Microsoft.AspNet.JsonPatch.Exceptions } - public JsonPatchException(JsonPatchError jsonPatchError) + public JsonPatchException(JsonPatchError jsonPatchError) { FailedOperation = jsonPatchError.Operation; _message = jsonPatchError.ErrorMessage; AffectedObject = jsonPatchError.AffectedObject; } - public JsonPatchException(JsonPatchError jsonPatchError, Exception innerException) + public JsonPatchException(JsonPatchError jsonPatchError, Exception innerException) : this(jsonPatchError) { InnerException = innerException; diff --git a/src/Microsoft.AspNet.JsonPatch/Helpers/ExpressionHelpers.cs b/src/Microsoft.AspNet.JsonPatch/Helpers/ExpressionHelpers.cs index 041ffbb928..4de728853f 100644 --- a/src/Microsoft.AspNet.JsonPatch/Helpers/ExpressionHelpers.cs +++ b/src/Microsoft.AspNet.JsonPatch/Helpers/ExpressionHelpers.cs @@ -9,7 +9,7 @@ namespace Microsoft.AspNet.JsonPatch.Helpers { internal static class ExpressionHelpers { - public static string GetPath(Expression> expr) where T : class + public static string GetPath(Expression> expr) where TModel : class { return "/" + GetPath(expr.Body, true); } diff --git a/src/Microsoft.AspNet.JsonPatch/TypedJsonPatchDocument.cs b/src/Microsoft.AspNet.JsonPatch/JsonPatchDocumentOfT.cs similarity index 53% rename from src/Microsoft.AspNet.JsonPatch/TypedJsonPatchDocument.cs rename to src/Microsoft.AspNet.JsonPatch/JsonPatchDocumentOfT.cs index 4bc1721862..24d9591104 100644 --- a/src/Microsoft.AspNet.JsonPatch/TypedJsonPatchDocument.cs +++ b/src/Microsoft.AspNet.JsonPatch/JsonPatchDocumentOfT.cs @@ -18,21 +18,21 @@ namespace Microsoft.AspNet.JsonPatch // including type data in the JsonPatchDocument serialized as JSON (to allow for correct deserialization) - that's // not according to RFC 6902, and would thus break cross-platform compatibility. [JsonConverter(typeof(TypedJsonPatchDocumentConverter))] - public class JsonPatchDocument : IJsonPatchDocument where T : class + public class JsonPatchDocument : IJsonPatchDocument where TModel : class { - public List> Operations { get; private set; } + public List> Operations { get; private set; } [JsonIgnore] public IContractResolver ContractResolver { get; set; } public JsonPatchDocument() { - Operations = new List>(); + Operations = new List>(); ContractResolver = new DefaultContractResolver(); } // Create from list of operations - public JsonPatchDocument(List> operations, IContractResolver contractResolver) + public JsonPatchDocument(List> operations, IContractResolver contractResolver) { Operations = operations; ContractResolver = contractResolver; @@ -46,13 +46,14 @@ namespace Microsoft.AspNet.JsonPatch /// path /// value /// - public JsonPatchDocument Add(Expression> path, TProp value) + public JsonPatchDocument Add(Expression> path, TProp value) { - Operations.Add(new Operation( + Operations.Add(new Operation( "add", - ExpressionHelpers.GetPath(path).ToLower(), + ExpressionHelpers.GetPath(path).ToLower(), from: null, value: value)); + return this; } @@ -64,12 +65,18 @@ namespace Microsoft.AspNet.JsonPatch /// value /// position /// - public JsonPatchDocument Add(Expression>> path, TProp value, int position) + public JsonPatchDocument Add( + Expression>> path, + TProp value, + int position) { - Operations.Add(new Operation( + Operations.Add(new Operation( "add", - ExpressionHelpers.GetPath>(path).ToLower() + "/" + position, - null, value)); + ExpressionHelpers.GetPath(path).ToLower() + "/" + position, + from: null, + value: value)); + return this; } @@ -80,9 +87,14 @@ namespace Microsoft.AspNet.JsonPatch /// path /// value /// - public JsonPatchDocument Add(Expression>> path, TProp value) + public JsonPatchDocument Add(Expression>> path, TProp value) { - Operations.Add(new Operation("add", ExpressionHelpers.GetPath>(path).ToLower() + "/-", null, value)); + Operations.Add(new Operation( + "add", + ExpressionHelpers.GetPath(path).ToLower() + "/-", + from: null, + value: value)); + return this; } @@ -93,9 +105,10 @@ namespace Microsoft.AspNet.JsonPatch /// /// /// - public JsonPatchDocument Remove(Expression> path) + public JsonPatchDocument Remove(Expression> path) { - Operations.Add(new Operation("remove", ExpressionHelpers.GetPath(path).ToLower(), null)); + Operations.Add(new Operation("remove", ExpressionHelpers.GetPath(path).ToLower(), from: null)); + return this; } @@ -106,9 +119,13 @@ namespace Microsoft.AspNet.JsonPatch /// target location /// position /// - public JsonPatchDocument Remove(Expression>> path, int position) + public JsonPatchDocument Remove(Expression>> path, int position) { - Operations.Add(new Operation("remove", ExpressionHelpers.GetPath>(path).ToLower() + "/" + position, null)); + Operations.Add(new Operation( + "remove", + ExpressionHelpers.GetPath(path).ToLower() + "/" + position, + from: null)); + return this; } @@ -118,9 +135,13 @@ namespace Microsoft.AspNet.JsonPatch /// value type /// target location /// - public JsonPatchDocument Remove(Expression>> path) + public JsonPatchDocument Remove(Expression>> path) { - Operations.Add(new Operation("remove", ExpressionHelpers.GetPath>(path).ToLower() + "/-", null)); + Operations.Add(new Operation( + "remove", + ExpressionHelpers.GetPath(path).ToLower() + "/-", + from: null)); + return this; } @@ -131,9 +152,14 @@ namespace Microsoft.AspNet.JsonPatch /// /// /// - public JsonPatchDocument Replace(Expression> path, TProp value) + public JsonPatchDocument Replace(Expression> path, TProp value) { - Operations.Add(new Operation("replace", ExpressionHelpers.GetPath(path).ToLower(), null, value)); + Operations.Add(new Operation( + "replace", + ExpressionHelpers.GetPath(path).ToLower(), + from: null, + value: value)); + return this; } @@ -144,9 +170,16 @@ namespace Microsoft.AspNet.JsonPatch /// target location /// position /// - public JsonPatchDocument Replace(Expression>> path, TProp value, int position) + public JsonPatchDocument Replace( + Expression>> path, + TProp value, int position) { - Operations.Add(new Operation("replace", ExpressionHelpers.GetPath>(path).ToLower() + "/" + position, null, value)); + Operations.Add(new Operation( + "replace", + ExpressionHelpers.GetPath(path).ToLower() + "/" + position, + from: null, + value: value)); + return this; } @@ -156,9 +189,14 @@ namespace Microsoft.AspNet.JsonPatch /// value type /// target location /// - public JsonPatchDocument Replace(Expression>> path, TProp value) + public JsonPatchDocument Replace(Expression>> path, TProp value) { - Operations.Add(new Operation("replace", ExpressionHelpers.GetPath>(path).ToLower() + "/-", null, value)); + Operations.Add(new Operation( + "replace", + ExpressionHelpers.GetPath(path).ToLower() + "/-", + from: null, + value: value)); + return this; } @@ -169,10 +207,15 @@ namespace Microsoft.AspNet.JsonPatch /// /// /// - public JsonPatchDocument Move(Expression> from, Expression> path) + public JsonPatchDocument Move( + Expression> from, + Expression> path) { - Operations.Add(new Operation("move", ExpressionHelpers.GetPath(path).ToLower() - , ExpressionHelpers.GetPath(from).ToLower())); + Operations.Add(new Operation( + "move", + ExpressionHelpers.GetPath(path).ToLower(), + ExpressionHelpers.GetPath(from).ToLower())); + return this; } @@ -184,10 +227,16 @@ namespace Microsoft.AspNet.JsonPatch /// /// /// - public JsonPatchDocument Move(Expression>> from, int positionFrom, Expression> path) + public JsonPatchDocument Move( + Expression>> from, + int positionFrom, + Expression> path) { - Operations.Add(new Operation("move", ExpressionHelpers.GetPath(path).ToLower() - , ExpressionHelpers.GetPath>(from).ToLower() + "/" + positionFrom)); + Operations.Add(new Operation( + "move", + ExpressionHelpers.GetPath(path).ToLower(), + ExpressionHelpers.GetPath(from).ToLower() + "/" + positionFrom)); + return this; } @@ -199,12 +248,16 @@ namespace Microsoft.AspNet.JsonPatch /// /// /// - public JsonPatchDocument Move(Expression> from, - Expression>> path, int positionTo) + public JsonPatchDocument Move( + Expression> from, + Expression>> path, + int positionTo) { - Operations.Add(new Operation("move", ExpressionHelpers.GetPath>(path).ToLower() - + "/" + positionTo - , ExpressionHelpers.GetPath(from).ToLower())); + Operations.Add(new Operation( + "move", + ExpressionHelpers.GetPath(path).ToLower() + "/" + positionTo, + ExpressionHelpers.GetPath(from).ToLower())); + return this; } @@ -216,12 +269,17 @@ namespace Microsoft.AspNet.JsonPatch /// /// /// - public JsonPatchDocument Move(Expression>> from, int positionFrom, - Expression>> path, int positionTo) + public JsonPatchDocument Move( + Expression>> from, + int positionFrom, + Expression>> path, + int positionTo) { - Operations.Add(new Operation("move", ExpressionHelpers.GetPath>(path).ToLower() - + "/" + positionTo - , ExpressionHelpers.GetPath>(from).ToLower() + "/" + positionFrom)); + Operations.Add(new Operation( + "move", + ExpressionHelpers.GetPath(path).ToLower() + "/" + positionTo, + ExpressionHelpers.GetPath(from).ToLower() + "/" + positionFrom)); + return this; } @@ -233,12 +291,16 @@ namespace Microsoft.AspNet.JsonPatch /// /// /// - public JsonPatchDocument Move(Expression>> from, int positionFrom, - Expression>> path) + public JsonPatchDocument Move( + Expression>> from, + int positionFrom, + Expression>> path) { - Operations.Add(new Operation("move", ExpressionHelpers.GetPath>(path).ToLower() - + "/-" - , ExpressionHelpers.GetPath>(from).ToLower() + "/" + positionFrom)); + Operations.Add(new Operation( + "move", + ExpressionHelpers.GetPath(path).ToLower() + "/-", + ExpressionHelpers.GetPath(from).ToLower() + "/" + positionFrom)); + return this; } @@ -250,10 +312,15 @@ namespace Microsoft.AspNet.JsonPatch /// /// /// - public JsonPatchDocument Move(Expression> from, Expression>> path) + public JsonPatchDocument Move( + Expression> from, + Expression>> path) { - Operations.Add(new Operation("move", ExpressionHelpers.GetPath>(path).ToLower() + "/-" - , ExpressionHelpers.GetPath(from).ToLower())); + Operations.Add(new Operation( + "move", + ExpressionHelpers.GetPath(path).ToLower() + "/-", + ExpressionHelpers.GetPath(from).ToLower())); + return this; } @@ -264,10 +331,15 @@ namespace Microsoft.AspNet.JsonPatch /// /// /// - public JsonPatchDocument Copy(Expression> from, Expression> path) + public JsonPatchDocument Copy( + Expression> from, + Expression> path) { - Operations.Add(new Operation("copy", ExpressionHelpers.GetPath(path).ToLower() - , ExpressionHelpers.GetPath(from).ToLower())); + Operations.Add(new Operation( + "copy", + ExpressionHelpers.GetPath(path).ToLower() + , ExpressionHelpers.GetPath(from).ToLower())); + return this; } @@ -279,10 +351,16 @@ namespace Microsoft.AspNet.JsonPatch /// /// /// - public JsonPatchDocument Copy(Expression>> from, int positionFrom, Expression> path) + public JsonPatchDocument Copy( + Expression>> from, + int positionFrom, + Expression> path) { - Operations.Add(new Operation("copy", ExpressionHelpers.GetPath(path).ToLower() - , ExpressionHelpers.GetPath>(from).ToLower() + "/" + positionFrom)); + Operations.Add(new Operation( + "copy", + ExpressionHelpers.GetPath(path).ToLower(), + ExpressionHelpers.GetPath(from).ToLower() + "/" + positionFrom)); + return this; } @@ -294,12 +372,16 @@ namespace Microsoft.AspNet.JsonPatch /// /// /// - public JsonPatchDocument Copy(Expression> from, - Expression>> path, int positionTo) + public JsonPatchDocument Copy( + Expression> from, + Expression>> path, + int positionTo) { - Operations.Add(new Operation("copy", ExpressionHelpers.GetPath>(path).ToLower() - + "/" + positionTo - , ExpressionHelpers.GetPath(from).ToLower())); + Operations.Add(new Operation( + "copy", + ExpressionHelpers.GetPath(path).ToLower() + "/" + positionTo, + ExpressionHelpers.GetPath(from).ToLower())); + return this; } @@ -311,12 +393,17 @@ namespace Microsoft.AspNet.JsonPatch /// /// /// - public JsonPatchDocument Copy(Expression>> from, int positionFrom, - Expression>> path, int positionTo) + public JsonPatchDocument Copy( + Expression>> from, + int positionFrom, + Expression>> path, + int positionTo) { - Operations.Add(new Operation("copy", ExpressionHelpers.GetPath>(path).ToLower() - + "/" + positionTo - , ExpressionHelpers.GetPath>(from).ToLower() + "/" + positionFrom)); + Operations.Add(new Operation( + "copy", + ExpressionHelpers.GetPath(path).ToLower() + "/" + positionTo, + ExpressionHelpers.GetPath(from).ToLower() + "/" + positionFrom)); + return this; } @@ -328,12 +415,17 @@ namespace Microsoft.AspNet.JsonPatch /// /// /// - public JsonPatchDocument Copy(Expression>> from, int positionFrom, - Expression>> path) + public JsonPatchDocument Copy( + Expression>> from, + int positionFrom, + Expression>> path) { - Operations.Add(new Operation("copy", ExpressionHelpers.GetPath>(path).ToLower() - + "/-" - , ExpressionHelpers.GetPath>(from).ToLower() + "/" + positionFrom)); + Operations.Add(new Operation( + "copy", + ExpressionHelpers.GetPath(path).ToLower() + "/-", + ExpressionHelpers.GetPath(from).ToLower() + "/" + positionFrom)); + return this; } @@ -345,24 +437,29 @@ namespace Microsoft.AspNet.JsonPatch /// /// /// - public JsonPatchDocument Copy(Expression> from, Expression>> path) + public JsonPatchDocument Copy( + Expression> from, + Expression>> path) { - Operations.Add(new Operation("copy", ExpressionHelpers.GetPath>(path).ToLower() + "/-" - , ExpressionHelpers.GetPath(from).ToLower())); + Operations.Add(new Operation( + "copy", + ExpressionHelpers.GetPath(path).ToLower() + "/-", + ExpressionHelpers.GetPath(from).ToLower())); + return this; } - public void ApplyTo(T objectToApplyTo) + public void ApplyTo(TModel objectToApplyTo) { - ApplyTo(objectToApplyTo, new ObjectAdapter(ContractResolver, logErrorAction: null)); + ApplyTo(objectToApplyTo, new ObjectAdapter(ContractResolver, logErrorAction: null)); } - public void ApplyTo(T objectToApplyTo, Action> logErrorAction) + public void ApplyTo(TModel objectToApplyTo, Action> logErrorAction) { - ApplyTo(objectToApplyTo, new ObjectAdapter(ContractResolver, logErrorAction)); + ApplyTo(objectToApplyTo, new ObjectAdapter(ContractResolver, logErrorAction)); } - public void ApplyTo(T objectToApplyTo, IObjectAdapter adapter) + public void ApplyTo(TModel objectToApplyTo, IObjectAdapter adapter) { // apply each operation in order foreach (var op in Operations) diff --git a/src/Microsoft.AspNet.JsonPatch/JsonPatchError.cs b/src/Microsoft.AspNet.JsonPatch/JsonPatchError.cs index a441abda55..9b45f86615 100644 --- a/src/Microsoft.AspNet.JsonPatch/JsonPatchError.cs +++ b/src/Microsoft.AspNet.JsonPatch/JsonPatchError.cs @@ -9,17 +9,17 @@ namespace Microsoft.AspNet.JsonPatch /// /// Captures error message and the related entity and the operation that caused it. /// - public class JsonPatchError where T : class + public class JsonPatchError where TModel : class { /// - /// Initializes a new instance of . + /// Initializes a new instance of . /// /// The object that is affected by the error. - /// The that caused the error. + /// The that caused the error. /// The error message. public JsonPatchError( - [NotNull] T affectedObject, - [NotNull] Operation operation, + [NotNull] TModel affectedObject, + [NotNull] Operation operation, [NotNull] string errorMessage) { AffectedObject = affectedObject; @@ -30,12 +30,12 @@ namespace Microsoft.AspNet.JsonPatch /// /// Gets the object that is affected by the error. /// - public T AffectedObject { get; } + public TModel AffectedObject { get; } /// - /// Gets the that caused the error. + /// Gets the that caused the error. /// - public Operation Operation { get; } + public Operation Operation { get; } /// /// Gets the error message. diff --git a/src/Microsoft.AspNet.JsonPatch/Operations/TypedOperation.cs b/src/Microsoft.AspNet.JsonPatch/Operations/OperationOfT.cs similarity index 86% rename from src/Microsoft.AspNet.JsonPatch/Operations/TypedOperation.cs rename to src/Microsoft.AspNet.JsonPatch/Operations/OperationOfT.cs index de315b99de..34c1aec1dc 100644 --- a/src/Microsoft.AspNet.JsonPatch/Operations/TypedOperation.cs +++ b/src/Microsoft.AspNet.JsonPatch/Operations/OperationOfT.cs @@ -1,11 +1,12 @@ // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using Microsoft.AspNet.JsonPatch.Adapters; namespace Microsoft.AspNet.JsonPatch.Operations { - public class Operation : Operation where T : class + public class Operation : Operation where TModel : class { public Operation() { @@ -24,7 +25,7 @@ namespace Microsoft.AspNet.JsonPatch.Operations } - public void Apply(T objectToApplyTo, IObjectAdapter adapter) + public void Apply(TModel objectToApplyTo, IObjectAdapter adapter) { switch (OperationType) { @@ -44,8 +45,7 @@ namespace Microsoft.AspNet.JsonPatch.Operations adapter.Copy(this, objectToApplyTo); break; case OperationType.Test: - adapter.Test(this, objectToApplyTo); - break; + throw new NotSupportedException(Resources.TestOperationNotSupported); default: break; } diff --git a/src/Microsoft.AspNet.JsonPatch/Properties/Resources.Designer.cs b/src/Microsoft.AspNet.JsonPatch/Properties/Resources.Designer.cs index a2ce237bc4..d9f8a7ee41 100644 --- a/src/Microsoft.AspNet.JsonPatch/Properties/Resources.Designer.cs +++ b/src/Microsoft.AspNet.JsonPatch/Properties/Resources.Designer.cs @@ -106,6 +106,22 @@ namespace Microsoft.AspNet.JsonPatch return string.Format(CultureInfo.CurrentCulture, GetString("PropertyDoesNotExist"), p0); } + /// + /// The test operation is not supported. + /// + internal static string TestOperationNotSupported + { + get { return GetString("TestOperationNotSupported"); } + } + + /// + /// The test operation is not supported. + /// + internal static string FormatTestOperationNotSupported() + { + return GetString("TestOperationNotSupported"); + } + private static string GetString(string name, params string[] formatterNames) { var value = _resourceManager.GetString(name); diff --git a/src/Microsoft.AspNet.JsonPatch/Resources.resx b/src/Microsoft.AspNet.JsonPatch/Resources.resx index e41ac8c272..f6dedfd5f7 100644 --- a/src/Microsoft.AspNet.JsonPatch/Resources.resx +++ b/src/Microsoft.AspNet.JsonPatch/Resources.resx @@ -135,4 +135,7 @@ Property does not exist at path '{0}'. + + The test operation is not supported. + \ No newline at end of file