Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit acb596c

Browse files
committed
Don't use serialized EnumMember as db values, use enum name instead
1 parent 36cca9d commit acb596c

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

src/ServiceStack.OrmLite/Converters/SpecialConverters.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using System.Collections.Concurrent;
44
using System.Collections.Generic;
55
using System.Data;
6+
using System.Reflection;
7+
using System.Runtime.Serialization;
68
using ServiceStack.DataAnnotations;
79
#if NETSTANDARD2_0
810
using System.Globalization;
@@ -14,7 +16,8 @@ public enum EnumKind
1416
{
1517
String,
1618
Int,
17-
Char
19+
Char,
20+
EnumMember,
1821
}
1922

2023
public class EnumConverter : StringConverter
@@ -32,7 +35,9 @@ public static EnumKind GetEnumKind(Type enumType)
3235
? EnumKind.Int
3336
: enumType.HasAttributeCached<EnumAsCharAttribute>()
3437
? EnumKind.Char
35-
: EnumKind.String;
38+
: HasEnumMembers(enumType)
39+
? EnumKind.EnumMember
40+
: EnumKind.String;
3641

3742
Dictionary<Type, EnumKind> snapshot, newCache;
3843
do
@@ -109,6 +114,9 @@ public override object ToDbValue(Type fieldType, object value)
109114
value = Enum.ToObject(fieldType, enumValue);
110115
}
111116

117+
if (enumKind == EnumKind.EnumMember) // Don't use serialized Enum Value
118+
return value.ToString();
119+
112120
var enumString = DialectProvider.StringSerializer.SerializeToString(value);
113121
return enumString != null && enumString != "null"
114122
? enumString.Trim('"')
@@ -141,6 +149,18 @@ public static bool IsIntEnum(Type fieldType)
141149
return isIntEnum;
142150
}
143151

152+
public static bool HasEnumMembers(Type enumType)
153+
{
154+
var enumMembers = enumType.GetFields(BindingFlags.Public | BindingFlags.Static);
155+
foreach (var fi in enumMembers)
156+
{
157+
var enumMemberAttr = fi.FirstAttribute<EnumMemberAttribute>();
158+
if (enumMemberAttr?.Value != null)
159+
return true;
160+
}
161+
return false;
162+
}
163+
144164
public override object FromDbValue(Type fieldType, object value)
145165
{
146166
var enumKind = GetEnumKind(fieldType);

tests/ServiceStack.OrmLite.MySql.Tests/EnumTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Linq;
2+
using System.Runtime.Serialization;
23
using NUnit.Framework;
34
using ServiceStack.DataAnnotations;
45

tests/ServiceStack.OrmLite.Tests/EnumTests.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
22
using System.Linq;
3+
using System.Runtime.Serialization;
34
using NUnit.Framework;
45
using ServiceStack.DataAnnotations;
56
using ServiceStack.Logging;
@@ -453,6 +454,21 @@ public void Can_select_enum_using_tuple()
453454

454455
Assert.That(rows[0].someEnum, Is.EqualTo(SomeEnum.Value2));
455456
}
457+
458+
[Test]
459+
public void Does_insert_types_with_EnumMembers()
460+
{
461+
OrmLiteUtils.PrintSql();
462+
using var db = OpenDbConnection();
463+
db.DropAndCreateTable<TypeWithEnumMember>();
464+
465+
db.Insert(new TypeWithEnumMember {Id = 1, WorkflowType = WorkflowType.SalesInvoice});
466+
db.Insert(new TypeWithEnumMember {Id = 2, WorkflowType = WorkflowType.PurchaseInvoice});
467+
468+
var results = db.Select<TypeWithEnumMember>().ToDictionary(x => x.Id);
469+
Assert.That(results[1].WorkflowType, Is.EqualTo(WorkflowType.SalesInvoice));
470+
Assert.That(results[2].WorkflowType, Is.EqualTo(WorkflowType.PurchaseInvoice));
471+
}
456472
}
457473

458474
[EnumAsChar]
@@ -545,4 +561,21 @@ public class TypeWithNullableEnum
545561
public SomeEnum EnumValue { get; set; }
546562
public SomeEnum? NullableEnumValue { get; set; }
547563
}
564+
565+
public enum WorkflowType
566+
{
567+
Unknown,
568+
[EnumMember(Value = "Sales Invoices")]
569+
SalesInvoice,
570+
[EnumMember(Value = "Purchase Invoices")]
571+
PurchaseInvoice,
572+
[EnumMember(Value = "Supplier Statement")]
573+
SupplierStatement
574+
}
575+
576+
public class TypeWithEnumMember
577+
{
578+
public int Id { get; set; }
579+
public WorkflowType WorkflowType { get; set; }
580+
}
548581
}

0 commit comments

Comments
 (0)