diff --git a/src/Microsoft.AspNetCore.JsonPatch/Operations/OperationBase.cs b/src/Microsoft.AspNetCore.JsonPatch/Operations/OperationBase.cs index eb35fa7e9d..e629e2308d 100644 --- a/src/Microsoft.AspNetCore.JsonPatch/Operations/OperationBase.cs +++ b/src/Microsoft.AspNetCore.JsonPatch/Operations/OperationBase.cs @@ -2,26 +2,21 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using Microsoft.AspNetCore.JsonPatch.Exceptions; using Newtonsoft.Json; namespace Microsoft.AspNetCore.JsonPatch.Operations { public class OperationBase { + private string _op; + private OperationType _operationType; + [JsonIgnore] public OperationType OperationType { get { - OperationType result; - if (!Enum.TryParse(op, ignoreCase: true, result: out result)) - { - throw new JsonPatchException( - Resources.FormatInvalidJsonPatchOperation(op), - innerException: null); - } - return result; + return _operationType; } } @@ -29,7 +24,23 @@ namespace Microsoft.AspNetCore.JsonPatch.Operations public string path { get; set; } [JsonProperty("op")] - public string op { get; set; } + public string op + { + get + { + return _op; + } + set + { + OperationType result; + if (!Enum.TryParse(value, ignoreCase: true, result: out result)) + { + result = OperationType.Invalid; + } + _operationType = result; + _op = value; + } + } [JsonProperty("from")] public string from { get; set; } diff --git a/src/Microsoft.AspNetCore.JsonPatch/Operations/OperationOfT.cs b/src/Microsoft.AspNetCore.JsonPatch/Operations/OperationOfT.cs index 5af4f5d3f9..7189cf8210 100644 --- a/src/Microsoft.AspNetCore.JsonPatch/Operations/OperationOfT.cs +++ b/src/Microsoft.AspNetCore.JsonPatch/Operations/OperationOfT.cs @@ -75,6 +75,9 @@ namespace Microsoft.AspNetCore.JsonPatch.Operations break; case OperationType.Test: throw new JsonPatchException(new JsonPatchError(objectToApplyTo, this, Resources.TestOperationNotSupported)); + case OperationType.Invalid: + throw new JsonPatchException( + Resources.FormatInvalidJsonPatchOperation(op), innerException: null); default: break; } diff --git a/src/Microsoft.AspNetCore.JsonPatch/Operations/OperationType.cs b/src/Microsoft.AspNetCore.JsonPatch/Operations/OperationType.cs index 846b07019f..725646df3a 100644 --- a/src/Microsoft.AspNetCore.JsonPatch/Operations/OperationType.cs +++ b/src/Microsoft.AspNetCore.JsonPatch/Operations/OperationType.cs @@ -10,6 +10,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Operations Replace, Move, Copy, - Test + Test, + Invalid } } \ No newline at end of file diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/OperationBaseTests.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/OperationBaseTests.cs new file mode 100644 index 0000000000..955344404f --- /dev/null +++ b/test/Microsoft.AspNetCore.JsonPatch.Test/OperationBaseTests.cs @@ -0,0 +1,41 @@ +// 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 Xunit; + +namespace Microsoft.AspNetCore.JsonPatch.Operations +{ + public class OperationBaseTests + { + [Theory] + [InlineData("ADd", OperationType.Add)] + [InlineData("Copy", OperationType.Copy)] + [InlineData("mOVE", OperationType.Move)] + [InlineData("REMOVE", OperationType.Remove)] + [InlineData("replace", OperationType.Replace)] + [InlineData("TeSt", OperationType.Test)] + public void SetValidOperationType(string op, OperationType operationType) + { + // Arrange + var operationBase = new OperationBase(); + operationBase.op = op; + + // Act & Assert + Assert.Equal(operationType, operationBase.OperationType); + } + + [Theory] + [InlineData("invalid", OperationType.Invalid)] + [InlineData("coppy", OperationType.Invalid)] + [InlineData("notvalid", OperationType.Invalid)] + public void InvalidOperationType_SetsOperationTypeInvalid(string op, OperationType operationType) + { + // Arrange + var operationBase = new OperationBase(); + operationBase.op = op; + + // Act & Assert + Assert.Equal(operationType, operationBase.OperationType); + } + } +}