Skip to content

JSON Patch replace entire object in root path throws error #17927

@fabich

Description

@fabich

Describe the bug

JSON Patch implementation does not work as expected when trying to replace entire object.
Error: The target location specified by path segment '' was not found.

To Reproduce

input:

{
  "flightnumber": "3523",
  "dep": "YUL",
  "dest": "YYZ"
}

patch:

[{
"op": "replace",
"path": "",
"value": {
  "flightnumber": "111",
  "dep": "YYC",
  "dest": "YUL"}
}
]

output:

{
  "flightnumber": "111",
  "dep": "YYC",
  "dest": "YUL"
}

Test runs successful with json8 (https://github.com/sonnyp/JSON8):
https://json8.github.io/patch/demos/apply/

code example:

var flight = new FlightPatchDTO{...};
var patchDoc = new JsonPatchDocument<FlightPatchDTO>();
patchDoc.Replace(x => x, flight);

....

Error:

Microsoft.AspNetCore.JsonPatch.Exceptions.JsonPatchException : The target location specified by path segment '' was not found.
   at Microsoft.AspNetCore.JsonPatch.Internal.ErrorReporter.<>c.<.cctor>b__1_0(JsonPatchError error)
   at Microsoft.AspNetCore.JsonPatch.Adapters.ObjectAdapter.Replace(Operation operation, Object objectToApplyTo)
   at Microsoft.AspNetCore.JsonPatch.Operations.Operation`1.Apply(TModel objectToApplyTo, IObjectAdapter adapter)
   at Microsoft.AspNetCore.JsonPatch.JsonPatchDocument`1.ApplyTo(TModel objectToApplyTo, IObjectAdapter adapter)
   at Microsoft.AspNetCore.JsonPatch.JsonPatchDocument`1.ApplyTo(TModel objectToApplyTo)

Further technical details

  • ASP.NET Core version: netcoreapp2.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    ✔️ Resolution: DuplicateResolved as a duplicate of another issueStatus: Resolvedaffected-very-fewThis issue impacts very few customersarea-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesarea-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractionsbugThis issue describes a behavior which is not expected - a bug.feature-json-patchseverity-minorThis label is used by an internal tool

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions