Swashbuckle.aspnetcore: StringEnumConverter does not work in "5.0.0-rc5"

Created on 8 Jan 2020  路  10Comments  路  Source: domaindrivendev/Swashbuckle.AspNetCore

Hello, I've been testing the latest release candidate version "5.0.0-rc4" and "5.0.0-rc5", and I have detected a bug.

In "rc5" the string enum conversion to string does not work.

I have the same conversion configuration in _ConfigureServices_:

image

Result schema in rc4:

image

Result schema in rc5:

image

Most helpful comment

As System.Text.Json is now the default serializer for ASP.NET Core, Swashbuckle honors it's options/attributes by default as well. To honor the Newtonsoft settings/attributes instead, you need to install a separate Swashbuckle package and opt-in. See https://github.com/domaindrivendev/Swashbuckle.AspNetCore#systemtextjson-stj-vs-newtonsoft

All 10 comments

As System.Text.Json is now the default serializer for ASP.NET Core, Swashbuckle honors it's options/attributes by default as well. To honor the Newtonsoft settings/attributes instead, you need to install a separate Swashbuckle package and opt-in. See https://github.com/domaindrivendev/Swashbuckle.AspNetCore#systemtextjson-stj-vs-newtonsoft

I have the same problem. The difference is I have separate property attribute for viewmodel [JsonConverter(typeof(StringEnumConverter))]

And 'services.AddSwaggerGenNewtonsoftSupport();' is used

Did you call services.AddSwaggerGenNewtonsoftSupport() after you call services.AddSwaggerGen()? I've been able to get mine to work using these via this:

builder.AddNewtonsoftJson(options =>
{
    var converter = new StringEnumConverter(namingStrategy: new CamelCaseNamingStrategy());
    options.SerializerSettings.Converters.Add(converter);
});

Yes, the code:

services.AddSwaggerGen(c =>
            {
                c.CustomSchemaIds(r => r.FullName);
            });
            services.AddSwaggerGenNewtonsoftSupport();

and I don't use camelCase for enums, but general camel-case resolver

.AddNewtonsoftJson(opt =>
                {
                    var resolver = new CamelCasePropertyNamesContractResolver();
                    resolver.NamingStrategy.ProcessDictionaryKeys = true;
                    opt.SerializerSettings.ContractResolver = resolver;
                });

Hi @josalper , @Transmuter
Swashbuckle.AspNetCore 5.0.0 stable version was released several hours ago. I tested this issue and it works for me.
You need
services.AddSwaggerGenNewtonsoftSupport();
and
.AddNewtonsoftJson( { options.SerializerSettings.Converters.Add(new StringEnumConverter()); })
Thanks @domaindrivendev !

Thank you for fixes! Yes, it works for
.AddNewtonsoftJson( { options.SerializerSettings.Converters.Add(new StringEnumConverter()); })
Unfortunately, property attribute [JsonConverter(typeof(StringEnumConverter))] still has problem

While global converters (i.e. via options) and type-level converters (i.e. via JsonConverter on the enum definition) will work, you're correct that the property-level attribute will not work. As it happens, this would be a non-trivial feature to support.

With that said, I can't think of too many use cases where you would want to serialize an enum one way for a certain property and a different way for another. Perhaps you could elaborate on your particular use case. If nothing else, it would help me understand your needs a little better and allow me to prioritize making the tricky enhancement accordingly.

My case is a big project with much legacy code. So, the problem with attribute is not critical, but requires some refactoring. Anyway, I'm glad that there is a way to solve the original issue, thank you very much!

@IlyaUhlianitsa it's helped for me but services.AddSwaggerGenNewtonsoftSupport() need add after services.AddSwaggerGen()

I have tested it in the final version and it works!

Closing....

Was this page helpful?
0 / 5 - 0 ratings