1

我有课堂操作:

public class Operation
{
    public string Type { get; set; }

    public OperationOptions Options { get; set; }
}

其中的值Type定义了 的类型Options。但我必须添加discriminatorOperationOptions类型

[KnownType(typeof(EchoOptions))]
[KnownType(typeof(VetDocumentAcceptanceOptions))]
[JsonConverter(typeof(JsonInheritanceConverter), "discriminator")]
public class OperationOptions
{
}

public class EchoOptions : OperationOptions
{
}

public class VetDocumentAcceptanceOptions : OperationOptions
{
}

所以我进入swagger.json:

  "OperationOptions": {
    "type": "object",
    "discriminator": {
      "propertyName": "discriminator",
      "mapping": {
        "EchoOptions": "#/components/schemas/EchoOptions",
        "VetDocumentAcceptanceOptions": "#/components/schemas/VetDocumentAcceptanceOptions"
      }
    }

Discriminator但我的模型中没有属性OperationOptions

有没有办法使用TypeinOperation作为鉴别器OperationOptions

4

1 回答 1

0

JsonInheritanceConverter自动将该discriminator属性添加到序列化的 JSON 中,并在将 JSON 反序列化为类型时使用该属性来选择正确的类型。实际上最好不要将该属性视为 C# 属性。

于 2018-12-03T11:29:40.300 回答