-
Notifications
You must be signed in to change notification settings - Fork 10k
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
JsonPatchDocument should use System.Text.Json in ASP.NET vNext #24333
Comments
I propose to:
|
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process. |
Thanks for your willingness to help. |
There have been many issues reported regarding json patch still being tied to Newtonsoft.Json deserialization. The issues are caused by adverse effects of the association, or directly indicating dissatisfaction with it: System.Text.Json has merits over Newtonsoft.Json purported by Microsoft with indication to fully remove the Json.NET dependency from ASP.NET Core.
Configuration of json patch to only use Newtonsoft.Json processing for patch requests is contrived, requiring building an entire separate service collection with complete MVC to pull an input formatter with appropriate DI. To indicate the intent of complete System.Text.Json support in ASP.NET Core, not have the support, and then to consistently delay in the face of consistent issues is extremely discouraging: |
I wonder if the easiest way to have that feature added wouldn't be to fork the feature project https://github.com/dotnet/aspnetcore/commits/master/src/Features/JsonPatch in another repo |
This is bit of an annoying oversight. Any news? |
+1, Everything is moving to System.Text.Json... Swagger, Refit, etc... this needs to be addressed. |
Do you have any plans to use JsonPatchDocument with System.Text.Json in upcoming .NET 6 ? |
Has anybody found good open source json patch library for system text json? |
|
A lot of projects have dropped support for Newtonsoft.Json (especially for annotations) in favor of System.Text.Json |
@pranavkm I think this is worth looking into. It is a bummer that we have to rely on Newtonsoft. The code in the documentation throws |
Still a issue? Keeps us from switching. |
Yes. |
Install this package Microsoft.AspNetCore.Mvc.Formatters.Json for System.Text.Json |
This seems to use newtonsoft from Microsoft.AspNetCore.JsonPatch |
Indirectly your are https://www.nuget.org/packages/Microsoft.AspNetCore.JsonPatch/ |
@pdevito3 Thanks! I'm not sure we'll be able to pull this off for .NET 7 as patch support for System.Text.Json is currently unplanned but this is something we should get ahead of at least in the docs. Maybe we can write a sample converter to make this work in the mean time. |
I started implementing this myself, its based on aspnet version. https://github.com/Havunen/SystemTextJsonPatch |
@Havunen WONDERFUL! We should point people to this for now. Are there any other implementations that you are aware of? |
|
Any update on this? |
We are not doing this for .NET 7, the work isn't planned and doesn't fit into the release based on the other work we're already doing. PS: This item is in the backlog. |
Could MS please clarify what their commitment to AspNetCore.JsonPatch is, as it's looking pretty shaky to me. Reasons for thinking this:
|
Can you try out SystemTextJsonPatch? I believe I have fixed both the issues you reported about AspNetCore.JsonPatch. |
This issue doesn't impact a small number of customers... |
Why isn't System.Text.Json adopting JsonPatchDocument? |
@davidfowl any plan to release in .NET 8? |
Minimal API in .Net 7 supports patch operations. Has anyone worked out how to use this with JsonPatchDocument?? |
You can do it, step by step, this way, for example: dotnet add package Newtonsoft.Json app.MapPatch("/", async (HttpContext context) =>
{
if (context.Request.HasJsonContentType())
{
using var reader = new StreamReader(context.Request.Body);
var json = await reader.ReadToEndAsync();
var doc = JsonConvert.DeserializeObject<JsonPatchDocument>(json);
var originalDocument = new Models.Document(); // change to get an entity from database
doc.ApplyTo(originalDocument);
return Results.Ok(originalDocument);
}
return Results.BadRequest();
});
public class Document
{
public string Baz { get; set; } = "qux";
public string[] Hello { get; set; }
public string Foo { get; set; } = "bar";
} With this solution Newtonsoft.Json is used only for single endpoint. Maybe someone knows a better solution? |
Probably use: |
For those who prefer to avoid adding the [HttpPatch]
public async Task<ApiResponse<bool>> PatchAsync([FromBody] JsonElement jsonElement)
{
// Use Newtonsoft.Json.JsonConvert to deserialize the JSON string into a JsonPatchDocument
var patch = JsonConvert.DeserializeObject<JsonPatchDocument>(jsonElement.GetRawText());
} |
Thank to @deleteLater we can use in Minimal Api: app.MapPatch("/", async ([FromBody] JsonElement jsonElement) =>
{
var json = jsonElement.GetRawText();
var doc = JsonConvert.DeserializeObject<JsonPatchDocument>(json);
var originalDocument = new JsonPatchDocumentMinimalApi.Models.Document();
doc.ApplyTo(originalDocument);
return Results.Ok(originalDocument);
}); |
I'm looking for better implementation yet. I thought about using my own implemention of IModelBinder but in the minimal api seems not to be supported. In .NET 7 for Custom Binding we can use only TryParse or BindAsync methods: It is easy to add for own model but not for JsonPatchDocument. |
@Havunen solution works very well. I have just given the GitHub project a star :) Highly recommend we all use this until Microsoft comes up with a solution |
this is something that should be coming with the framework instead of doing a custom model binder to map all parameters. Microsoft should follow the standard: https://datatracker.ietf.org/doc/html/rfc6902 |
mkArtakMSFT you're thinking quite a lot, maybe time to do anything? :) |
Happy 4 year anniversary for a very important issue :( |
I've tried this out and it worked out of the box, brilliant work! |
Its niggling and neglected issues like this that cause many enterprise projects to abandon using the .Net framework entirely and move other API frameworks like Python FastAPI (https://medium.com/@david.danier/how-to-handle-patch-requests-with-fastapi-c9a47ac51f04) or Java SpringBoot 3 (https://www.baeldung.com/spring-rest-json-patch). .Net is regarded as incomplete for enterprise work due to such issues and makes advocating for it's continued use in the enterprise really difficult when competing frameworks offer a friction free developer experience. |
Related to #16968
Will you accept pull-request with this feature?
The text was updated successfully, but these errors were encountered: