[csharp] fix enum serialization of first value (#6873)

* [csharp] Treat enum models consistently

C# works differently from most languages in that enums are not
considered objects. This means default(EnumType) will choose a default
of the first enum option. This isn't desirable because it breaks the
required = false functionality of swagger specs, which defines a
property which isn't required to exist in the message body.

Rather than force consumers to use enum values such as UNSPECIFIED, UNKNOWN,
NOT_SET, etc... we can treat enums as primitives. This means any
non-required enum will become Nullable<EnumType> regardless of whether
it is defined as an inline enum or a referenced enum model.

* Categorizing C# integration test for enums as general

* [csharp] Remove enum-ref integration test

* [csharp] Clean up general enum support integration test, validate different enum usage cases.

* [csharp][all] Assign one-based int to string enums

The EmitDefaultValue=false for string based enums will prevent the first
enum value from being serialized, because as 0 it is considered the
default.

This commit assigns an explicit numerical value to all non-integer
enums. This assignment has no effect on the
serialization/deserialization values, and only assigns the compiled
integer.

NOTE: This will have an effect of requiring recompilation of any code
that references the client/server models. This is because:

    public enum Pet { Available }

Source files referencing Pet.Available as defined above will have a
constant 0 in place of the enum value.

    public enum Pet { Available = 1 }

Source files referencing Pet.Available as defined above will have a
constant 1 in place of the enum value.

After compilation, Pet.Available in both instances lose their semantic
values and refer to the byte representation of their integral values.

For more info, see
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/enum
This commit is contained in:
Jim Schubert
2017-11-13 03:02:03 -05:00
committed by William Cheng
parent 9ca9887de4
commit 28e2fcee78
9 changed files with 28 additions and 28 deletions

View File

@@ -8,6 +8,6 @@
/// Enum {{name}} for {{{value}}}
/// </summary>
[EnumMember(Value = {{#isLong}}"{{/isLong}}{{#isInteger}}"{{/isInteger}}{{#isFloat}}"{{/isFloat}}{{#isDouble}}"{{/isDouble}}{{{value}}}{{#isLong}}"{{/isLong}}{{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}}{{#isFloat}}"{{/isFloat}})]
{{name}}{{#isLong}} = {{{value}}}{{/isLong}}{{#isInteger}} = {{{value}}}{{/isInteger}}{{^-last}},
{{name}}{{#isLong}} = {{{value}}}{{/isLong}}{{#isInteger}} = {{{value}}}{{/isInteger}}{{^isInteger}} = {{-index}}{{/isInteger}}{{^-last}},
{{/-last}}{{/enumVars}}{{/allowableValues}}
}

View File

@@ -12,6 +12,6 @@
/// Enum {{name}} for {{{value}}}
/// </summary>
[EnumMember(Value = {{#isLong}}"{{/isLong}}{{#isInteger}}"{{/isInteger}}{{#isFloat}}"{{/isFloat}}{{#isDouble}}"{{/isDouble}}{{{value}}}{{#isLong}}"{{/isLong}}{{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}}{{#isFloat}}"{{/isFloat}})]
{{name}}{{#isLong}} = {{{value}}}{{/isLong}}{{#isInteger}} = {{{value}}}{{/isInteger}}{{^-last}},
{{name}}{{#isLong}} = {{{value}}}{{/isLong}}{{#isInteger}} = {{{value}}}{{/isInteger}}{{^isInteger}} = {{-index}}{{/isInteger}}{{^-last}},
{{/-last}}{{/enumVars}}{{/allowableValues}}
}

View File

@@ -12,6 +12,6 @@
/// Enum {{name}} for {{{value}}}
/// </summary>
[EnumMember(Value = {{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}}{{{value}}}{{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}})]
{{name}}{{#isInteger}} = {{{value}}}{{/isInteger}}{{^-last}},
{{name}}{{#isInteger}} = {{{value}}}{{/isInteger}}{{^isInteger}} = {{-index}}{{/isInteger}}{{^-last}},
{{/-last}}{{/enumVars}}{{/allowableValues}}
}

View File

@@ -13,7 +13,7 @@
/// Enum {{name}} for {{{value}}}
/// </summary>
[EnumMember(Value = {{#isLong}}"{{/isLong}}{{#isInteger}}"{{/isInteger}}{{#isFloat}}"{{/isFloat}}{{#isDouble}}"{{/isDouble}}{{{value}}}{{#isLong}}"{{/isLong}}{{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}}{{#isFloat}}"{{/isFloat}})]
{{name}}{{#isLong}} = {{{value}}}{{/isLong}}{{#isInteger}} = {{{value}}}{{/isInteger}}{{^-last}},
{{name}}{{#isLong}} = {{{value}}}{{/isLong}}{{#isInteger}} = {{{value}}}{{/isInteger}}{{^isInteger}} = {{-index}}{{/isInteger}}{{^-last}},
{{/-last}}{{/enumVars}}{{/allowableValues}}
}
{{/isContainer}}

View File

@@ -4,7 +4,7 @@
public enum {{>innerApiEnumName}}
{
{{#allowableValues}}
{{#values}} {{&.}}{{^-last}}, {{/-last}}
{{#values}} {{&.}}{{^isInteger}} = {{-index}}{{/isInteger}}{{^-last}}, {{/-last}}
{{/values}}
{{/allowableValues}}
};

View File

@@ -10,6 +10,6 @@
/// <summary>
/// Enum {{name}}
/// </summary>
{{name}}{{#isInteger}} = {{{value}}}{{/isInteger}}{{^-last}},
{{name}}{{#isInteger}} = {{{value}}}{{/isInteger}}{{^isInteger}} = {{-index}}{{/isInteger}}{{^-last}},
{{/-last}}{{/enumVars}}{{/allowableValues}}
}
}

View File

@@ -41,43 +41,43 @@ namespace IO.Swagger.Model
/// Enum Sun for "sun"
/// </summary>
[EnumMember(Value = "sun")]
Sun,
Sun = 1,
/// <summary>
/// Enum Mon for "mon"
/// </summary>
[EnumMember(Value = "mon")]
Mon,
Mon = 2,
/// <summary>
/// Enum Tue for "tue"
/// </summary>
[EnumMember(Value = "tue")]
Tue,
Tue = 3,
/// <summary>
/// Enum Wed for "wed"
/// </summary>
[EnumMember(Value = "wed")]
Wed,
Wed = 4,
/// <summary>
/// Enum Thu for "thu"
/// </summary>
[EnumMember(Value = "thu")]
Thu,
Thu = 5,
/// <summary>
/// Enum Fri for "fri"
/// </summary>
[EnumMember(Value = "fri")]
Fri,
Fri = 6,
/// <summary>
/// Enum Sat for "sat"
/// </summary>
[EnumMember(Value = "sat")]
Sat
Sat = 7
}
/// <summary>

View File

@@ -41,43 +41,43 @@ namespace IO.Swagger.Model
/// Enum Sun for "sun"
/// </summary>
[EnumMember(Value = "sun")]
Sun,
Sun = 1,
/// <summary>
/// Enum Mon for "mon"
/// </summary>
[EnumMember(Value = "mon")]
Mon,
Mon = 2,
/// <summary>
/// Enum Tue for "tue"
/// </summary>
[EnumMember(Value = "tue")]
Tue,
Tue = 3,
/// <summary>
/// Enum Wed for "wed"
/// </summary>
[EnumMember(Value = "wed")]
Wed,
Wed = 4,
/// <summary>
/// Enum Thu for "thu"
/// </summary>
[EnumMember(Value = "thu")]
Thu,
Thu = 5,
/// <summary>
/// Enum Fri for "fri"
/// </summary>
[EnumMember(Value = "fri")]
Fri,
Fri = 6,
/// <summary>
/// Enum Sat for "sat"
/// </summary>
[EnumMember(Value = "sat")]
Sat
Sat = 7
}
/// <summary>

View File

@@ -35,43 +35,43 @@ namespace IO.Swagger.Model
/// Enum Sun for "sun"
/// </summary>
[EnumMember(Value = "sun")]
Sun,
Sun = 1,
/// <summary>
/// Enum Mon for "mon"
/// </summary>
[EnumMember(Value = "mon")]
Mon,
Mon = 2,
/// <summary>
/// Enum Tue for "tue"
/// </summary>
[EnumMember(Value = "tue")]
Tue,
Tue = 3,
/// <summary>
/// Enum Wed for "wed"
/// </summary>
[EnumMember(Value = "wed")]
Wed,
Wed = 4,
/// <summary>
/// Enum Thu for "thu"
/// </summary>
[EnumMember(Value = "thu")]
Thu,
Thu = 5,
/// <summary>
/// Enum Fri for "fri"
/// </summary>
[EnumMember(Value = "fri")]
Fri,
Fri = 6,
/// <summary>
/// Enum Sat for "sat"
/// </summary>
[EnumMember(Value = "sat")]
Sat
Sat = 7
}
}