Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# How to handle polymorphic models using NSwag

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

like image 733
6footunder Avatar asked Sep 04 '17 22:09

6footunder


1 Answers

You need

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

on the base class so that the discriminator is added.

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

like image 66
Rico Suter Avatar answered Oct 12 '22 15:10

Rico Suter