1

I have class operation:

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

    public OperationOptions Options { get; set; }
}

Where value of Type defines type of Options. But I have to add discriminator to OperationOptions type

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

public class EchoOptions : OperationOptions
{
}

public class VetDocumentAcceptanceOptions : OperationOptions
{
}

So I get in swagger.json:

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

But I don't have property Discriminator in OperationOptions in my model.

Is there any way to use Type in Operation as discriminator for OperationOptions?

Backs
  • 24,430
  • 5
  • 58
  • 85

1 Answers1

0

The JsonInheritanceConverter will automatically add the discriminator property to the serialized JSON and use this property when deserializing JSON to a type to select the correct type. It's better to actually not see the property as a C# property.

Rico Suter
  • 11,548
  • 6
  • 67
  • 93