Skip to content
This repository was archived by the owner on Nov 15, 2018. It is now read-only.

Commit 3b48590

Browse files
committed
1 parent aeb5c57 commit 3b48590

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

src/Microsoft.AspNetCore.JsonPatch/Internal/ObjectVisitor.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
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 System.Collections;
65
using Microsoft.AspNetCore.JsonPatch.Adapters;
76
using Newtonsoft.Json.Serialization;
87

@@ -56,6 +55,13 @@ public bool TryVisit(ref object target, out IAdapter adapter, out string errorMe
5655
adapter = null;
5756
return false;
5857
}
58+
59+
// If we hit a null on an interior segment then we need to stop traversing.
60+
if (next == null)
61+
{
62+
adapter = null;
63+
return false;
64+
}
5965

6066
target = next;
6167
adapter = SelectAdapter(target);

test/Microsoft.AspNetCore.JsonPatch.Test/IntegrationTests/SimpleObjectIntegrationTest.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
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 System.Dynamic;
6+
using Microsoft.AspNetCore.JsonPatch.Exceptions;
57
using Xunit;
68

79
namespace Microsoft.AspNetCore.JsonPatch.IntegrationTests
@@ -124,5 +126,30 @@ public void AddReplacesGuid()
124126
Assert.Equal(newGuid, targetObject.GuidValue);
125127
}
126128

129+
// https://github.com/aspnet/AspNetCore/issues/3634
130+
[Fact]
131+
public void Regression_AspNetCore3634()
132+
{
133+
// Assert
134+
var document = new JsonPatchDocument();
135+
document.Move("/Object", "/Object/goodbye");
136+
137+
dynamic @object = new ExpandoObject();
138+
@object.hello = "world";
139+
140+
var target = new Regression_AspNetCore3634_Object();
141+
target.Object = @object;
142+
143+
// Act
144+
var ex = Assert.Throws<JsonPatchException>(() => document.ApplyTo(target));
145+
146+
// Assert
147+
Assert.Equal("For operation 'move', the target location specified by path '/Object/goodbye' was not found.", ex.Message);
148+
}
149+
150+
private class Regression_AspNetCore3634_Object
151+
{
152+
public dynamic Object { get; set; }
153+
}
127154
}
128155
}

test/Microsoft.AspNetCore.JsonPatch.Test/Internal/ObjectVisitorTest.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,22 @@ public void Visit_DoesNotValidate_FinalPathSegment()
205205
Assert.IsType<PocoAdapter>(adapter);
206206
}
207207

208+
[Fact]
209+
public void Visit_NullInteriorTarget_ReturnsFalse()
210+
{
211+
// Arrange
212+
var visitor = new ObjectVisitor(new ParsedPath("/States/0"), new DefaultContractResolver());
213+
214+
// Act
215+
object target = new Class1() { States = null, };
216+
var visitStatus = visitor.TryVisit(ref target, out var adapter, out var message);
217+
218+
// Assert
219+
Assert.False(visitStatus);
220+
Assert.Null(adapter);
221+
Assert.Null(message);
222+
}
223+
208224
[Fact]
209225
public void Visit_NullTarget_ReturnsNullAdapter()
210226
{

0 commit comments

Comments
 (0)