Urbanite.Extensions.Swagger
0.2.1
dotnet add package Urbanite.Extensions.Swagger --version 0.2.1
NuGet\Install-Package Urbanite.Extensions.Swagger -Version 0.2.1
<PackageReference Include="Urbanite.Extensions.Swagger" Version="0.2.1" />
<PackageVersion Include="Urbanite.Extensions.Swagger" Version="0.2.1" />
<PackageReference Include="Urbanite.Extensions.Swagger" />
paket add Urbanite.Extensions.Swagger --version 0.2.1
#r "nuget: Urbanite.Extensions.Swagger, 0.2.1"
#:package Urbanite.Extensions.Swagger@0.2.1
#addin nuget:?package=Urbanite.Extensions.Swagger&version=0.2.1
#tool nuget:?package=Urbanite.Extensions.Swagger&version=0.2.1
Urbanite
This library extends System.Text.Json to make it easier to serialize polymorphic types. Especially when working with type hierarchies that are not easily described using attributes.
This library works with type hierarhies described using attributes as well as using dependency injection extensions.
To use it, register Urbanite using DIExtension
services.AddUrbanite();
and then use AddUrbaniteSerializableType
to register types that should be serialized polymorphically and couldn't be described using attributes.
interface IAnimal { }
class Dog : IAnimal { }
class Cat : IAnimal { }
services.AddUrbaniteSerializableType<IAnimal, Dog>("dog");
services.AddUrbaniteSerializableType<IAnimal, Cat>("cat");
To serialize polymorphically, use JsonSerializerOptions
with PolymorphicTypeResolver
:
class JsonOptionsConfiguration : IConfigureOptions<JsonOptions>
{
private readonly PolymorphicTypeResolver _resolver;
public JsonOptionsConfiguration(PolymorphicTypeResolver resolver)
{
_resolver = resolver;
}
public void Configure(JsonOptions options)
{
options.JsonSerializerOptions.TypeInfoResolver = _resolver;
}
}
To change type discriminator property name, use the JsonPolymorphic
attirbute
[JsonPolymorphic(TypeDiscriminatorPropertyName = "$my_discriminator")]
interface IBase{}
Swagger
To make swagger work with polymorphic types, use Urbanite.Extensions.Swagger
:
services.AddUrbaniteSwaggerDocumentationPolymorphism();
It will configure swagger to use PolymorphicTypeResolver
to describe polymorphic types (selecting derived types and type discriminators).
And set polymorphic type handling to use All of for inheritance.
Packages
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net6.0
- Microsoft.Extensions.Options (>= 7.0.1)
- Swashbuckle.AspNetCore.SwaggerGen (>= 6.5.0)
- Urbanite (>= 0.2.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.