Azure / autorest.csharp

Extension for AutoRest (https://github.com/Azure/autorest) that generates C# code
MIT License
142 stars 166 forks source link

C# Capitalize words in enum member names? #201

Open kevinoid opened 4 years ago

kevinoid commented 4 years ago

Both Autorest 2.0.4407 and 3.0.5231 appear to generate member names for enums by removing invalid characters, then capitalizing the resulting word. This results in member names which are difficult to read when the value is all lower-case. For example:

swagger: "2.0"
info:
  version: "1.0.0"
  title: Enum Variable Name Example
paths: {}
definitions:
  Ethnicity:
    type: string
    enum:
    - american indian and alaska native
    - asian
    - black or african american
    - hispanic or latino
    - native hawaiian or other pacific islander
    - two or more races
    - white
    x-ms-enum:
      name: Ethnicity

generates

/// <summary>
/// Defines values for Ethnicity.
/// </summary>
[JsonConverter(typeof(StringEnumConverter))]
public enum Ethnicity
{
    [EnumMember(Value = "american indian and alaska native")]
    Americanindianandalaskanative,
    [EnumMember(Value = "asian")]
    Asian,
    [EnumMember(Value = "black or african american")]
    Blackorafricanamerican,
    [EnumMember(Value = "hispanic or latino")]
    Hispanicorlatino,
    [EnumMember(Value = "native hawaiian or other pacific islander")]
    Nativehawaiianorotherpacificislander,
    [EnumMember(Value = "two or more races")]
    Twoormoreraces,
    [EnumMember(Value = "white")]
    White
}

Is there any chance this could be changed to align with the Capitalization Rules for Identifiers in the Microsoft Guidelines "To differentiate words in an identifier, capitalize the first letter of each word in the identifier."?

Thanks for considering, Kevin

P.S. There are easy workarounds available (e.g. defining name in x-ms-enum.values, so this is mostly just a convenience.

kevinoid commented 4 years ago

Note: The current conversion is similarly problematic for all-uppercase values:

All-Uppercase OpenAPI 2 Example ```yaml swagger: "2.0" info: version: "1.0.0" title: Enum Variable Name Example paths: {} definitions: Ethnicity: type: string enum: - AMERICAN INDIAN AND ALASKA NATIVE - ASIAN - BLACK OR AFRICAN AMERICAN - HISPANIC OR LATINO - NATIVE HAWAIIAN OR OTHER PACIFIC ISLANDER - TWO OR MORE RACES - WHITE x-ms-enum: name: Ethnicity ```
Generated C# Code ```csharp [JsonConverter(typeof(StringEnumConverter))] public enum Ethnicity { [EnumMember(Value = "AMERICAN INDIAN AND ALASKA NATIVE")] AMERICANINDIANANDALASKANATIVE, [EnumMember(Value = "ASIAN")] ASIAN, [EnumMember(Value = "BLACK OR AFRICAN AMERICAN")] BLACKORAFRICANAMERICAN, [EnumMember(Value = "HISPANIC OR LATINO")] HISPANICORLATINO, [EnumMember(Value = "NATIVE HAWAIIAN OR OTHER PACIFIC ISLANDER")] NATIVEHAWAIIANOROTHERPACIFICISLANDER, [EnumMember(Value = "TWO OR MORE RACES")] TWOORMORERACES, [EnumMember(Value = "WHITE")] WHITE } ```