Details
-
Bug
-
Status: Resolved
-
Major
-
Resolution: Fixed
-
None
-
None
-
None
Description
Hi All,
I'm new to Apache and open-source projects in general. So please bear with me.
This one looks like a bug:
public class JsonPatchTest { ... @Test public void testReplacingObjectAttribute() { JsonObject object = Json.createObjectBuilder() .add("foo", Json.createObjectBuilder() .add("baz", new JsonStringImpl("1"))) .add("bar", Json.createObjectBuilder() .add("baz", new JsonStringImpl("2"))) .build(); JsonPatchImpl patch = new JsonPatchImpl(PROVIDER, new JsonPatchImpl.PatchValue(PROVIDER, JsonPatch.Operation.REPLACE, "/bar/baz", null, new JsonStringImpl("3"))); JsonObject patched = patch.apply(object); assertNotNull(patched); assertNotSame(object, patched); JsonObject o = patched.getJsonObject("foo"); assertNotNull(o); assertEquals(new JsonStringImpl("1"), o.getJsonString("baz")); o = patched.getJsonObject("bar"); assertNotNull(o); assertEquals(new JsonStringImpl("3"), o.getJsonString("baz")); // fails here assertEquals("{\"foo\":{\"baz\":\"1\"},\"bar\":{\"baz\":\"3\"}}", toJsonString(patched)); } @Test public void testReplacingArrayElementAttribute() { JsonObject object = Json.createObjectBuilder() .add("foo", Json.createArrayBuilder() .add(Json.createObjectBuilder().add("bar", new JsonStringImpl("1"))) .add(Json.createObjectBuilder().add("bar", new JsonStringImpl("2")))) .build(); JsonPatchImpl patch = new JsonPatchImpl(PROVIDER, new JsonPatchImpl.PatchValue(PROVIDER, JsonPatch.Operation.REPLACE, "/foo/1/bar", null, new JsonStringImpl("3"))); JsonObject patched = patch.apply(object); assertNotNull(patched); assertNotSame(object, patched); JsonArray array = patched.getJsonArray("foo"); assertNotNull(array); assertNotSame(object.getJsonArray("foo"), array); assertEquals(2, array.size()); assertEquals(new JsonStringImpl("3"), array.getJsonObject(1).getJsonString("bar")); assertEquals(new JsonStringImpl("1"), array.getJsonObject(0).getJsonString("bar")); // fails here assertEquals("{\"foo\":[{\"bar\":\"1\"},{\"bar\":\"3\"}]}", toJsonString(patched)); } ... }
Basically I'm trying to do this:
{ "action": "replace", "path": "/bar/baz", "value": "3"}
to this:
{"foo":{"baz":"1"},"bar":{"baz":"2"}}
and I get
{"foo":{"baz":null},"bar":{"baz":"3"}}
instead of
{"foo":{"baz":"1"},"bar":{"baz":"3"}}
Similarly when I do
{ "action": "replace", "path": "/foo/1/bar", "value": "3"}
to this:
{"foo":[{"bar":"1"},{"bar":"2"}]}
and I get
{"foo":[{"bar":null},{"bar":"3"}]}
instead of
{"foo":[{"bar":"1"},{"bar":"3"}]}
The problem seems to be in the removing method in JsonPointerImpl. The enclosed patch passed all the tests.
(based on commit 515d363b8c55f0ba0835bda18f58dd545c0eb637 on master)