【问题标题】:Swagger Newtonsoft.Json.JsonSerializationException: Required property 'parcel_Triclops_Data' expects a non-null valueSwagger Newtonsoft.Json.JsonSerializationException:必需的属性“parcel_Triclops_Data”需要一个非空值
【发布时间】:2021-12-28 09:00:49
【问题描述】:

我有以下课程:

public class TriclopsParcelDataViewModel
{
    public long ParcelId { get; set; }
    public string ParcelSkuType { get; set; }
    public string ParcelDescription { get; set; }
    public Parcel_Triclops_Data Parcel_Triclops_Data { get; set; }
}

public class Parcel_Triclops_Data
{
    [Key]
    public long ParcelId { get; set; }
    public DateTime Timestamp { get; set; }
    public double WidthCm { get; set; }
    public double HeightCm { get; set; }
    public double DepthCm { get; set; }
    public double WeightKg { get; set; }
    public string TriclopsImage { get; set; }
}

TriclopsParcelDataViewModel 是在我的一个 swagger/api 端点中给出的结果。

然后我从 swagger 定义中生成了一个客户端。

当我调用该函数时,我收到以下错误:

Newtonsoft.Json.JsonSerializationException:必需的属性“parcel_Triclops_Data”需要一个非空值。路径“[0]”,第 1 行,位置 99。

当我查看返回的 json 时,它看起来像这样:

[{"parcelId":13121,"parcelSkuType":"luggage-xl","parcelDescription":"","parcel_Triclops_Data":null},{"parcelId":13122,"parcelSkuType":"luggage-xl","parcelDescription":"","parcel_Triclops_Data":null},{"parcelId":13123,"parcelSkuType":"luggage-xl","parcelDescription":"","parcel_Triclops_Data":null},{"parcelId":13124,"parcelSkuType":"luggage-xl","parcelDescription":"","parcel_Triclops_Data":null}]

我觉得我需要覆盖生成客户端中的 json 序列化设置,但不确定我是否正确。

请问有人能解释一下吗?

【问题讨论】:

  • 您是否使用可空引用类型?如果您将属性更改为 public Parcel_Triclops_Data? Parcel_Triclops_Data { get; set; } 有帮助吗?

标签: c# swagger


【解决方案1】:

我认为你的代码应该是这样的:

public class TriclopsParcelDataViewModel
{
    public Parcel_Triclops_Data ParcelId { get; set; } //changed from long to Parcel_Triclops_Data
    public string ParcelSkuType { get; set; }
    public string ParcelDescription { get; set; }
    public Parcel_Triclops_Data Parcel_Triclops_Data { get; set; }
}

public class Parcel_Triclops_Data
{
    [Key]
    public long ParcelId { get; set; }
    public DateTime Timestamp { get; set; }
    public double WidthCm { get; set; }
    public double HeightCm { get; set; }
    public double DepthCm { get; set; }
    public double WeightKg { get; set; }
    public string TriclopsImage { get; set; }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-16
    • 2017-06-19
    • 1970-01-01
    • 2015-12-28
    • 2022-01-25
    • 1970-01-01
    相关资源
    最近更新 更多