Is there a way to display all enums as their string value in swagger instead of their int value?
I want to be able to submit POST actions and put enums according to their string value without having to look at the enum every time.
I tried DescribeAllEnumsAsStrings
but the server then receives strings instead of the enum value which is not what we're looking for.
Has anyone solved this?
Edit:
public class Letter
{
[Required]
public string Content {get; set;}
[Required]
[EnumDataType(typeof(Priority))]
public Priority Priority {get; set;}
}
public class LettersController : ApiController
{
[HttpPost]
public IHttpActionResult SendLetter(Letter letter)
{
// Validation not passing when using DescribeEnumsAsStrings
if (!ModelState.IsValid)
return BadRequest("Not valid")
..
}
// In the documentation for this request I want to see the string values of the enum before submitting: Low, Medium, High. Instead of 0, 1, 2
[HttpGet]
public IHttpActionResult GetByPriority (Priority priority)
{
}
}
public enum Priority
{
Low,
Medium,
High
}
There were a number of shortcomings I found in the other answers for what we were looking for, so I thought I'd supply my own take on this. We're using ASP.NET Core 3.1 with System.Text.Json, but our approach works irrespective of the JSON serializer used.
Our goal was to accept lower-camel-cased enum string values in both the ASP.NET Core API as well as document the same in Swagger. We're currently making use of
[DataContract]
and[EnumMember]
, so the approach is to take the lower-camel-cased value from the EnumMember value property and use that across the board.Our sample enum:
We'll use the EnumMember values in Swashbuckle by using an ISchemaFilter as in the following:
We're using a third-party NuGet package (GitHub repo) to ensure that this naming scheme is also utilized in ASP.NET Core. Configure it in Startup.cs within ConfigureServices with:
Finally, we need to register our ISchemaFilter in Swashbuckle, so also add the following also in ConfigureServices():
I wanted to use rory_za's answer in a .NET Core application, but I had to modify it a bit to make it work. Here is the implementation I came up with for .NET Core.
I also changed it so it doesn't assume the underlying type is
int
, and use new lines between the values for easier reading.Then add this to your
ConfigureServices
method in Startup.cs:I have found nice workaround Here:
@PauloVetor - solved it using ShemaFilter like this:
And in Startup.cs:
So I think I have a similar problem. I'm looking for swagger to generate enums along with the int -> string mapping. The API must accept the int. The swagger-ui matters less, what I really want is code generation with a "real" enum on the other side (android apps using retrofit in this case).
So from my research this ultimately seems to be a limit of the OpenAPI specification which Swagger uses. It's not possible to specify names and numbers for enums.
The best issue I've found to follow is https://github.com/OAI/OpenAPI-Specification/issues/681 which looks like a "maybe soon" but then Swagger would have to be updated, and in my case Swashbuckle as well.
For now my workaround has been to implement a document filter that looks for enums and populates the relevant description with the contents of the enum.
SwaggerAddEnumDescriptions.cs:
This results in something like the following on your swagger-ui so at least you can "see what you're doing":
For ASP.Net Core 3 with Newtonsoft JSON library
In Startup.cs/ConfigureServices():
This does not require a call to
options.DescribeAllEnumsAsStrings()
in AddSwaggerGen().For ASP.Net Core 3 with Microsoft JSON library
See @Bashir's answer, basically:
And as he describes, it currently does require
options.DescribeAllEnumsAsStrings()
.For ASP.Net Core 2
In Startup.cs/ConfigureServices():
This does not require a call to
options.DescribeAllEnumsAsStrings()
in AddSwaggerGen().Pre ASP.Net Core
Use the
DescribeAllEnumsAsStrings()
approach from the accepted answer.ASP NET SOLUTION
In my api docs one enum was still shown as int despite the property being marked with
StringEnumConverter
. We couldn't afford using the global setting for all enums mentioned above. Adding this line in SwaggerConfig solved the issue: