diff --git a/src/2.1-JsonMergePatch/Builders/PatchBuilder.cs b/src/2.1-JsonMergePatch/Builders/PatchBuilder.cs index 9c134d2..d5b334e 100644 --- a/src/2.1-JsonMergePatch/Builders/PatchBuilder.cs +++ b/src/2.1-JsonMergePatch/Builders/PatchBuilder.cs @@ -41,7 +41,9 @@ private static void AddOperation(JsonMergePatchDocument jsonMergePatchDocument, { foreach (var jProperty in patchObject) { - var path = pathPrefix + jProperty.Key; + // Encode any possible "/" in the path. Ref: https://tools.ietf.org/html/rfc6901#section-3 + var path = pathPrefix + jProperty.Key.Replace("/", "~1"); + if (jProperty.Value is JValue jValue) { if (options.EnableDelete && jValue.Value == null) diff --git a/src/3.0-JsonMergePatch.NewtonsoftJson/Builders/PatchBuilder.cs b/src/3.0-JsonMergePatch.NewtonsoftJson/Builders/PatchBuilder.cs index 40fc0b7..59a04a8 100644 --- a/src/3.0-JsonMergePatch.NewtonsoftJson/Builders/PatchBuilder.cs +++ b/src/3.0-JsonMergePatch.NewtonsoftJson/Builders/PatchBuilder.cs @@ -44,7 +44,9 @@ private static void AddOperation(IInternalJsonMergePatchDocument jsonMergePatchD { foreach (var jProperty in patchObject) { - var path = pathPrefix + jProperty.Key; + // Encode any possible "/" in the path. Ref: https://tools.ietf.org/html/rfc6901#section-3 + var path = pathPrefix + jProperty.Key.Replace("/", "~1"); + if (jProperty.Value is JValue jValue) { if (options.EnableDelete && jValue.Value == null) diff --git a/src/3.0-JsonMergePatch.Tests/NewtonsoftJson/Builders/Json/Simple.cs b/src/3.0-JsonMergePatch.Tests/NewtonsoftJson/Builders/Json/Simple.cs index 118cb39..a65a871 100644 --- a/src/3.0-JsonMergePatch.Tests/NewtonsoftJson/Builders/Json/Simple.cs +++ b/src/3.0-JsonMergePatch.Tests/NewtonsoftJson/Builders/Json/Simple.cs @@ -2,6 +2,8 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; +using System.Collections.Generic; +using System.Linq; using Xunit; namespace Morcatko.AspNetCore.JsonMergePatch.Tests.NewtonsoftJson.Builders.Json @@ -14,6 +16,7 @@ class SimpleClass public int Integer { get; set; } = 1; public string String { get; set; } = "abc"; public DateTimeOffset Date { get; set; } = new DateTimeOffset(2019, 10, 29, 9, 38, 0, 0, TimeSpan.FromHours(2)); + public Dictionary Dic { get; set; } = new Dictionary(); } private readonly PatchBuilder builder = new PatchBuilder(); @@ -29,6 +32,17 @@ public void String() Assert.Equal(3, result.Integer); } + [Fact] + public void DictionaryAddKeyWithSlash() + { + var original = new SimpleClass(); + + var patch = builder.Build("{ \"Dic\": { \"key/1\": {} }"); + var result = patch.ApplyTo(original); + + Assert.Equal("key/1", result.Dic.First().Key); + } + [Fact] public void StringForDateTimeOffset() { diff --git a/src/3.0-JsonMergePatch.Tests/NewtonsoftJson/Patching/PatchTest.cs b/src/3.0-JsonMergePatch.Tests/NewtonsoftJson/Patching/PatchTest.cs index dfccb02..c9c7c38 100644 --- a/src/3.0-JsonMergePatch.Tests/NewtonsoftJson/Patching/PatchTest.cs +++ b/src/3.0-JsonMergePatch.Tests/NewtonsoftJson/Patching/PatchTest.cs @@ -1,4 +1,5 @@ using Morcatko.AspNetCore.JsonMergePatch.NewtonsoftJson.Builders; +using System.Linq; using Xunit; namespace Morcatko.AspNetCore.JsonMergePatch.Tests.NewtonsoftJson.Patching