8

I have a polymorphic model:

public class CreateOrderRequest
{
    public List<CreateOrderItem> OrderItems { get; set; } 
}

/// <summary>
/// Identifies a new item within an order
/// </summary>    
[JsonConverter(typeof(CreateOrderLineJsonConvertor))]
[KnownType("GetKnownTypes")]
public class CreateOrderItem
{
    public string OrderContext { get; set; }
    public string OrderItemType { get; set; }

    public static Type[] GetKnownTypes()
    {
        return new[]
        {
            typeof(OrderItemType1), typeof(OrderItemType2)
        };
    }
}

public class OrderItemType1: CreateOrderItem
{
     public string Type1Prop {get;set;}
}

public class OrderItemType2: CreateOrderItem
{
     public string Type2Prop {get;set;}
}

Using NSwag (NSwag.AspNetCore) according to the documentation, I expected this to be enough so that documentation indicated / contained the order item types? But no...

enter image description here

Have i completely missed the point of what NSwag requires? I have the OrderItemType property. Do i need to get a discriminator involved? Where is this documented?

TIA

6footunder
  • 1,120
  • 14
  • 25

1 Answers1

6

You need

[JsonConverter(typeof(JsonInheritanceConverter), "discriminator")]

on the base class so that the discriminator is added.

See https://github.com/RSuter/NJsonSchema/wiki/Inheritance

Rico Suter
  • 10,556
  • 3
  • 57
  • 89
  • 1
    Thanks Rico. Possibly dumb question. This is an aspnet core web api application. Do i read the linked doc correctly that the attribute enables correct swagger behaviour and deserialization in generated clients ... but i cannot remove the existing base class attribute required for the server to deserialize requests? (Sry, cannot test from my current location) – 6footunder Sep 05 '17 at 07:54
  • 1
    NSwag (or NJS) looks for this converter (the name) and only if its available the correct schema is generated. Additionally the converter adds the discriminator property to the json so that deserializing is working correctly... – Rico Suter Sep 05 '17 at 11:30
  • And btw: The served swagger ui 2.x does not support discriminators, maybe you have to use an own newer version or redoc – Rico Suter Sep 05 '17 at 22:20
  • Didn't work for me. The generated C# class doesn't contain the inherited types. – Vin Shahrdar Sep 27 '19 at 20:37
  • 1
    You also need to add KnownType attributes, and generate the spec and code with NSwag – Rico Suter Sep 27 '19 at 23:23
  • @RicoSuter So question, I have an abstract base class called ResultRecord. I have seven "KnownType" attributes and I have also specified `[JsonConverter(typeof(JsonInheritanceConverter), "discriminator")]` as specified above. When I generate the code, I don't see all the properties of the descendant objects flattened into ResultRecord. Instead, I see a bunch of `JsonInheritanceAttribute` attributes which indicate the hierarchy. Is there a way to achieve what I'm trying to achieve? – Vin Shahrdar Sep 30 '19 at 19:13
  • Maybe this https://github.com/RicoSuter/NJsonSchema/blob/master/src/NJsonSchema/Annotations/JsonSchemaFlattenAttribute.cs – Rico Suter Sep 30 '19 at 20:52
  • But if you have multille concrete types then flatten wont work. You need inheritance – Rico Suter Sep 30 '19 at 20:53