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

Csharp : Generate enum with annotations #2154

Open
mdmota opened this issue May 3, 2019 · 8 comments
Open

Csharp : Generate enum with annotations #2154

mdmota opened this issue May 3, 2019 · 8 comments

Comments

@mdmota
Copy link

mdmota commented May 3, 2019

How to generate enum with annotations?

My original enum :

 public enum EnumSituacaoSuspensao
    {
        [Display(Name = "Success")]
        Successo= 0,
        [Display(Name = "Error")]
        Error= 1
    }

Enum generated:

 [System.Runtime.Serialization.EnumMember(Value = @"SUCCESS ")]
        SUCCESS = 0,
    
        [System.Runtime.Serialization.EnumMember(Value = @"ERROR")]
        ERROR = 1,
@RicoSuter
Copy link
Owner

What library are you using to generate the swagger/openapi spec?

@unchase
Copy link
Contributor

unchase commented May 6, 2019

If you use Swashbuckle to generate the swagger/openapi specification, I recommend:

  1. First use Unchase.Swashbuckle.AspNetCore.Extensions nuget-package for generating specification with enums with annotations as you want them;
  2. Then Unchase OpenAPI (Swagger) Connected Service for generating client code by specification.

See the README for details.

@mdmota
Copy link
Author

mdmota commented May 6, 2019

@RicoSuter
Copy link
Owner

Ok, interesting... :-)

My question now: What do you expect as output? Do you want that it preserves the display name, the field name and number? Because I think that is not really possible (ie. not describable) in OpenAPI/Swagger.

@RicoSuter
Copy link
Owner

If you generate the spec with NSwag (not Swashbuckle) it will always generate an x-enumNames property with the preserved names (openapi/swagger can only describe enum values and if they are serialized as numbers then the enum names are lost by default).

@unchase
Copy link
Contributor

unchase commented May 6, 2019

With Unchase.Swashbuckle.AspNetCore.Extensions nuget-package you can generate from:

[DataContract]
public enum Title
{
    [Description("None enum description")]
    [EnumMember]
    None = 0,

    [Description("Miss enum description")]
    [EnumMember]
    Miss,

    [Description("Mr enum description")]
    [EnumMember]
    Mr
}

This:

/// <summary>Sample Person title.
/// 0 = None (None enum description)
/// 1 = Miss (Miss enum description)
/// 2 = Mr (Mr enum description)</summary>
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "9.13.35.0 (Newtonsoft.Json v11.0.0.0)")]
public enum Title
{
    None = 0,
    
    Miss = 1,
    
    Mr = 2,

}

In specification looks like this:
Enum Description in Schema Parameters

After client code generating with NSwagStudio or Unchase OpenAPI (Swagger) Connected Service your annotation just will placed in summary-comments (not in attributes).

P.S. OpenAPI (Swagger) Connected Service by Dmitry Pavlov is the same, only Unchase OpenAPI (Swagger) Connected Service more flexible in customization like NSwagStudio.

@RicoSuter
Copy link
Owner

With NSwag you can also generate the summary with the GenerateEnumMappingDescription setting
RicoSuter/NJsonSchema@b6faffc

See #1993

@mdmota
Copy link
Author

mdmota commented May 6, 2019

I need Display name annotation to show name in select asp-items="Html.GetEnumSelectList()"

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

3 participants