Skip to content

Commit bc90e44

Browse files
committed
Merge pull request #403 from hazzik/NH-2738
NH-2738 - Fix getting default Enum value in AbstractEnumType
2 parents e8b9f03 + b23a3b6 commit bc90e44

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed
Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,49 @@
1+
using System;
12
using NHibernate.Mapping.ByCode;
23
using NUnit.Framework;
34

45
namespace NHibernate.Test.MappingByCode.IntegrationTests.NH2738
56
{
67
public enum MyEmptyEnum
78
{
8-
9+
}
10+
11+
public enum WierdEnum
12+
{
13+
MinusOne = -1,
14+
Zero = 0,
15+
PlusOne = 1
916
}
1017

1118
public class MyClass
1219
{
1320
public virtual int Id { get; set; }
14-
public MyEmptyEnum MyEmptyEnum { get; set; }
21+
public virtual MyEmptyEnum MyEmptyEnum { get; set; }
1522
}
23+
1624
public class Fixture
1725
{
1826
[Test]
19-
public void WhenMapEmptyEnumThenThrowsExplicitException()
27+
public void WhenMapEmptyEnumThenDoesNotThrowExplicitException()
2028
{
2129
var mapper = new ModelMapper();
2230
mapper.Class<MyClass>(rc =>
23-
{
24-
rc.Id(x => x.Id);
25-
rc.Property(x => x.MyEmptyEnum);
26-
});
31+
{
32+
rc.Id(x => x.Id);
33+
rc.Property(x => x.MyEmptyEnum);
34+
});
2735
var mappings = mapper.CompileMappingForAllExplicitlyAddedEntities();
2836
var conf = TestConfigurationHelper.GetDefaultConfiguration();
2937
conf.AddMapping(mappings);
38+
Assert.That(() => conf.BuildSessionFactory(), Throws.Nothing);
39+
}
3040

31-
Assert.That(() => conf.BuildSessionFactory(), Throws.TypeOf<MappingException>().And.Message.ContainsSubstring("MyEmptyEnum"));
41+
[Test]
42+
public void DefaultOfWierdEnumIsZero()
43+
{
44+
Assert.That(Activator.CreateInstance(typeof (WierdEnum)), Is.EqualTo(WierdEnum.Zero));
45+
Assert.That(Enum.ToObject(typeof (WierdEnum), 0), Is.EqualTo(WierdEnum.Zero));
46+
Assert.That(Enum.GetValues(typeof (WierdEnum)).GetValue(0), Is.EqualTo(WierdEnum.Zero)); // This depends on implementation and can be wrong
3247
}
3348
}
3449
}

src/NHibernate/Type/AbstractEnumType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ protected AbstractEnumType(SqlType sqlType,System.Type enumType)
2424
{
2525
throw new MappingException(enumType.Name + " did not inherit from System.Enum");
2626
}
27-
defaultValue = Enum.GetValues(enumType).GetValue(0);
27+
defaultValue = Enum.ToObject(enumType, 0);
2828
}
2929

3030
private readonly object defaultValue;

src/NHibernate/Type/TypeFactory.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -520,14 +520,7 @@ public static IType HeuristicType(string typeName, IDictionary<string, string> p
520520
var unwrapped = typeClass.UnwrapIfNullable();
521521
if (unwrapped.IsEnum)
522522
{
523-
try
524-
{
525-
return (IType) Activator.CreateInstance(typeof (EnumType<>).MakeGenericType(unwrapped));
526-
}
527-
catch (Exception e)
528-
{
529-
throw new MappingException(string.Format("Can't instantiate enum {0}; The enum can't be empty", typeClass.FullName), e);
530-
}
523+
return (IType) Activator.CreateInstance(typeof (EnumType<>).MakeGenericType(unwrapped));
531524
}
532525

533526
if (!typeClass.IsSerializable)

0 commit comments

Comments
 (0)