【问题标题】:Error when deserializing to ProblemDetails with System.Text.Json.JsonSerializer. .Net Core 3.0使用 System.Text.Json.JsonSerializer 反序列化到 ProblemDetails 时出错。 .Net Core 3.0
【发布时间】:2020-03-11 14:12:00
【问题描述】:

考虑以下 json:

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

它由 API 响应生成,该响应构造如下问题详细信息:

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 }));

当尝试使用 System.Text.JsonSerialiser 将 json 反序列化为问题详细信息时,我发现了以下问题:

  • 状态、代码和标题未反序列化为问题详细信息属性,它们为空
  • 扩展数据未反序列化。

我正在像这样测试这种行为:

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);

有什么见解吗?

【问题讨论】:

    标签: json json-deserialization asp.net-core-3.0


    【解决方案1】:

    C# 是区分大小写的语言,你的 JSON 字符串有问题,键必须与目标类的属性完全相同。我把“title”改成了“Title”,然后得到了正确的值,附在这里供大家参考:

    11/20 更新:

    没有注意到它是一个MVC内部类,我们现在可以在本地框简单地重现这个问题,但是在网上查了源代码和已知问题,没有存在相关问题。所以我报告了这个问题的一个新错误,附在这里供您参考

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

    11/22 更新: 这已在 ASP.NET Core 3.0 中确认为 bug,并在 3.1 版本中修复,请升级到 3.1-preview2。或者,您可以根据我们在 3.1 中的实现指定自定义 JsonConverter,作为您在反序列化时传递的 JsonSerializerOptions 的一部分 - https://github.com/aspnet/AspNetCore/blob/release/3.1/src/Mvc/Mvc.Core/src/Infrastructure/ValidationProblemDetailsJsonConverter.cs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-26
      • 2021-05-21
      • 2020-11-30
      • 2018-06-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多