dotnet / AspNetCore.Docs

Documentation for ASP.NET Core
https://docs.microsoft.com/aspnet/core
Creative Commons Attribution 4.0 International
12.61k stars 25.3k forks source link

JsonPatch fix for ASP.NET Core 3.1 no longer works? #19320

Closed kikaragyozov closed 4 years ago

kikaragyozov commented 4 years ago

I've done as instructed by writing the following snippet of code

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllersWithViews(options =>
    {
        options.InputFormatters.Insert(0, GetJsonPatchInputFormatter());
    });
}

private static NewtonsoftJsonPatchInputFormatter GetJsonPatchInputFormatter()
{
    var builder = new ServiceCollection()
        .AddLogging()
        .AddMvc()
        .AddNewtonsoftJson()
        .Services.BuildServiceProvider();

    return builder
        .GetRequiredService<IOptions<MvcOptions>>()
        .Value
        .InputFormatters
        .OfType<NewtonsoftJsonPatchInputFormatter>()
        .First();
}

But it doesn't seem to work at all. I get the same PatchDocument error as per usual.

An example of my Endpoint (deriving from ControllerBase)

        [HttpPatch("save")]
        public TaskDto SaveObject([FromBody] JsonPatchDocument<SampleObject> patchDoc)
        {
            var obj= new SampleObject();
            patchDoc.ApplyTo(obj);
            return obj;
        }

If I just include the Newtonsoft json serializer into the service collection, it all works, but I lose the benefits of Microsoft's System.Text.Json package, as the serializer is now Newton's.


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Rick-Anderson commented 4 years ago

If I just include the Newtonsoft json serializer into the service collection, it all works,

currently JSON patch requires NewtonsoftJson

kikaragyozov commented 4 years ago

@Rick-Anderson docs say you can still leverage System.Text.Json by doing the above snippet of code? But that's not the case at all!

Rick-Anderson commented 4 years ago

Where does it say that? What exactly. It says:

The preceding code requires the Microsoft.AspNetCore.Mvc.NewtonsoftJson

cc @serpent5

kikaragyozov commented 4 years ago

Direct quote from documentation:

AddNewtonsoftJson replaces the System.Text.Json-based input and output formatters used for formatting all JSON content. To add support for JSON Patch using Newtonsoft.Json, while leaving the other formatters unchanged, update the project's Startup.ConfigureServices method as follows:

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllersWithViews(options =>
    {
        options.InputFormatters.Insert(0, GetJsonPatchInputFormatter());
    });
}

private static NewtonsoftJsonPatchInputFormatter GetJsonPatchInputFormatter()
{
    var builder = new ServiceCollection()
        .AddLogging()
        .AddMvc()
        .AddNewtonsoftJson()
        .Services.BuildServiceProvider();

    return builder
        .GetRequiredService<IOptions<MvcOptions>>()
        .Value
        .InputFormatters
        .OfType<NewtonsoftJsonPatchInputFormatter>()
        .First();
}

The text in bold is where I believe it says that. The code snippet included is part of the documentation and what the quote was referring to as the solution - using it as provided does nothing - the JSON parser fails to parse incoming JSON to JsonPatchDocument.

serpent5 commented 4 years ago

The approach in the topic works. What are you sending as the Content-Type and what's the error?

sarahbe commented 4 years ago

I am having the same issue and the same configuration mentioned above. this is the error message I am getting. { "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1", "title": "One or more validation errors occurred.", "status": 400, "traceId": "|f5ba494d-422d1595d82115d9.", "errors": { "$": [ "The JSON value could not be converted to Microsoft.AspNetCore.JsonPatch.JsonPatchDocument`1[Portal.Web.DataModels.TestModel]. Path: $ | LineNumber: 0 | BytePositionInLine: 1." ] } }

kikaragyozov commented 4 years ago

Minimal reproducible repository/solution: https://github.com/SpiritBob/BrokenJsonPatch

Could we finally get back to opening this issue?

serpent5 commented 4 years ago

Minimal reproducible repository/solution: https://github.com/SpiritBob/BrokenJsonPatch

This also works when I try it. You must use application/json-patch+json as the Content-Type for the request being sent to that controller. If you use application/json, you'll see the error message shown in @sarahbe's comment.