C# NSwag and swagger-codegen with Enums
Asked Answered
F

3

14

I have a .Net Core v2.1 Web API which uses NSwag to generate its Swagger Json.

I have a response model as such -

public class LoginResponse
{
    public LoginResult LoginResult { get; set; }
}

public enum LoginResult
{
    AwaitingEmailConfirmation = 0,
    Locked = 1,
    Failed = 2,
    Success = 3
}

Which generates the Swagger JSON of -

"definitions":{  
"LoginResponse":{  
   "type":"object",
   "additionalProperties":false,
   "required":[  
      "loginResult"
   ],
   "properties":{  
      "loginResult":{  
         "$ref":"#/definitions/LoginResult"
      }
   }
},
"LoginResult":{  
   "type":"integer",
   "description":"",
   "x-enumNames":[  
      "AwaitingEmailConfirmation",
      "Locked",
      "Failed",
      "Success"
   ],
   "enum":[  
      0,
      1,
      2,
      3
   ]
},

and when running swagger codegen on the JSON I get the following LoginResult model in my IO.Swagger project for C# (targetFramework 5.0 chosen) -

[JsonConverter(typeof(StringEnumConverter))]

public enum LoginResult
{

    /// <summary>
    /// Enum _0 for value: 0
    /// </summary>
    [EnumMember(Value = "0")]
    _0 = 1,

    /// <summary>
    /// Enum _1 for value: 1
    /// </summary>
    [EnumMember(Value = "1")]
    _1 = 2,

    /// <summary>
    /// Enum _2 for value: 2
    /// </summary>
    [EnumMember(Value = "2")]
    _2 = 3,

    /// <summary>
    /// Enum _3 for value: 3
    /// </summary>
    [EnumMember(Value = "3")]
    _3 = 4
  }

}

Could someone help describe how I get the enums to generate with the same names etc as the original LoginResult Model in the IO.Swagger generated client code using swagger-codegen?

Frightened answered 4/10, 2018 at 14:18 Comment(3)
See github.com/RSuter/NJsonSchema/wiki/EnumsBeauharnais
Can you just remove the numeric assignment in your original enum definition? It's not necessaryForetime
#36452968Bianca
A
9

You can use this code in your start up:

services.AddMvc(option => option.EnableEndpointRouting = false).AddJsonOptions(options =>
        {
            options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
        });
Amass answered 20/4, 2020 at 13:20 Comment(0)
C
6

Using Nswag 13.10.7 and .netcore 3.1

Edit your Startup.ConfigureServices to something like

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers()
                .AddJsonOptions(options =>
                {
                    options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
                });
             ...
         }
Comstock answered 6/3, 2021 at 14:38 Comment(0)
L
0

im using the 14.x versioon of NSwag. seems like thtat this issue happens as well. if i add the converters, as proposed - i still end up with crappy enum values generated.

Luckett answered 12/2 at 14:5 Comment(1)
This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From ReviewTraditional

© 2022 - 2024 — McMap. All rights reserved.