Skip to content

Invalid RFC6902 copy operation succeeds #894

Description

@cmannett85
using nlohmann::json;

int main()
{
    auto model = R"({
        "one": {
            "two": {
                "three": "hello",
                "four": 42
            }
        }
    })"_json;

    try {
        model.patch(R"([{"op": "move",
                         "from": "/one/two/three",
                         "path": "/a/b/c"}])"_json);
    } catch (json::exception& e) {
        std::cout << "Move: " << e.id << std::endl;
    }

    try {
        model = model.patch(R"([{"op": "copy",
                                 "from": "/one/two/three",
                                 "path": "/a/b/c"}])"_json);
        std::cout << model.at("/a/b/c"_json_pointer) << std::endl;
    } catch (json::exception& e) {
        std::cout << "Copy: " << e.id << std::endl;
    }
}

// Results in:
Move: 403
"hello"

The above "move" and "copy" JSON patch operations are both invalid for the same reason: /a/b/c is an invalid path because b does not already exist - however, the "copy" succeeds.

Legalise:
RFC6902 4.5:

This operation is functionally identical to an "add" operation at the target location using the value specified in the "from" member.

RFC6092 4.1:

Because this operation is designed to add to existing objects and arrays, its target location will often not exist. Although the pointer's error handling algorithm will thus be invoked, this specification defines the error handling behavior for "add" pointers to ignore that error and add the value as specified.
However, the object itself or an array containing it does need to exist, and it remains an error for that not to be the case.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions