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

JSonPatch doesn't work #22216

Closed
erwan-joly opened this issue May 26, 2020 · 5 comments
Closed

JSonPatch doesn't work #22216

erwan-joly opened this issue May 26, 2020 · 5 comments
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. Status: Resolved

Comments

@erwan-joly
Copy link

erwan-joly commented May 26, 2020

Describe the bug

When trying to use a JsonPatchDocument i get error trying to deserialize an interface.
https://github.com/dotnet/aspnetcore/blob/master/src/Features/JsonPatch/src/JsonPatchDocument.cs#L24
https://github.com/dotnet/aspnetcore/blob/master/src/Features/JsonPatch/src/JsonPatchDocumentOfT.cs
This seems to be because it's still using Newtonsoft instead of the new JSon.Net

using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

my understanding is that it should be

using System.Text.Json;
using System.Text.Json.Serialization;

To Reproduce

[HttpPatch]
public HttpStatusCode Test([FromBody] JsonPatchDocument data)

This will error on binding.

System.NotSupportedException: Deserialization of interface types is not supported. Type 'Newtonsoft.Json.Serialization.IContractResolver' Path: $.ContractResolver | LineNumber: 0 | BytePositionInLine: 140.
 ---> System.NotSupportedException: Deserialization of interface types is not supported. Type 'Newtonsoft.Json.Serialization.IContractResolver'
   at System.Text.Json.ThrowHelper.ThrowNotSupportedException_DeserializeNoDeserializationConstructor(Type invalidType)
   at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   at System.Text.Json.JsonPropertyInfo`1.ReadJsonAndSetMember(Object obj, ReadStack& state, Utf8JsonReader& reader)
   at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   --- End of inner exception stack trace ---
   at System.Text.Json.ThrowHelper.ThrowNotSupportedException(ReadStack& state, Utf8JsonReader& reader, NotSupportedException ex)
   at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   at System.Text.Json.Serialization.JsonConverter`1.ReadCoreAsObject(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   at System.Text.Json.JsonSerializer.ReadCore[TValue](JsonConverter jsonConverter, Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   at System.Text.Json.JsonSerializer.ReadCore[TValue](JsonReaderState& readerState, Boolean isFinalBlock, ReadOnlySpan`1 buffer, JsonSerializerOptions options, ReadStack& state, JsonConverter converterBase)
   at System.Text.Json.JsonSerializer.ReadAsync[TValue](Stream utf8Json, Type returnType, JsonSerializerOptions options, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonInputFormatter.ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding)
   at Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonInputFormatter.ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding)
   at Microsoft.AspNetCore.Mvc.ModelBinding.Binders.BodyModelBinder.BindModelAsync(ModelBindingContext bindingContext)
   at Microsoft.AspNetCore.Mvc.ModelBinding.ParameterBinder.BindModelAsync(ActionContext actionContext, IModelBinder modelBinder, IValueProvider valueProvider, ParameterDescriptor parameter, ModelMetadata metadata, Object value)
   at Microsoft.AspNetCore.Mvc.Controllers.ControllerBinderDelegateProvider.<>c__DisplayClass0_0.<<CreateBinderDelegate>g__Bind|0>d.MoveNext()
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)

Further technical details

  • ASP.NET Core 3.1
  • Windows
  • Visual Studio 2019
@javiercn javiercn added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label May 26, 2020
@pranavkm
Copy link
Contributor

JsonPatch requires using Newtonsoft.Json in the app. https://docs.microsoft.com/en-us/aspnet/core/web-api/jsonpatch has some suggestions on configuring the application to use Newtonsoft.Json specifically for JsonPatch while using S.T.Json for the remainder of JSON serialization.

@pranavkm pranavkm added the ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. label May 26, 2020
@ghost ghost added the Status: Resolved label May 26, 2020
@ghost
Copy link

ghost commented May 27, 2020

This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes.

See our Issue Management Policies for more information.

@ghost ghost closed this as completed May 27, 2020
@erwan-joly
Copy link
Author

@pranavkm is the second method still working ?
if i use that it works perfectly (my understanding is that it use newtonsoftjson for everything)

            services
                .AddControllers()
                .AddNewtonsoftJson()
               .AddControllersAsServices();

but when i use this

            services
                  .AddControllers(options =>
                 {
                      options.InputFormatters.Insert(0, GetJsonPatchInputFormatter());
                 })
                .AddControllersAsServices();

json patch is deserialized as null.

i did a quick repo to reproduce:

https://github.com/erwan-joly/jsonpatchbugexample/blob/master/JSonPatchExample/Program.cs#L58

@pranavkm
Copy link
Contributor

pranavkm commented Jun 1, 2020

The media type in your application isn't configured correctly: https://github.com/erwan-joly/jsonpatchbugexample/blob/master/JSonPatchExample/Program.cs#L77. It needs to be application/json-patch+json: https://tools.ietf.org/html/rfc6902#section-1.

@erwan-joly
Copy link
Author

Thanks @pranavkm it definitely was the issue.
got it wrong as AddNewtonsoftJson make it works with just application/json and i was not aware of the other mime type.

@ghost ghost locked as resolved and limited conversation to collaborators Jul 1, 2020
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. Status: Resolved
Projects
None yet
Development

No branches or pull requests

3 participants