tavis-software / tavis.jsonpatch Goto Github PK
View Code? Open in Web Editor NEWLicense: Apache License 2.0
License: Apache License 2.0
It looks like Tavis.JsonPatch puts an extra "
at the beginning and end of JSON arrays.
The following example (taken from jsonpatch.com) fails:
{
"baz": "qux",
"foo": "bar"
}
[
{ "op": "replace", "path": "/baz", "value": "boo" },
{ "op": "add", "path": "/hello", "value": ["world"] },
{ "op": "remove", "path": "/foo"}
]
{
"baz": "boo",
"hello": ["world"]
}
This can be written in C#, xUnit.net, using Tavis.JsonPatch as:
[Fact]
public void PatchFromJsonStringReturnsCorrectResult()
{
// Arrange
var jsonString = @"
{
""baz"": ""qux"",
""foo"": ""bar""
}";
var operations = ConvertFrom(JToken.Parse(@"
[
{ ""op"": ""replace"", ""path"": ""/baz"", ""value"": ""boo"" },
{ ""op"": ""add"", ""path"": ""/hello"", ""value"": [""world""] },
{ ""op"": ""remove"", ""path"": ""/foo""}
]"));
var patchDocument = new PatchDocument(operations.ToArray());
// Act
var actual = JToken.Parse(jsonString);
patchDocument.ApplyTo(actual);
// Assert
var expected = JToken.Parse(@"
{
""baz"": ""boo"",
""hello"": [""world""]
}");
Assert.Equal(expected, actual);
}
Output:
Test 'Xyz.PatchFromJsonStringReturnsCorrectResult' failed: Assert.Equal() Failure
Position: First difference is at position 0
Expected: JObject { "baz": "boo", "hello": [
"world"
] }
Actual: JObject { "baz": "boo", "hello": "[\r\n \"world\"\r\n]" }
Xyz.cs(307,0): at Xyz.PatchFromJsonStringReturnsCorrectResult()
It looks like Tavis.JsonPatch puts an extra "
at the beginning and end of JSON arrays, but it shouldn't.
The ConvertFrom
function is defined as:
private static IEnumerable<Operation> ConvertFrom(JToken operations)
{
foreach (dynamic operation in operations)
{
var op = operation.op.ToString();
var path = operation.path.ToString();
if (op == "replace")
{
var value = operation.value.ToString();
yield return new ReplaceOperation
{
Path = new JsonPointer(path),
Value = new JValue(value)
};
}
else if (op == "add")
{
var value = operation.value.ToString();
yield return new AddOperation
{
Path = new JsonPointer(path),
Value = new JValue(value)
};
}
else if (op == "remove")
{
yield return new RemoveOperation
{
Path = new JsonPointer(path)
};
}
}
}
Hi,
Your test operation is incorrect : it compares Path(target) with target.
The code should be :
protected override void Test(TestOperation operation)
{
var existingValue = operation.Path.Find(_target);
if (!existingValue.Equals(operation.Value))
{
throw new InvalidOperationException("Value at " + operation.Path.ToString() + " does not match.");
}
}
Note: I'm unable to make a PR from where I'm writing
Ensure that we don't need to clone.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.