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

Commit 561884d

Browse files
committed
Ensure Enums are saved as strings by default in all providers
1 parent b62b495 commit 561884d

File tree

2 files changed

+81
-26
lines changed

2 files changed

+81
-26
lines changed

src/ServiceStack.OrmLite/OrmLiteDialectProviderBase.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,13 @@ protected virtual object GetValue<T>(FieldDefinition fieldDef, object obj)
705705
}
706706
return OrmLiteConfig.DialectProvider.StringSerializer.SerializeToString(value);
707707
}
708+
if (fieldDef.FieldType.IsEnum)
709+
{
710+
var enumValue = OrmLiteConfig.DialectProvider.StringSerializer.SerializeToString(value);
711+
return enumValue != null
712+
? enumValue.Trim('"')
713+
: null;
714+
}
708715
if (fieldDef.FieldType == typeof(TimeSpan))
709716
{
710717
var timespan = (TimeSpan)value;

tests/ServiceStack.OrmLite.Tests/EnumTests.cs

Lines changed: 74 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@ public void CanStoreEnumValue()
2828
[Test]
2929
public void CanGetEnumValue()
3030
{
31-
using (var con = OpenDbConnection())
31+
using (var db = OpenDbConnection())
3232
{
33-
con.CreateTable<TypeWithEnum>(true);
33+
db.DropAndCreateTable<TypeWithEnum>();
34+
3435
var obj = new TypeWithEnum { Id = 1, EnumValue = SomeEnum.Value1 };
35-
con.Save(obj);
36-
var target = con.SingleById<TypeWithEnum>(obj.Id);
36+
db.Save(obj);
37+
var target = db.SingleById<TypeWithEnum>(obj.Id);
3738
Assert.AreEqual(obj.Id, target.Id);
3839
Assert.AreEqual(obj.EnumValue, target.EnumValue);
3940
}
@@ -42,31 +43,31 @@ public void CanGetEnumValue()
4243
[Test]
4344
public void CanQueryByEnumValue_using_select_with_expression()
4445
{
45-
using (var con = OpenDbConnection())
46+
using (var db = OpenDbConnection())
4647
{
47-
con.CreateTable<TypeWithEnum>(true);
48-
con.Save(new TypeWithEnum { Id = 1, EnumValue = SomeEnum.Value1 });
49-
con.Save(new TypeWithEnum { Id = 2, EnumValue = SomeEnum.Value1 });
50-
con.Save(new TypeWithEnum { Id = 3, EnumValue = SomeEnum.Value2 });
48+
db.DropAndCreateTable<TypeWithEnum>();
49+
db.Save(new TypeWithEnum { Id = 1, EnumValue = SomeEnum.Value1 });
50+
db.Save(new TypeWithEnum { Id = 2, EnumValue = SomeEnum.Value1 });
51+
db.Save(new TypeWithEnum { Id = 3, EnumValue = SomeEnum.Value2 });
5152

52-
var results = con.Select<TypeWithEnum>(q => q.EnumValue == SomeEnum.Value1);
53+
var results = db.Select<TypeWithEnum>(q => q.EnumValue == SomeEnum.Value1);
5354
Assert.That(results.Count, Is.EqualTo(2));
54-
results = con.Select<TypeWithEnum>(q => q.EnumValue == SomeEnum.Value2);
55+
results = db.Select<TypeWithEnum>(q => q.EnumValue == SomeEnum.Value2);
5556
Assert.That(results.Count, Is.EqualTo(1));
5657
}
5758
}
5859

5960
[Test]
6061
public void CanQueryByEnumValue_using_select_with_string()
6162
{
62-
using (var con = OpenDbConnection())
63+
using (var db = OpenDbConnection())
6364
{
64-
con.CreateTable<TypeWithEnum>(true);
65-
con.Save(new TypeWithEnum { Id = 1, EnumValue = SomeEnum.Value1 });
66-
con.Save(new TypeWithEnum { Id = 2, EnumValue = SomeEnum.Value1 });
67-
con.Save(new TypeWithEnum { Id = 3, EnumValue = SomeEnum.Value2 });
65+
db.DropAndCreateTable<TypeWithEnum>();
66+
db.Save(new TypeWithEnum { Id = 1, EnumValue = SomeEnum.Value1 });
67+
db.Save(new TypeWithEnum { Id = 2, EnumValue = SomeEnum.Value1 });
68+
db.Save(new TypeWithEnum { Id = 3, EnumValue = SomeEnum.Value2 });
6869

69-
var target = con.SelectFmt<TypeWithEnum>(
70+
var target = db.SelectFmt<TypeWithEnum>(
7071
"EnumValue".SqlColumn() + " = {0}", SomeEnum.Value1);
7172

7273
Assert.AreEqual(2, target.Count());
@@ -76,14 +77,14 @@ public void CanQueryByEnumValue_using_select_with_string()
7677
[Test]
7778
public void CanQueryByEnumValue_using_where_with_AnonType()
7879
{
79-
using (var con = OpenDbConnection())
80+
using (var db = OpenDbConnection())
8081
{
81-
con.CreateTable<TypeWithEnum>(true);
82-
con.Save(new TypeWithEnum { Id = 1, EnumValue = SomeEnum.Value1 });
83-
con.Save(new TypeWithEnum { Id = 2, EnumValue = SomeEnum.Value1 });
84-
con.Save(new TypeWithEnum { Id = 3, EnumValue = SomeEnum.Value2 });
82+
db.DropAndCreateTable<TypeWithEnum>();
83+
db.Save(new TypeWithEnum { Id = 1, EnumValue = SomeEnum.Value1 });
84+
db.Save(new TypeWithEnum { Id = 2, EnumValue = SomeEnum.Value1 });
85+
db.Save(new TypeWithEnum { Id = 3, EnumValue = SomeEnum.Value2 });
8586

86-
var target = con.Where<TypeWithEnum>(new { EnumValue = SomeEnum.Value1 });
87+
var target = db.Where<TypeWithEnum>(new { EnumValue = SomeEnum.Value1 });
8788

8889
Assert.AreEqual(2, target.Count());
8990
}
@@ -161,6 +162,40 @@ public void Updates_enum_flags_with_int_value()
161162
}
162163
}
163164

165+
[Test]
166+
public void Does_save_Enum_with_label_by_default()
167+
{
168+
using (var db = OpenDbConnection())
169+
{
170+
db.DropAndCreateTable<TypeWithEnum>();
171+
172+
db.Insert(new TypeWithEnum { Id = 1, EnumValue = SomeEnum.Value1 });
173+
db.Insert(new TypeWithEnum { Id = 2, EnumValue = SomeEnum.Value2 });
174+
175+
var row = db.SingleFmt<TypeWithEnum>(
176+
"EnumValue".SqlColumn() + " = {0}", "Value2");
177+
178+
Assert.That(row.Id, Is.EqualTo(2));
179+
}
180+
}
181+
182+
[Test]
183+
public void Can_save_Enum_as_Integers()
184+
{
185+
using (JsConfig.With(treatEnumAsInteger: true))
186+
using (var db = OpenDbConnection())
187+
{
188+
db.DropAndCreateTable<TypeWithEnumAsInt>();
189+
190+
db.Insert(new TypeWithEnumAsInt { Id = 1, EnumValue = SomeEnumAsInt.Value1 });
191+
db.Insert(new TypeWithEnumAsInt { Id = 2, EnumValue = SomeEnumAsInt.Value2 });
192+
193+
var row = db.SingleFmt<TypeWithEnumAsInt>(
194+
"EnumValue".SqlColumn() + " = {0}", "2");
195+
196+
Assert.That(row.Id, Is.EqualTo(2));
197+
}
198+
}
164199
}
165200

166201

@@ -179,9 +214,9 @@ public enum State
179214

180215
public enum SomeEnum
181216
{
182-
Value1,
183-
Value2,
184-
Value3
217+
Value1 = 1,
218+
Value2 = 2,
219+
Value3 = 3
185220
}
186221

187222
public class TypeWithEnum
@@ -190,6 +225,19 @@ public class TypeWithEnum
190225
public SomeEnum EnumValue { get; set; }
191226
}
192227

228+
public enum SomeEnumAsInt
229+
{
230+
Value1 = 1,
231+
Value2 = 2,
232+
Value3 = 3
233+
}
234+
235+
public class TypeWithEnumAsInt
236+
{
237+
public int Id { get; set; }
238+
public SomeEnumAsInt EnumValue { get; set; }
239+
}
240+
193241
[Flags]
194242
public enum FlagsEnum
195243
{

0 commit comments

Comments
 (0)