Add swashbuckle support to the project with JsonKnownTypes
To add JsonKnowTypes support in swashbuckle use AddSwaggerGenJsonKnownTypesSupport
extension method
public void ConfigureServices(IServiceCollection services)
{
services
.AddMvc()
.AddNewtonsoftJson();
services
.AddSwaggerGen()
.AddSwaggerGenNewtonsoftSupport()
.AddSwaggerGenJsonKnownTypesSupport();
}
It will add discriminator field and will use base class
[JsonConverter(typeof(JsonKnownTypesConverter<Animal>))]
public record Animal(int Age);
Example Value | Schema in SwaggerUI:
{ "$type": "string", "age": 0 }
Also you can use it with custom discriminator
[JsonConverter(typeof(JsonKnownTypesConverter<Animal>))]
[JsonDiscriminator(Name = "kind")]
public record Animal(int Age);
Example Value | Schema in SwaggerUI:
{ "kind": "string", "age": 0 }
Authored by: Dmitry Kaznacheev (dmitry-bym)
This project is under MIT license. You can obtain the license copy here.
This work using work of :
- James Newton-King, author of Json.NET. https://www.newtonsoft.com/json
- Richard Morris, author of Swashbuckle.AspNetCore https://github.com/domaindrivendev/Swashbuckle.AspNetCore