ardalis / Result

A result abstraction that can be mapped to HTTP response codes if needed.
MIT License
866 stars 107 forks source link

System.NotSupportedException: Serialization and deserialization of 'System.Type' instances are not supported. Path: $.ValueType. #91

Closed kfathy-ys closed 1 year ago

kfathy-ys commented 2 years ago

When return result it get exception : System.NotSupportedException: Serialization and deserialization of 'System.Type' instances are not supported. Path: $.ValueType. ---> System.NotSupportedException: Serialization and deserialization of 'System.Type' instances are not supported. at System.Text.Json.Serialization.Converters.UnsupportedTypeConverter1.Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options) at System.Text.Json.Serialization.JsonConverter1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state) at System.Text.Json.Serialization.Metadata.JsonPropertyInfo1.GetMemberAndWriteJson(Object obj, WriteStack& state, Utf8JsonWriter writer) at System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, WriteStack& state) at System.Text.Json.Serialization.JsonConverter1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state) at System.Text.Json.Serialization.JsonConverter1.WriteCore(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)


public Result AddUser(AddUserRequest addUserRequest) { try { return Result.Success("test"); }

koralbit commented 1 year ago

Maybe is too late but you need to add this nuget Microsoft.AspNetCore.Mvc.NewtonsoftJson and then add builder.Services.AddControllers().AddNewtonsoftJson(); in your Program.cs or Startup.cs file.

KyleMcMaster commented 1 year ago

Maybe is too late but you need to add this nuget Microsoft.AspNetCore.Mvc.NewtonsoftJson and then add builder.Services.AddControllers().AddNewtonsoftJson(); in your Program.cs or Startup.cs file.

The original comment is related to System.Text.Json and using the above configuration for MVC and Newtonsoft.Json won't fix this issue as far as I know.

KyleMcMaster commented 1 year ago

We should be able to fix this by either providing a customer serializer for System.Text.Json or possibly refactoring the Result class to make Type a calculated field using an expression. I'll try to work with Steve to get a fix in for this soon.

ardalis commented 1 year ago

Do we need related serializer packages like we have for Ardalis.SmartEnum?

VictorioBerra commented 1 year ago

@ardalis I would personally like serialization support for a result/array of results.

Rogal27 commented 1 year ago

I got this issue when I used [TranslateResultToActionResult] attribute but my controller didn't inherited ControllerBase class. When I added the inheritance everything worked as expected.

KyleMcMaster commented 1 year ago

I believe this can be resolved by making the Type field use an expression rather than a property with an underlying field. I will try to put up a PR with the proposed changes here shortly.