SmartEnum with Swashbuckle doesn't show list of values like an enum
- Dominant language
- C#
- Stars
- 2.4k
- Forks
- 184
- PR merge metrics
- No merged PRs in 30d
Description
If you have a model on an API with a smart enum like this one and an enum
```
public TestEnum TestEnum { get; set; }
public TestSmartEnum SmartEnum { get; set; }
public class TestSmartEnum: SmartEnum
{
public static readonly TestSmartEnum One = new TestSmartEnum("One", 1);
public static readonly TestSmartEnum Two = new TestSmartEnum("Two", 2);
public static readonly TestSmartEnum Three = new TestSmartEnum("Three", 3);
public TestSmartEnum(string name, byte value) : base(name, value)
{
}
}
public enum TestEnum
{
One,
Two,
Three
}
```
```
// Configurates enums to show their names instead the values.
services.AddMvc()
.AddJsonOptions(options => options.SerializerSettings.Converters.Add(new StringEnumConverter()))
```
and you are using Swashbuckle to show your API, the smartEnum doesn´t show their values as if were an enum.
I've been able to create a schemaFilter and use it to show something thats shows information but It's not the same that with an enum.
```
public class SmartEnumSchemaFilter : ISchemaFilter
{
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
{
if (context.Type == typeof(TestSmartEnum))
{
var list = TestSmartEnum.List.Select(c => Tuple.Create(c.Value, c.Name)).ToList();
schema.Example = new OpenApiString(string.Join(',', list));
schema.Description = string.Join(',', list);
}
}
}
```
```
services.AddSwaggerGen(swagger =>
{
swagger.SchemaFilter();
});
```
**Behaviour without any schema filter:**



**Behaviour with schema filter:**



Contributor guide
Research direction
Reproduce the issue using the TestSmartEnum, TestEnum, SmartEnumSchemaFilter, and AddSwaggerGen configuration shown in the report. Start by comparing the generated Swashbuckle schema for TestSmartEnum with the regular enum, then determine what is needed for the smart enum's values to appear like enum values. Done means the generated API schema exposes the smart enum values without requiring the custom filter.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- api
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100