From abeda203c087d152495efcfbda3a72780ffd2b4b Mon Sep 17 00:00:00 2001 From: Jonathan Rajotte Date: Wed, 24 Mar 2021 03:07:17 -0400 Subject: [PATCH] Fixed issue with slashes in the key of a dictionary (#46) * Fixed issue with slashes in the key of a dictionary * Changed location of unit test --- src/2.1-JsonMergePatch/Builders/PatchBuilder.cs | 4 +++- .../Builders/PatchBuilder.cs | 4 +++- .../NewtonsoftJson/Builders/Json/Simple.cs | 14 ++++++++++++++ .../NewtonsoftJson/Patching/PatchTest.cs | 1 + 4 files changed, 21 insertions(+), 2 deletions(-) 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