question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Global StringEnumConverter no longer respected

See original GitHub issue

When 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:open
  • Created 4 years ago
  • Reactions:2
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
RicoSutercommented, Jan 17, 2020

image

1reaction
ThomasBaechlerConticommented, May 20, 2020

After integrating nswag generation into the build process, I suffer from the same problem. I use aspNetCoreToOpenApi with assemblyPaths.

The only workaround I found so far is annotating every enum with

[JsonConverter(typeof(StringEnumConverter))]
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found