Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DocumentContext returns for the same path always a different object #211

Closed
jlolling opened this issue Mar 17, 2016 · 2 comments
Closed

Comments

@jlolling
Copy link
Contributor

I am testing this configuration. It looks like this provider does not keep the objects in sync.
When I modify a JsonNode from the Jackson API these modification will not refected by the JsonNode returned by the DocumentContext.read() method.

    DocumentContext docContext = JsonPath.using(JACKSON_JSON_NODE_CONFIGURATION).parse("[]");

    ObjectNode nodeMadeByJackson_1 = new ObjectNode(JsonNodeFactory.instance);
    nodeMadeByJackson_1.put("a1", "v1");
    docContext.add("$", nodeMadeByJackson_1);
    // looks: [{"a1":"v1"}]
    System.out.println("1: " + docContext.jsonString());

    // now add to the first array node a new attribute referring to a new node
    ObjectNode nodeMadeByJackson_2 = new ObjectNode(JsonNodeFactory.instance);
    nodeMadeByJackson_2.put("a21", "v21");
    docContext.put("$[0]","a2", nodeMadeByJackson_2);
    // looks: [{"a1":"v1","a2":{"a21":"v21"}}]
    System.out.println("2: " + docContext.jsonString());

    // read back the first array node
    ObjectNode node_retrieved_from_context_1 = docContext.read("$[0]");
    System.out.println("3: " + node_retrieved_from_context_1);

    ObjectNode test21 = docContext.read("$[0].a2");
    System.out.println("4: " + test21);

    // now we are trying to get back the nodeMadeByJackson_2 but we get null!
    JsonNode test22 = nodeMadeByJackson_1.get("a2");
    System.out.println("5: " + test22);

Here the output:

1: [{"a1":"v1"}]
2: [{"a1":"v1","a2":{"a21":"v21"}}]
3: {"a1":"v1","a2":{"a21":"v21"}}
4: {"a21":"v21"}
5: null

This is a huge problem in the conjunction of jayway json path with Jackson Json API.
Do you have some suggestions how to improve this ?

@jlolling jlolling changed the title JacksonJsonNodeJsonProvider JacksonJsonNodeJsonProvider does not keep the nodes in sync Mar 17, 2016
@jlolling jlolling changed the title JacksonJsonNodeJsonProvider does not keep the nodes in sync DocumentContext returns for the same path always a different object Mar 17, 2016
@jlolling
Copy link
Contributor Author

I have found the problem. It is caused by the JacksonJsonNodeJsonNodeProvider.
In this class the setProperty and setArrayIndex methods always creates a copy from the given value also if the value is a JsonNode.
I have created a patch for me and now it works. I will fork your repository, make the changes and send you a pull request.

Your API is great and now I can modify the returned JsonNodes and it is in sync with the model.

kallestenflo added a commit that referenced this issue Mar 24, 2016
…ed_cloned_objects

Bug fixed: #211 setProperty and setArrayIndex set/add cloned nodes
@ben-manes
Copy link

This backwards incompatible change broke me in #364. Can we iterate on it further?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants