Global StringEnumConverter no longer respected
See original GitHub issueWhen applying the Newtonsoft.Json StringEnumConverter
globally in an ASP.NET Core 3.1 application by doing the following:
services.AddControllers().AddNewtonsoftJson(options => options.SerializerSettings.Converters.Add(new StringEnumConverter()));
NSwag no longer generates the enum members as strings as it used to. If, however, the StringEnumConverter
is specified directly on an enum like this:
[JsonConverter(typeof(StringEnumConverter))]
public enum MyEnum
...
It works as expected.
This issue was introduced at some point after version 13.1.5 (currently tested on 13.2.1).
I’ve created a small test application to reproduce the issue here: https://github.com/Sharparam/nswag-stringenum-test
Simply clone down the repo, build it in Release configuration, and run NSwag with the included nswag.json
configuration.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:10 (6 by maintainers)
Top Results From Across the Web
StringEnumConverter works as an attribute but not globally
I have an .NET Core API, so I don't see how to configure Json.NET standalone on a more high level than the Startup....
Read more >StringEnumConverterWithDefault<TEnum> Class
Specialized StringEnumConverter that returns the default enum value instead of throwing if the string cannot be matched to an enum value during ...
Read more >Should you use Newtonsoft.Json or System.Text. ...
My understanding is that he did not work on System. ... Json is convenient, but problematic in a lot of ways (e.g. global...
Read more >[Solved]-JSON.NET - custom enum handling at deserialization-C
The converter handles the case when the underlying type is nullable as well and is invoked only when the type of property declared...
Read more >JSON .Net not respecting PreserveReferencesHandling on ...
I have the following JSON settings: _jsonSettings = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Auto, ConstructorHandling = ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
After integrating nswag generation into the build process, I suffer from the same problem. I use
aspNetCoreToOpenApi
withassemblyPaths
.The only workaround I found so far is annotating every enum with