Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature request]: Integer as type discriminator #3132

Open
vanillajonathan opened this issue Nov 7, 2024 · 1 comment
Open

[Feature request]: Integer as type discriminator #3132

vanillajonathan opened this issue Nov 7, 2024 · 1 comment
Labels
feature suggestion help-wanted A change up for grabs for contributions from the community

Comments

@vanillajonathan
Copy link
Contributor

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 martincostello added the help-wanted A change up for grabs for contributions from the community label Nov 7, 2024
@martincostello
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature suggestion help-wanted A change up for grabs for contributions from the community
Projects
None yet
Development

No branches or pull requests

2 participants