johnzon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jeffrey Hu (Jira)" <j...@apache.org>
Subject [jira] [Created] (JOHNZON-274) Out-of-path values get removed when removing one with same name&level
Date Sun, 08 Sep 2019 15:29:00 GMT
Jeffrey Hu created JOHNZON-274:
----------------------------------

             Summary: 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
         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)

Mime
View raw message