Skip to content
New issue

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

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

Already on GitHub? # to your account

DefaultDeltaFormatter.FormatArray throws System.FormatException: 'Invalid patch document.' #49

Open
stefarv opened this issue Jul 1, 2024 · 0 comments

Comments

@stefarv
Copy link

stefarv commented Jul 1, 2024

When using ArrayObjectItemKeyFinder with test data like below, System.FormatException: 'Invalid patch document.' is thrown

json1
{ "id": 1, "name": "Some name", "projects": [{"id": 88, "name": "bar"}] }

json2
{ "id": 1, "name": "Some name", "projects": [{"id": 77, "name": "foo"}, {"id": 88, "name": "bar z"}] }

Code to reproduce:

using System.Text.Json.JsonDiffPatch.Diffs.Formatters;
using System.Text.Json.JsonDiffPatch;
using System.Text.Json.Nodes;

string json1 = "{ \"id\": 1, \"name\": \"Some name\", \"projects\": [{\"id\": 88, \"name\": \"bar\"}] }";
string json2 = "{ \"id\": 1, \"name\": \"Some name\", \"projects\": [{\"id\": 77, \"name\": \"foo\"}, {\"id\": 88, \"name\": \"bar z\"}] }";

// project with id 77 inserted first in the array, name for project with id 88 changed from "bar" to "bar z"

object? IdKeyFinder(JsonNode? n, int i)
{
    if (n is JsonObject obj
           && obj.TryGetPropertyValue("id", out var value))
    {
        return value?.ToString();
    }

    return null;
}

var diff = JsonDiffPatcher.Diff(
    json1,
    json2,
    new JsonPatchDeltaFormatter(),
    new JsonDiffOptions
    {
        JsonElementComparison = JsonElementComparison.RawText,
        ArrayObjectItemMatchByPosition = false,
        ArrayObjectItemKeyFinder = IdKeyFinder
    });

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

No branches or pull requests

1 participant