[ https://issues.apache.org/jira/browse/JOHNZON-274?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Jeffrey Hu updated JOHNZON-274: ------------------------------- 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: {code:java} 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)); } ... }{code} Basically I'm trying to do this: {code:java} { "action": "replace", "path": "/bar/baz", "value": "3"} {code} to this: {code:java} {"foo":{"baz":"1"},"bar":{"baz":"2"}} {code} and I get {code:java} {"foo":{"baz":null},"bar":{"baz":"3"}} {code} instead of {code:java} {"foo":{"baz":"1"},"bar":{"baz":"3"}} {code} Similarly when I do {code:java} { "action": "replace", "path": "/foo/1/bar", "value": "3"} {code} to this: {code:java} {"foo":[{"bar":"1"},{"bar":"2"}]} {code} and I get {code:java} {"foo":[{"bar":null},{"bar":"3"}]} {code} instead of {code:java} {"foo":[{"bar":"1"},{"bar":"3"}]} {code} The problem seems [to be in|https://github.com/stohu/johnzon/commit/515d363b8c55f0ba0835bda18f58dd545c0eb637] the removing method in JsonPointerImpl. The enclosed patch passed all the tests. (based on commit 515d363b8c55f0ba0835bda18f58dd545c0eb637 on master) was: Hi All, I'm new to Apache and open-source projects in general. So please bear with me. This one looks like a bug: {code:java} 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)); } ... }{code} Basically I'm trying to do this: {code:java} { "action": "replace", "path": "/bar/baz", "value": "3"} {code} to this: {code:java} {"foo":{"baz":"1"},"bar":{"baz":"2"}} {code} and I get {code:java} {"foo":{"baz":null},"bar":{"baz":"3"}} {code} instead of {code:java} {"foo":{"baz":"1"},"bar":{"baz":"3"}} {code} Similarly when I do {code:java} { "action": "replace", "path": "/foo/1/bar", "value": "3"} {code} to this: {code:java} {"foo":[{"bar":"1"},{"bar":"2"}]} {code} and I get {code:java} {"foo":[{"bar":null},{"bar":"3"}]} {code} instead of {code:java} {"foo":[{"bar":"1"},{"bar":"3"}]} {code} The problem seems [to be in|https://github.com/stohu/johnzon/commit/515d363b8c55f0ba0835bda18f58dd545c0eb637] the removing method in JsonPointerImpl. The enclosed patch passed all the tests. (based on commit 515d363b8c55f0ba0835bda18f58dd545c0eb637 on master) > Out-of-path values get removed when removing one with same name&level > --------------------------------------------------------------------- > > Key: JOHNZON-274 > URL: https://issues.apache.org/jira/browse/JOHNZON-274 > Project: Johnzon > Issue Type: Bug > Reporter: Jeffrey Hu > Priority: Major > Attachments: > 0001-fix-when-JsonPatch-removing-JsonObjects-not-on-the-p.patch > > > Hi All, > I'm new to Apache and open-source projects in general. So please bear with me. > This one looks like a bug: > > {code:java} > 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)); > } > ... > }{code} > Basically I'm trying to do this: > {code:java} > { "action": "replace", "path": "/bar/baz", "value": "3"} > {code} > to this: > {code:java} > {"foo":{"baz":"1"},"bar":{"baz":"2"}} > {code} > and I get > {code:java} > {"foo":{"baz":null},"bar":{"baz":"3"}} > {code} > instead of > {code:java} > {"foo":{"baz":"1"},"bar":{"baz":"3"}} > {code} > Similarly when I do > {code:java} > { "action": "replace", "path": "/foo/1/bar", "value": "3"} > {code} > to this: > {code:java} > {"foo":[{"bar":"1"},{"bar":"2"}]} > {code} > and I get > {code:java} > {"foo":[{"bar":null},{"bar":"3"}]} > {code} > instead of > {code:java} > {"foo":[{"bar":"1"},{"bar":"3"}]} > {code} > > The problem seems [to be > in|https://github.com/stohu/johnzon/commit/515d363b8c55f0ba0835bda18f58dd545c0eb637] > the removing method in JsonPointerImpl. The enclosed patch passed all the > tests. > (based on commit 515d363b8c55f0ba0835bda18f58dd545c0eb637 on master) -- This message was sent by Atlassian Jira (v8.3.2#803003)