ardalis / ardalis/SmartEnum

SmartEnum with Swashbuckle doesn't show list of values like an enum

Open
#92 10 comments 0 reactions 0 assignees View on GitHub
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:**
![imagen](https://user-images.githubusercontent.com/4290346/90407192-804dd980-e0a6-11ea-9e6a-ca797b63115d.png)

![imagen](https://user-images.githubusercontent.com/4290346/90407474-dd498f80-e0a6-11ea-86a3-6eb053fa752b.png)

![imagen](https://user-images.githubusercontent.com/4290346/90407328-ad9a8780-e0a6-11ea-8fdc-ee7402f72fc5.png)

**Behaviour with schema filter:**
![imagen](https://user-images.githubusercontent.com/4290346/90406865-0a497280-e0a6-11ea-93ba-cd7880d71a6f.png)

![imagen](https://user-images.githubusercontent.com/4290346/90406933-23522380-e0a6-11ea-8f53-492d06b00eb7.png)

![imagen](https://user-images.githubusercontent.com/4290346/90407020-4086f200-e0a6-11ea-9588-959ec0938863.png)

Contributor guide

Open the contributing 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.