domaindrivendev / Swashbuckle.AspNetCore

Swagger tools for documenting API's built on ASP.NET Core
MIT License
5.25k stars 1.31k forks source link

StringEnumConverter does not work in "5.0.0-rc5" #1459

Closed josalper closed 4 years ago

josalper commented 4 years ago

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

domaindrivendev commented 4 years ago

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

Transmuter commented 4 years ago

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

And 'services.AddSwaggerGenNewtonsoftSupport();' is used

kylepope-ge commented 4 years ago

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);
});
Transmuter commented 4 years ago

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;
                });
IlyaUhlianitsa commented 4 years ago

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 !

Transmuter commented 4 years ago

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

domaindrivendev commented 4 years ago

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.

Transmuter commented 4 years ago

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!

absentik commented 4 years ago

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

josalper commented 4 years ago

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

Closing....