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

[Aspnetcore] - Newtonsoft json serializer not works in .net8 #12354

Open
engpauloandrade opened this issue Mar 8, 2024 · 0 comments
Open

[Aspnetcore] - Newtonsoft json serializer not works in .net8 #12354

engpauloandrade opened this issue Mar 8, 2024 · 0 comments

Comments

@engpauloandrade
Copy link

engpauloandrade commented Mar 8, 2024

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

1 participant