Skip to content

Commit c816bce

Browse files
authored
Refactor invalid operation exception handling (#87)
Addresses part of #80
1 parent a8b6fa7 commit c816bce

File tree

4 files changed

+67
-11
lines changed

4 files changed

+67
-11
lines changed

src/Microsoft.AspNetCore.JsonPatch/Operations/OperationBase.cs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,45 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using Microsoft.AspNetCore.JsonPatch.Exceptions;
65
using Newtonsoft.Json;
76

87
namespace Microsoft.AspNetCore.JsonPatch.Operations
98
{
109
public class OperationBase
1110
{
11+
private string _op;
12+
private OperationType _operationType;
13+
1214
[JsonIgnore]
1315
public OperationType OperationType
1416
{
1517
get
1618
{
17-
OperationType result;
18-
if (!Enum.TryParse(op, ignoreCase: true, result: out result))
19-
{
20-
throw new JsonPatchException(
21-
Resources.FormatInvalidJsonPatchOperation(op),
22-
innerException: null);
23-
}
24-
return result;
19+
return _operationType;
2520
}
2621
}
2722

2823
[JsonProperty("path")]
2924
public string path { get; set; }
3025

3126
[JsonProperty("op")]
32-
public string op { get; set; }
27+
public string op
28+
{
29+
get
30+
{
31+
return _op;
32+
}
33+
set
34+
{
35+
OperationType result;
36+
if (!Enum.TryParse(value, ignoreCase: true, result: out result))
37+
{
38+
result = OperationType.Invalid;
39+
}
40+
_operationType = result;
41+
_op = value;
42+
}
43+
}
3344

3445
[JsonProperty("from")]
3546
public string from { get; set; }

src/Microsoft.AspNetCore.JsonPatch/Operations/OperationOfT.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ public void Apply(TModel objectToApplyTo, IObjectAdapter adapter)
7575
break;
7676
case OperationType.Test:
7777
throw new JsonPatchException(new JsonPatchError(objectToApplyTo, this, Resources.TestOperationNotSupported));
78+
case OperationType.Invalid:
79+
throw new JsonPatchException(
80+
Resources.FormatInvalidJsonPatchOperation(op), innerException: null);
7881
default:
7982
break;
8083
}

src/Microsoft.AspNetCore.JsonPatch/Operations/OperationType.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public enum OperationType
1010
Replace,
1111
Move,
1212
Copy,
13-
Test
13+
Test,
14+
Invalid
1415
}
1516
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using Xunit;
5+
6+
namespace Microsoft.AspNetCore.JsonPatch.Operations
7+
{
8+
public class OperationBaseTests
9+
{
10+
[Theory]
11+
[InlineData("ADd", OperationType.Add)]
12+
[InlineData("Copy", OperationType.Copy)]
13+
[InlineData("mOVE", OperationType.Move)]
14+
[InlineData("REMOVE", OperationType.Remove)]
15+
[InlineData("replace", OperationType.Replace)]
16+
[InlineData("TeSt", OperationType.Test)]
17+
public void SetValidOperationType(string op, OperationType operationType)
18+
{
19+
// Arrange
20+
var operationBase = new OperationBase();
21+
operationBase.op = op;
22+
23+
// Act & Assert
24+
Assert.Equal(operationType, operationBase.OperationType);
25+
}
26+
27+
[Theory]
28+
[InlineData("invalid", OperationType.Invalid)]
29+
[InlineData("coppy", OperationType.Invalid)]
30+
[InlineData("notvalid", OperationType.Invalid)]
31+
public void InvalidOperationType_SetsOperationTypeInvalid(string op, OperationType operationType)
32+
{
33+
// Arrange
34+
var operationBase = new OperationBase();
35+
operationBase.op = op;
36+
37+
// Act & Assert
38+
Assert.Equal(operationType, operationBase.OperationType);
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)