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:

  • Status, Code and Title are not deserialised to the problem details properties, they are null
  • Extension data is not deserialized.

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?

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.