Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
josalper opened this issue Jan 8, 2020 · 10 comments
Closed

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

josalper opened this issue Jan 8, 2020 · 10 comments

Comments

@josalper
Copy link

josalper commented Jan 8, 2020

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
Copy link
Owner

domaindrivendev commented Jan 8, 2020

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
Copy link

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
Copy link

kylepope-ge commented Jan 9, 2020

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
Copy link

Transmuter commented Jan 9, 2020

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
Copy link

IlyaUhlianitsa commented Jan 14, 2020

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
Copy link

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
Copy link
Owner

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
Copy link

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
Copy link

absentik commented Jan 15, 2020

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

@josalper
Copy link
Author

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

Closing....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants