Skip to content
Permalink
Browse files

Don't allow EnumMember to control int enum values

  • Loading branch information...
mythz committed Jun 1, 2019
1 parent 612083b commit 635ff161e24d6e39816110f515f6af9f31f25023
Showing with 26 additions and 10 deletions.
  1. +19 −8 src/ServiceStack/NativeTypes/NativeTypesMetadata.cs
  2. +7 −2 tests/Check.ServiceModel/CodeGenTestTypes.cs
@@ -401,27 +401,38 @@ public MetadataType ToType(Type type)
metaType.EnumValues = new List<string>();

var isDefaultLayout = true;
var isIntEnum = JsConfig.TreatEnumAsInteger || type.IsEnumFlags();
var names = Enum.GetNames(type);
for (var i = 0; i < names.Length; i++)
{
var name = names[i];
metaType.EnumNames.Add(name);

var enumMember = GetEnumMember(type, name);

var value = enumMember.GetRawConstantValue();
var enumValue = Convert.ToInt64(value).ToString();

var enumMemberValue = enumMember.FirstAttribute<EnumMemberAttribute>()?.Value;
if (enumMemberValue != null)
if (isIntEnum)
{
enumValue = enumMemberValue;
isDefaultLayout = false;
}
metaType.EnumValues.Add(enumValue);
}
else
{
var enumMemberValue = enumMember.FirstAttribute<EnumMemberAttribute>()?.Value;
if (enumMemberValue != null)
{
isDefaultLayout = false;
metaType.EnumValues.Add(enumMemberValue);
}
else
{
metaType.EnumValues.Add(name);
}
}

if (enumValue != i.ToString())
isDefaultLayout = false;

metaType.EnumNames.Add(name);
metaType.EnumValues.Add(enumValue);
}

if (isDefaultLayout)
@@ -118,17 +118,22 @@ public enum EnumType

public enum EnumWithValues
{
None = 0,
[EnumMember(Value = "Value 1")]
Value1 = 1,
Value2 = 2
[Description("Value 2")]
Value2 = 2,
}

[Flags]
public enum EnumFlags
{
Value0 = 0,
[EnumMember(Value = "Value 1")]
Value1 = 1,
[Description("Value 2")]
Value2 = 2,
Value3 = 3,
Value3 = 4,
Value123 = Value1 | Value2 | Value3,
}

0 comments on commit 635ff16

Please sign in to comment.
You can’t perform that action at this time.