Skip to content

Commit

Permalink
Fixed issue with slashes in the key of a dictionary (#46)
Browse files Browse the repository at this point in the history
* Fixed issue with slashes in the key of a dictionary

* Changed location of unit test
  • Loading branch information
joerage authored Mar 24, 2021
1 parent c1b2549 commit abeda20
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/2.1-JsonMergePatch/Builders/PatchBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<string, object> Dic { get; set; } = new Dictionary<string, object>();
}

private readonly PatchBuilder<SimpleClass> builder = new PatchBuilder<SimpleClass>();
Expand All @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Morcatko.AspNetCore.JsonMergePatch.NewtonsoftJson.Builders;
using System.Linq;
using Xunit;

namespace Morcatko.AspNetCore.JsonMergePatch.Tests.NewtonsoftJson.Patching
Expand Down

0 comments on commit abeda20

Please sign in to comment.