DescribeAllEnumsAsStrings is obsolete

See original GitHub issue

In Swashbuckle.AspNetCore.SwaggerGen-5.0.0-rc3, the DescribeAllEnumsAsStrings method is marked obsolete and to-be-deprecated because it will automatically be applied if that convention is present in the serializer. However, the functionality is only present if Newtonsoft.Json is used as the serializer. If using the new default Json serializer in .NET Core 3, the StringEnumConverter is not respected.

Reproduction Code

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers()
            .AddJsonOptions(x =>
            {
                x.JsonSerializerOptions.WriteIndented = !Environment.IsProduction();
                x.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
            });

            services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
                options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, @"MyApi.xml"));
            });
    }

    ...
}

public enum Result
{
    Success,
    Failed
}

image

Edit: Completely understand that this is obviously not removed from the API yet, and it does work if I add the DescribeAllEnumsAsStrings() and ignore the warning. More it’s just an observation of functionality being reported obsolete that may be required if using the default serializer and wondering if there is a plan to implement that convention for the new serializer?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:26
  • Comments:21 (4 by maintainers)

github_iconTop GitHub Comments

69reactions
PauloVetorcommented, Jan 22, 2020

I solved it using ShemaFilter like this:

public class EnumSchemaFilter : ISchemaFilter
{
    public void Apply(OpenApiSchema model, SchemaFilterContext context)
    {
        if (context.Type.IsEnum)
        {
            model.Enum.Clear();
            Enum.GetNames(context.Type)
                .ToList()
                .ForEach(n => model.Enum.Add(new OpenApiString(n)));
            }
        }
    }
}

And in Startup.cs:

services.AddSwaggerGen(options =>
{
    options.SchemaFilter<EnumSchemaFilter>();
}
27reactions
ggonzalez94commented, Feb 14, 2020

Seems like this is fixed in version 5.0.0. Am I missing something?

Just added the following code in ConfigureServices, no need to set DescribeAllEnumsAsStrings in Swagger config

services.AddControllersWithViews()
                .AddJsonOptions(options => 
                options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()));

And heres the generated schema: image

Read more comments on GitHub >

github_iconTop Results From Across the Web

Net 6 Swagger how to display enums as strings
DescribeAllEnumsAsStrings (); but I don't have it on the .net 6. program code example. I try adding options.JsonSerializerOptions.Converters.
Read more >
How to show Enums as Strings in Swashbuckle.AspNetCore
Once upon a time you could use DescribeAllEnumsAsStrings() to display enums as string instead of numbers in ... Unfortunately, that method is now...
Read more >
camelCase Enums in Swashbuckle
However, in newer versions of Swashbuckle, those methods have been deprecated and they ask you to use the built-in features of your ...
Read more >
How to use OpenAPI in Minimal API apps
To mark an endpoint as obsolete, set the Deprecated property on the ... A parameter type can describe its own annotation by implementing...
Read more >
[Swashbuckle and API Versioning] Obsolete attribute is not ...
Experiencing Obsolete action is not rendered as deprecated in Swagger UI? So you've upgraded your ASP.NET Core 2.1 app to 2.2 and you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found