Aspnetcore.docs: JsonPatch fix for ASP.NET Core 3.1 no longer works?

Created on 25 Jul 2020  Â·  8Comments  Â·  Source: dotnet/AspNetCore.Docs

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.

Not Triaged Source - Docs.ms

All 8 comments

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

currently JSON patch requires NewtonsoftJson

@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!

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

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

cc @serpent5

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.

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

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."
]
}
}

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

Could we finally get back to opening this issue?

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fabich picture fabich  Â·  3Comments

royshouvik picture royshouvik  Â·  3Comments

AnthonyMastrean picture AnthonyMastrean  Â·  3Comments

serpent5 picture serpent5  Â·  3Comments

StevenTCramer picture StevenTCramer  Â·  3Comments