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

If Replace operation is performed on JSON that doesn't have specified fields, these fields are added to JSON #397

Closed
anaruzhnii opened this issue Mar 9, 2023 · 2 comments · Fixed by #400
Labels
bug Something isn't working pkg:patch

Comments

@anaruzhnii
Copy link

Environment

  • Nuget Package: JsonPatch.Net
  • Nuget Version: 2.0.5
  • OS: Windows 11 Pro
  • .Net Target: .NET 5, 6, 7

Describe the bug
If Replace operation is performed on JSON that doesn't have specified fields, these fields are added to JSON

To Reproduce

Code to reproduce the problem

using System;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using Json.Patch;
using Json.Pointer;

const string mask = "*****";
var maskJson = JsonValue.Create(mask);

var pathsToPatch = new[] { "/first_name", "/last_name" };

var patchOperations = pathsToPatch.Select(path => PatchOperation.Replace(JsonPointer.Parse(path), maskJson));
var patchConfig = new JsonPatch(patchOperations);

const string singleObjectJson =
    "{\"id\":\"640729d45434f90313d25c78\",\"guid\":\"f2e2767c-03e0-4862-addc-7d46c55efb33\",\"city\":\"Boston\"}";

var singleObject = JsonNode.Parse(singleObjectJson);
var patchedSingleObject = patchConfig.Apply(singleObject).Result;
Console.WriteLine(JsonSerializer.Serialize(patchedSingleObject));
// ^ outputs {"id":"640729d45434f90313d25c78","guid":"f2e2767c-03e0-4862-addc-7d46c55efb33","city":"Boston","first_name":"*****","last_name":"*****"}

Expected behavior
Nothing changes in JSON
Considering that https://jsonpatch.com/ says that Replace equals to Remove and Add, I assume Remove result should be checked before adding the new fields

@anaruzhnii anaruzhnii added the bug Something isn't working label Mar 9, 2023
@gregsdennis
Copy link
Owner

That's a reasonable check. Have you tested on any other online playgrounds?

@anaruzhnii
Copy link
Author

So I've just checked that and found out that the RFC6902 notation explicitly says that The target location MUST exist for the operation to be successful.
I've also tried it out using online tools and JS libraries.
https://jsonpatch.me/ silently returns unmodified JSON.
Both jsonpatch.js and jsonpatch-js throw an error about missing value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pkg:patch
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants