1

I'm trying to generate a c# client from an AspNetCore Web API using NSwagStudio. The generated code contains a set of enums that, based on the attributes decorating them, are apparently flags for Json serialization - I can't locate where any of them are actually used. Several of these are generated with duplicate values, which makes the code not compile out of the box.

Example:

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "8.30.6304.31883")]
public enum ConstructorInfoMethodImplementationFlags
{
    _0 = 0,

    _0 = 0,

    _1 = 1,

    _2 = 2,

    _3 = 3,

    _3 = 3,

    _4 = 4,

    _4 = 4,

    _8 = 8,

    _16 = 16,

    _32 = 32,

    _64 = 64,

    _128 = 128,

    _256 = 256,

    _4096 = 4096,

    _65535 = 65535,

}

Is this a problem with the generator or something with my documentation? Is there a way to fix this without hand-editing the code?

Thanks.

Lamont
  • 41
  • 3
  • Can you create an issue on GitHub with a sample project to reproduce this? – Rico Suter Apr 19 '17 at 21:52
  • 1
    try editing your SwaggerConfig.cs file, uncomment this line: `c.DescribeAllEnumsAsStrings();` – Terry Kernan May 01 '17 at 12:45
  • +1 to Terry Kernan for this info. I am using swagger via Swashbuckle.AspNetCore.SwaggerGen in .net core and injecting swagger configuration in my Startup.cs like this: `services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" }); c.DescribeAllEnumsAsStrings(); });` – nzic Aug 25 '17 at 21:30

1 Answers1

0

Seems your enum in Web API project defined wrong. You have something like that:

[Flags]
public enum ConstructorInfoMethodImplementationFlags
{
    None,
    Method0 = 0,
    Method1 = 1,
    Method2 = 2
    Method3 = 4,
    ...
    AllMethods = 65535
}

So that translates to OAI specification in a way you don't expected:

"ConstructorInfoMethodImplementationFlags": {
          "format": "int32",
          "enum": [
            0,
            0,
            1,
            2,
            4
            ...
            65535
          ],
          "type": "integer"
        }

The solution here is assign integer values to every enum member.

Alexander Goldabin
  • 1,824
  • 15
  • 17