Enums marked with alternate values using [EnumMember(Value="...")] are not supported
See original GitHub issueBelow is copied from https://github.com/domaindrivendev/Swashbuckle/issues/562, because I am seeing the same issue in Swashbuckle.AspNetCore. It looks like someone attempted to fix this in https://github.com/domaindrivendev/Swashbuckle.AspNetCore/pull/414, but that PR is pretty out-of-date, and failing CI. That fix there appears to be replicating https://github.com/domaindrivendev/Swashbuckle/pull/563, but those changes are now out-of-date as well in Swashbuckle, as the enum schema is now generated using a Type extension.
Forgive the contrived example, but given the following code:
public class DefaultController : ApiController { [ResponseType(typeof(TestResult))] public IHttpActionResult Get(string id) { return this.Ok(new TestResult()); } } [DataContract] public class TestResult { [DataMember] public TestEnum Type { get; set; } } [DataContract] public enum TestEnum { [EnumMember(Value = "roger")] None = 0, [EnumMember(Value = "blake")] Something = 1, [EnumMember(Value = "sarah")] SomethingElse = 2 }
The API will return “roger”, “blake” or “sarah” depending on the enum value. Swashbuckle does not pick up on the alternative names and reports that the potential options are “None”, “Something” or “SomethingElse”.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:9 (2 by maintainers)
In case anybody needs a solution I solved it in the following way: For this enum:
Create an extension for the enumerator to convert the value to the EnumMember attribute value, here it is:
Then create a SchemaFilter class like this:
And register this SchemaFilter in the ConfigureServices where the AddSwaggerGen is called:
And there the EnumMember value is in the swagger UI spec
@elize-vdr’s solution does not work for me for 6.3.1, but it helped me to create a solution which works for me:
It basically ignores
EnumMember
values, and uses enum constant names as if theEnumMember
attribute is missing.