Prashant Tiwari
Prashant Tiwari

Reputation: 33

How to make Swagger generate model schema enum as string instead of int?

I have a Web API project build on .NET Core 3 and my Startup looks like this.

services.AddControllers(o => o.EnableEndpointRouting = false)
                .AddMvcOptions(o =>
                {
                    //Add mvc options here
                })
                .AddNewtonsoftJson(o =>
                {
                    o.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
                    o.SerializerSettings.Converters.Add(new StringEnumConverter());
                    o.SerializerSettings.DateFormatString = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.fff'Z'";
                })
                .SetCompatibilityVersion(CompatibilityVersion.Latest);

Swagger generates schema as:

status  MyEnumStatusinteger($int32)
Enum:
[ 1, 2, 3]

My C# enum looks like this:

public enum TimesheetWorkflowStatus
    {
      NotStarted = 1,
      InProgress = 2,
        Completed = 3
  }

Why is Swagger dcumenting this enum as int and not string? I tried using DescribeAllEnumsAsStrings but it has been rendered obsolete. Am I missing something here?

Upvotes: 1

Views: 1301

Answers (1)

Prashant Tiwari
Prashant Tiwari

Reputation: 33

I figured out the solution from here. I had to add services.AddSwaggerGenNewtonsoftSupport() alongwith services.AddSwaggerGen() to make it work.

Upvotes: 1

Related Questions