swagger-api / swagger-api/swagger-codegen
[Aspnetcore] - Newtonsoft json serializer not works in .net8
Open
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
When updating my project to the .NET8 version, Newtonsfot.Json serialization stopped working. To solve this problem, I had to put a global variable in the Startup.cs class:
services.AddControllers()
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
options.SerializerSettings.Converters.Add(new StringEnumConverter());
});
Template aspnetcore used to enum
/// <summary>
/// {{^description}}Gets or Sets {{{name}}}{{/description}}{{#description}}{{{description}}}{{/description}}
/// </summary>
{{#description}}
/// <value>{{{description}}}</value>
{{/description}}
{{#allowableValues}}{{#enumVars}}{{#-first}}{{#isString}}[JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]{{/isString}}{{/-first}}{{/enumVars}}{{/allowableValues}}
public enum {{#datatypeWithEnum}}{{.}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}
{
{{#allowableValues}}{{#enumVars}}
/// <summary>
/// Enum {{name}} for {{{value}}}
/// </summary>
{{#isString}}[EnumMember(Value = "{{{value}}}")]{{/isString}}
{{name}}{{^isString}} = {{{value}}}{{/isString}}{{#isString}} = {{-index}}{{/isString}}{{^-last}},
{{/-last}}{{/enumVars}}{{/allowableValues}}
}
Swagger-codegen version
Last version
Swagger declaration file content or url
Code used exemple
[JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
public enum StatusEnum
{
/// <summary>
/// Enum AtivoEnum for Ativo
/// </summary>
[EnumMember(Value = "Ativo")]
AtivoEnum = 1,
/// <summary>
/// Enum UtilizadoEnum for Utilizado
/// </summary>
[EnumMember(Value = "Utilizado")]
UtilizadoEnum = 2,
/// <summary>
/// Enum CanceladoEnum for Cancelado
/// </summary>
[EnumMember(Value = "Cancelado")]
CanceladoEnum = 3
}
/// <summary>
/// Situação da recuperação da senha.
/// </summary>
/// <value>Situação da recuperação da senha.</value>
[DataMember(Name="Status")]
public StatusEnum? Status { get; set; }
For example, in the code above, when the Status object is serialized using the JSON format, the value of the "status" field is converted to the string "1" instead of the string "Ativo" like before.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the ASP.NET Core enum template shown in the issue and the serialization setup in Startup.cs. Reproduce serialization of the provided StatusEnum example under .NET 8 with Newtonsoft.Json, then verify that the generated output uses "Ativo" rather than "1"; done means the regression is corrected and the behavior is covered by a test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100