Skip to content

dmitry-bym/JsonKnownTypes.Swashbuckle

Repository files navigation

JsonKnownTypes.Swashbuckle

nuget downloads lisence

Buy Me A Coffee

Add swashbuckle support to the project with JsonKnownTypes

Requirements

Documentation

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 }

License

Authored by: Dmitry Kaznacheev (dmitry-bym)

This project is under MIT license. You can obtain the license copy here.

This work using work of :

About

Swashbuckle support for JsonKnownTypes

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages