0

I would like to display the subtypes classes in swagger documentation. my code:


app.MapPost("/search", (BaseType t) =>
{
    return $"property: {t.p1}";
});

public interface BaseType
{ 
    public int p1 { get; set; } 
} 
public class SubTypeA : BaseType 
{
    public int p1 { get; set; }
} 
public class SubTypeB : BaseType
{ 
    public int p1 { get; set; } 
} 

I only see the BaseType schema. but not the subtypes schema. Please help :)

I used web api minimal template and enabled OpenAPI tool.

Guru Stron
  • 102,774
  • 10
  • 95
  • 132

1 Answers1

0

I succeeded to make it work just added this configuration:

builder.Services.AddSwaggerGen(c =>
{
    c.UseOneOfForPolymorphism();
    c.SelectSubTypesUsing(baseType =>
    {
        if (baseType == typeof(BaseType))
        {
            return new[]
            {
                typeof(SubTypeA),
                typeof(SubTypeB),
            };
        }

        return Enumerable.Empty<Type>();
    });
});
  • Note that this will not generate discriminator property since `BaseType` is interface. Also note that common convention is to name interfaces starting from `I` -> `IBaseType`. – Guru Stron Dec 29 '22 at 08:42