Allow whitespace and backslash in path

This commit is contained in:
Ajay Bhargav Baaskaran 2018-02-27 12:41:38 -08:00
parent a1b6c4c7aa
commit 63f0322810
3 changed files with 36 additions and 9 deletions

View File

@ -2,23 +2,24 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNetCore.JsonPatch.Exceptions; using Microsoft.AspNetCore.JsonPatch.Exceptions;
using System;
namespace Microsoft.AspNetCore.JsonPatch.Internal namespace Microsoft.AspNetCore.JsonPatch.Internal
{ {
internal static class PathHelpers internal static class PathHelpers
{ {
internal static string NormalizePath(string path) internal static string ValidateAndNormalizePath(string path)
{ {
// check for most common path errors on create. This is not // check for most common path errors on create. This is not
// absolutely necessary, but it allows us to already catch mistakes // absolutely necessary, but it allows us to already catch mistakes
// on creation of the patch document rather than on execute. // on creation of the patch document rather than on execute.
if (path.Contains("//") || path.Contains(" ") || path.Contains("\\")) if (path.Contains("//"))
{ {
throw new JsonPatchException(Resources.FormatInvalidValueForPath(path), null); throw new JsonPatchException(Resources.FormatInvalidValueForPath(path), null);
} }
if (!(path.StartsWith("/"))) if (!path.StartsWith("/", StringComparison.Ordinal))
{ {
return "/" + path; return "/" + path;
} }

View File

@ -60,7 +60,7 @@ namespace Microsoft.AspNetCore.JsonPatch
throw new ArgumentNullException(nameof(path)); throw new ArgumentNullException(nameof(path));
} }
Operations.Add(new Operation("add", PathHelpers.NormalizePath(path), null, value)); Operations.Add(new Operation("add", PathHelpers.ValidateAndNormalizePath(path), null, value));
return this; return this;
} }
@ -77,7 +77,7 @@ namespace Microsoft.AspNetCore.JsonPatch
throw new ArgumentNullException(nameof(path)); throw new ArgumentNullException(nameof(path));
} }
Operations.Add(new Operation("remove", PathHelpers.NormalizePath(path), null, null)); Operations.Add(new Operation("remove", PathHelpers.ValidateAndNormalizePath(path), null, null));
return this; return this;
} }
@ -95,7 +95,7 @@ namespace Microsoft.AspNetCore.JsonPatch
throw new ArgumentNullException(nameof(path)); throw new ArgumentNullException(nameof(path));
} }
Operations.Add(new Operation("replace", PathHelpers.NormalizePath(path), null, value)); Operations.Add(new Operation("replace", PathHelpers.ValidateAndNormalizePath(path), null, value));
return this; return this;
} }
@ -113,7 +113,7 @@ namespace Microsoft.AspNetCore.JsonPatch
throw new ArgumentNullException(nameof(path)); throw new ArgumentNullException(nameof(path));
} }
Operations.Add(new Operation("test", PathHelpers.NormalizePath(path), null, value)); Operations.Add(new Operation("test", PathHelpers.ValidateAndNormalizePath(path), null, value));
return this; return this;
} }
@ -136,7 +136,7 @@ namespace Microsoft.AspNetCore.JsonPatch
throw new ArgumentNullException(nameof(path)); throw new ArgumentNullException(nameof(path));
} }
Operations.Add(new Operation("move", PathHelpers.NormalizePath(path), PathHelpers.NormalizePath(from))); Operations.Add(new Operation("move", PathHelpers.ValidateAndNormalizePath(path), PathHelpers.ValidateAndNormalizePath(from)));
return this; return this;
} }
@ -159,7 +159,7 @@ namespace Microsoft.AspNetCore.JsonPatch
throw new ArgumentNullException(nameof(path)); throw new ArgumentNullException(nameof(path));
} }
Operations.Add(new Operation("copy", PathHelpers.NormalizePath(path), PathHelpers.NormalizePath(from))); Operations.Add(new Operation("copy", PathHelpers.ValidateAndNormalizePath(path), PathHelpers.ValidateAndNormalizePath(from)));
return this; return this;
} }

View File

@ -24,6 +24,23 @@ namespace Microsoft.AspNetCore.JsonPatch
Assert.Equal("/AnotherName", pathToCheck); Assert.Equal("/AnotherName", pathToCheck);
} }
[Fact]
public void Add_RespectsJsonPropertyAttribute_WithDotWhitespaceAndBackslashInName()
{
// Arrange
var obj = new JsonPropertyObjectWithStrangeNames();
var patchDocument = new JsonPatchDocument();
// Act
patchDocument.Add("/First Name.", "John");
patchDocument.Add("Last\\Name", "Doe");
patchDocument.ApplyTo(obj);
// Assert
Assert.Equal("John", obj.FirstName);
Assert.Equal("Doe", obj.LastName);
}
[Fact] [Fact]
public void Move_FallsbackToPropertyName_WhenJsonPropertyAttributeName_IsEmpty() public void Move_FallsbackToPropertyName_WhenJsonPropertyAttributeName_IsEmpty()
{ {
@ -46,6 +63,15 @@ namespace Microsoft.AspNetCore.JsonPatch
public string Name { get; set; } public string Name { get; set; }
} }
private class JsonPropertyObjectWithStrangeNames
{
[JsonProperty("First Name.")]
public string FirstName { get; set; }
[JsonProperty("Last\\Name")]
public string LastName { get; set; }
}
private class JsonPropertyWithNoPropertyName private class JsonPropertyWithNoPropertyName
{ {
[JsonProperty] [JsonProperty]