domaindrivendev / Swashbuckle.AspNetCore

Swagger tools for documenting API's built on ASP.NET Core
MIT License
5.26k stars 1.32k forks source link

[Feature request]: Integer as type discriminator #3132

Closed vanillajonathan closed 1 week ago

vanillajonathan commented 2 weeks ago

Is your feature request related to a specific problem? Or an existing feature?

I am using integers as type discriminator.

[JsonPolymorphic]
[JsonDerivedType(typeof(Cat), (int)PetKind.Cat)]
[JsonDerivedType(typeof(Dog), (int)PetKind.Dog)]
public class BaseModel
{
    public string Name { get; set; }
}

The SelectDiscriminatorValueUsing method can only return a String.

builder.Services.AddSwaggerGen(c =>
{
    c.UseOneOfForPolymorphism();
    c.SelectDiscriminatorNameUsing((baseType) => "$type");
    c.SelectDiscriminatorValueUsing((subType) => subType.Name); // can only return String
});

Describe the solution you'd like

An overload for the SelectDiscriminatorValueUsing method that accepts a custom selector with the signature Func<Type, int> customSelector.

Additional context

No response

martincostello commented 2 weeks ago

Assuming that arbitrary type descriminiators are valid in OpenAPI, then that sounds reasonable. However we'd want to make it generic if possible (int, bool, etc.), rather than only supporting either an int or a string.

bkoelman commented 1 week ago

Careful, it looks like the discriminator property must be a string. See https://redocly.com/docs/resources/discriminator and https://docs.42crunch.com/latest/content/oasv3/oasconformance/semantics/v3-semantic-discriminator-string.htm.

martincostello commented 1 week ago

Thanks for looking into that @bkoelman

If that's the case, then this isn't something we would add to Swashbuckle.