Swashbuckle.aspnetcore: camelCase enum fields with specified names not working as expected (v5.0.0)

Created on 8 Oct 2019  路  4Comments  路  Source: domaindrivendev/Swashbuckle.AspNetCore

As far as I can tell, the code that determines how to present enum names as part of the swagger output is in NewtonsoftApiModelResolver:

        private string GetConvertedEnumName(string memberName, StringEnumConverter stringEnumConverter)
        {
            return stringEnumConverter.NamingStrategy?.GetPropertyName(memberName, false) ?? memberName;
        }

But for enum fields that DO have a specified name via the EnumMember annotations (where the 2nd parameter to GetPropertyName logically should be true), they're incorrectly getting mangled by the naming strategy (e.g. you if using CamelCaseNamingStrategy and your EnumMember annotations are for ALL_UPPER_CASE you end up with rED, gREEN, bLUE etc.)
Any chance this can be fixed? (NB newtonsoft itself had this bug until recently).

Not a big priority personally at this stage as I'm not yet using a .NET Core 3 build and hence doesn't even use the NamingStrategy properly (even though I'm using a version of Newtonsoft that supports it).

Thanks

Dylan

Most helpful comment

I am working on adding Swashbuckle to an upgraded ASP.NET Core 3 API project. I am aiming to use the new System.Text.Json rather than Newtonsoft.Json (as is recommended now). Has this change been considered with Swashbuckle? I have the new JsonStringEnumConverter in my MVC set up, which works as expected wrt serialization. But Swashbuckle doesn't seem to understand this and all my enums come out as integers.

All 4 comments

Thanks again for the feedback here - should be addressed by ad91d72078e1e2276824b38e2483c1d8ab6e5376

I am working on adding Swashbuckle to an upgraded ASP.NET Core 3 API project. I am aiming to use the new System.Text.Json rather than Newtonsoft.Json (as is recommended now). Has this change been considered with Swashbuckle? I have the new JsonStringEnumConverter in my MVC set up, which works as expected wrt serialization. But Swashbuckle doesn't seem to understand this and all my enums come out as integers.

I don't understand why this is closed. I am using AddNewtonsoftJson() with a StringEnumConverter added and it still doesn't show any enum as anything other than an integer.

Please read the 5.0.0 release notes or this section of the readme: https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/master/README.md#systemtextjson-stj-vs-newtonsoft. The gist of it is that in the latest version SB honors System.Text.Json behavior by default instead of Newtonsoft. If you want it to honor NS behavior then you have to install a separate package and opt-in, as explained in the readme.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tibitoth picture tibitoth  路  3Comments

vanillajonathan picture vanillajonathan  路  3Comments

NinoFloris picture NinoFloris  路  3Comments

mrmartan picture mrmartan  路  3Comments

alasvant picture alasvant  路  3Comments