Skip to content

Commit b9f7895

Browse files
SeeSharp7SeeSharp7
authored andcommitted
Iterate to Array for JsonPatch
1 parent 751e7d3 commit b9f7895

16 files changed

+32
-12
lines changed

src/JsonPatch/JsonPatch.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,21 @@ namespace SeeSharp7.Patch4Net.JsonPatch
44
{
55
internal class JsonPatch
66
{
7-
internal JObject Patch(JObject jsonPatchModel, JObject originalModel)
7+
internal JObject Patch(JArray jsonPatchModel, JObject originalModel)
88
{
99
foreach (var token in jsonPatchModel)
1010
{
11-
ApplyPatchOperation(token.Value, originalModel);
11+
var operation = token["op"].Value<string>();
12+
var path = token["path"].Value<string>();
13+
var value = token["value"].Value<string>();
14+
15+
ApplyPatchOperation(originalModel, operation, path, value);
1216
}
1317

1418
return originalModel;
1519
}
1620

17-
private void ApplyPatchOperation(JToken token, JObject originalModel)
21+
private void ApplyPatchOperation(JObject originalModel, string operation, string path, string value)
1822
{
1923

2024
}

src/JsonPatcher.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,13 @@ public TModel UniversalPatch<TModel>(string requestBody, TModel originalModel, s
4848
{
4949
return Patch(requestBody, originalModel);
5050
}
51+
5152
if (string.Compare(contentTypeHeaderValue, ContentTypeMergePatch, StringComparison.InvariantCultureIgnoreCase) == 0)
5253
{
5354
return MergePatch(requestBody, originalModel);
5455
}
5556

56-
throw new UnknownContentTypeException($"The value {contentTypeHeaderValue} of ContentType Header (specified in parameter {nameof(contentTypeHeaderValue)}) is unknown");
57+
throw new UnknownContentTypeException($"Unknown value {contentTypeHeaderValue} of parameter {nameof(contentTypeHeaderValue)}");
5758
}
5859

5960
/// <summary>
@@ -84,9 +85,10 @@ public TModel MergePatch<TModel>(string mergePatchRequestBody, TModel originalMo
8485
public TModel Patch<TModel>(string patchRequestBody, TModel originalModel)
8586
{
8687
var jParsedOriginalModel = CloneToJObject(originalModel);
87-
var jParsedRequestBody = JObject.Parse(patchRequestBody);
88-
89-
//do some cool patch stuff here
88+
var jParsedRequestBody = JArray.Parse(patchRequestBody);
89+
90+
var patch = new JsonPatch.JsonPatch();
91+
patch.Patch(jParsedRequestBody, jParsedOriginalModel);
9092

9193
return _serializer.DeserializeObject<TModel>(jParsedOriginalModel.ToString());
9294
}

src/bin/Debug/Patch4Net.dll

512 Bytes
Binary file not shown.

src/bin/Debug/Patch4Net.pdb

2 KB
Binary file not shown.

src/obj/Debug/Patch4Net.csproj.FileListAbsolute.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,3 @@ C:\Work\GitHub\SeeSharp7\patch4net\src\bin\Debug\Newtonsoft.Json.dll
1111
C:\Work\GitHub\SeeSharp7\patch4net\src\bin\Debug\Newtonsoft.Json.xml
1212
C:\Work\GitHub\SeeSharp7\patch4net\src\obj\Debug\Patch4Net.dll
1313
C:\Work\GitHub\SeeSharp7\patch4net\src\obj\Debug\Patch4Net.pdb
14-
C:\Work\GitHub\SeeSharp7\patch4net\src\obj\Debug\Patch4Net.csprojResolveAssemblyReference.cache
-10.9 KB
Binary file not shown.

src/obj/Debug/Patch4Net.dll

512 Bytes
Binary file not shown.

src/obj/Debug/Patch4Net.pdb

2 KB
Binary file not shown.

test/Patch4Net.Tests/JsonPatcherTests.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,25 @@ public class PatchTests : JsonPatcherTests
1313
[TestMethod]
1414
public void ItShouldPatchTheSimpleModelCorrectly()
1515
{
16-
//var incomingContentTypeHeaderValue = "xxx";
17-
18-
//var jsonPatcher = new JsonPatcher();
19-
//var patchedModel = jsonPatcher.UniversalPatch(requestBody, originalModel, incomingContentTypeHeaderValue);
16+
//arrange
17+
const int age = 26;
18+
const string oldName = "Sarah Mayer";
19+
const decimal oldAccountBalance = 30.5m;
20+
21+
var simpleModel = new SimpleCustomerModel
22+
{
23+
Name = oldName,
24+
Age = age,
25+
AccountBalance = oldAccountBalance
26+
};
27+
28+
var patchJson = "[\r\n { \"op\": \"replace\", \"path\": \"/baz\", \"value\": \"boo\" },\r\n { \"op\": \"add\", \"path\": \"/hello\", \"value\": [\"world\"] },\r\n { \"op\": \"remove\", \"path\": \"/foo\"}\r\n]";
29+
30+
//act
31+
var jsonPatcher = new JsonPatcher();
32+
var patchedModel = jsonPatcher.Patch(patchJson, simpleModel);
33+
34+
//assert
2035
}
2136
}
2237

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)