Diego
Diego

Reputation: 796

Error when deserializing to ProblemDetails with System.Text.Json.JsonSerializer. .Net Core 3.0

Consider the following json:

{
    "title": "SOME TITEL",
    "status": 500,
    "detail": "Some detail",
    "errors": [
        {
            "Parameter": "SOME VALUE",
            "Code": "SOME CODE",
            "Message": "SOME MESSAGE",
            "Details": "SOME EXTRA DETAILS"
        }
    ]
}

It is generated by an API response that construct a problem details like this:

var problemDetails = new ProblemDetails
{
    Status = StatusCodes.Status500InternalServerError;
    Detail = "DETAIL";
    Title = "TITLE";
};

var customClass = new CustomCalss
{
    Code = "INTERNAL_SERVER_ERROR",
    Message = "Some message",
    Details = "Extra details"
};

problemDetails.Extensions.Add(new KeyValuePair<string, object>("errors", new [] { customClass }));

When trying to deserialize the json to a problem details using System.Text.JsonSerialiser i found the following issues:

I'm testing this behavior like this:

var json = @"{
                        ""title"": ""SOME TITLE"",
                        ""status"": 500,
                        ""detail"": ""Some detail"",
                        ""errors"": [
                            {
                                ""Parameter"": null,
                                ""Code"": ""SOME CODE"",
                                ""Message"": ""SOME MESSAGE"",
                                ""Details"": ""SOME EXTRA DETAILS""
                            }
                        ]
                    }";
var result = JsonSerializer.Deserialize<ProblemDetails>(json);
Assert.NotNull(result.Detail);
Assert.NotNull(result.Title);

var customClass = Assert.IsType<CustomCalss[]>(result.Extensions["errors"]);
var error = customClass.First();
Assert.Equal("INTERNAL_SERVER_ERROR", error.Code);

Any insights?

Upvotes: 0

Views: 3022

Answers (1)

DevPreSupport_MSFT
DevPreSupport_MSFT

Reputation: 282

C# is a case sensitive language, something wrong with your JSON string, the key must exactly same with the property of the target class. I changed the "title" to "Title" and then got the correct value, attached here for your reference:

Screenshot from my test project

Updated on 11/20:

Did not noticed that it is a MVC internal class, we can simple reproduce this issue in local box now, but checked the source codes and known issues online, no existing related issue there. So I reported a new bug for this issue, attached here for your reference

https://github.com/aspnet/AspNetCore/issues/17250

Updated on 11/22: This has been confirmed as a bug in ASP.NET Core 3.0, and fixed in 3.1 version, please upgrade to 3.1-preview2. Alternatively you could specify a custom JsonConverter based on the implementation we have in 3.1 as part of the JsonSerializerOptions that you pass when deserializing - https://github.com/aspnet/AspNetCore/blob/release/3.1/src/Mvc/Mvc.Core/src/Infrastructure/ValidationProblemDetailsJsonConverter.cs

Upvotes: 2

Related Questions