in

Swagger Swashbuckle Polymorphism doesn’t work with interface types


With the code below I’m trying to mention alternative schemas; which share the same interface, as the type of the response.

Startup.cs

services.AddSwaggerGen(c =>
{
    c.UseOneOfForPolymorphism();
});

app.UseSwagger();
app.UseSwaggerUI(options =>
{
    options.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
});

Models

public interface IOutput
{
    string Discriminator { get; }
}

public class OutputA : IOutput
{
    public string Discriminator => GetType().Name;

    public string PropA { get; set; }
}

public class OutputB : IOutput
{
    public string Discriminator => GetType().Name;

    public string PropB { get; set; }
}

Controller

[HttpGet]
[ProducesResponseType(typeof(IOutput), StatusCodes.Status200OK)]
public IActionResult Get()
{
    return Ok();
}

I’m expecting both types OutputA and OutputB will be listed as return types,
But only IOutput is mentioned:

Alternatively, If I change the IOutput from an interface to a class or an abstract class then OutputA and OutputB will be listed as valid return types:

enter image description here

Is this not supported due to a RESTFul/API standard?

Or can this be achieved?

This is a .NET Core 2.2 project, using Swashbuckle.AspNetCore v6.2.3



Source: https://stackoverflow.com/questions/70555022/swagger-swashbuckle-polymorphism-doesnt-work-with-interface-types

Quizify — Boost your knowledge with singleplayer & multiplayer quizes

Spider-Man: No Way Home’s Amazing Co-Stars Nearly Had a Much Different Introduction