Skip to content

Commit ca2aa47

Browse files
authored
Fix MappingByCode discriminator string type throws (#3126)
Fixes #2717
1 parent 2e24925 commit ca2aa47

File tree

4 files changed

+156
-6
lines changed

4 files changed

+156
-6
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using System.Linq;
12+
using NHibernate.Cfg.MappingSchema;
13+
using NHibernate.Mapping.ByCode;
14+
using NUnit.Framework;
15+
using NHibernate.Linq;
16+
17+
namespace NHibernate.Test.NHSpecificTest.GH2717
18+
{
19+
using System.Threading.Tasks;
20+
[TestFixture]
21+
public class DiscriminatorStringTypeByCodeFixtureAsync : TestCaseMappingByCode
22+
{
23+
protected override HbmMapping GetMappings()
24+
{
25+
var mapper = new ModelMapper();
26+
mapper.Class<Entity>(rc =>
27+
{
28+
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
29+
rc.Discriminator(x => x.Type(NHibernateUtil.String));
30+
rc.Property(x => x.Name);
31+
rc.Abstract(true);
32+
});
33+
mapper.Subclass<Subclass>(rc => rc.DiscriminatorValue("xxx"));
34+
35+
return mapper.CompileMappingForAllExplicitlyAddedEntities();
36+
}
37+
38+
protected override void OnSetUp()
39+
{
40+
using (var session = OpenSession())
41+
using (var transaction = session.BeginTransaction())
42+
{
43+
var e1 = new Subclass { Name = "Bob" };
44+
session.Save(e1);
45+
46+
transaction.Commit();
47+
}
48+
}
49+
50+
protected override void OnTearDown()
51+
{
52+
using (var session = OpenSession())
53+
using (var transaction = session.BeginTransaction())
54+
{
55+
session.CreateQuery("delete from System.Object").ExecuteUpdate();
56+
transaction.Commit();
57+
}
58+
}
59+
60+
[Test]
61+
public async Task QueryOnSubclassWithStringDiscriminatorAsync()
62+
{
63+
using (var session = OpenSession())
64+
{
65+
var result = await ((from e in session.Query<Subclass>()
66+
where e.Name == "Bob"
67+
select e)
68+
.ToListAsync());
69+
70+
Assert.That(result.Count, Is.EqualTo(1));
71+
}
72+
}
73+
}
74+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System.Linq;
2+
using NHibernate.Cfg.MappingSchema;
3+
using NHibernate.Mapping.ByCode;
4+
using NUnit.Framework;
5+
6+
namespace NHibernate.Test.NHSpecificTest.GH2717
7+
{
8+
[TestFixture]
9+
public class DiscriminatorStringTypeByCodeFixture : TestCaseMappingByCode
10+
{
11+
protected override HbmMapping GetMappings()
12+
{
13+
var mapper = new ModelMapper();
14+
mapper.Class<Entity>(rc =>
15+
{
16+
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
17+
rc.Discriminator(x => x.Type(NHibernateUtil.String));
18+
rc.Property(x => x.Name);
19+
rc.Abstract(true);
20+
});
21+
mapper.Subclass<Subclass>(rc => rc.DiscriminatorValue("xxx"));
22+
23+
return mapper.CompileMappingForAllExplicitlyAddedEntities();
24+
}
25+
26+
protected override void OnSetUp()
27+
{
28+
using (var session = OpenSession())
29+
using (var transaction = session.BeginTransaction())
30+
{
31+
var e1 = new Subclass { Name = "Bob" };
32+
session.Save(e1);
33+
34+
transaction.Commit();
35+
}
36+
}
37+
38+
protected override void OnTearDown()
39+
{
40+
using (var session = OpenSession())
41+
using (var transaction = session.BeginTransaction())
42+
{
43+
session.CreateQuery("delete from System.Object").ExecuteUpdate();
44+
transaction.Commit();
45+
}
46+
}
47+
48+
[Test]
49+
public void QueryOnSubclassWithStringDiscriminator()
50+
{
51+
using (var session = OpenSession())
52+
{
53+
var result = (from e in session.Query<Subclass>()
54+
where e.Name == "Bob"
55+
select e)
56+
.ToList();
57+
58+
Assert.That(result.Count, Is.EqualTo(1));
59+
}
60+
}
61+
}
62+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
3+
namespace NHibernate.Test.NHSpecificTest.GH2717
4+
{
5+
class Entity
6+
{
7+
public virtual Guid Id { get; set; }
8+
public virtual string Name { get; set; }
9+
}
10+
11+
class Subclass : Entity
12+
{
13+
}
14+
}

src/NHibernate/Mapping/ByCode/Impl/DiscriminatorMapper.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,15 @@ public void Column(Action<IColumnMapper> columnMapper)
5757

5858
public void Type(IType persistentType)
5959
{
60-
if (persistentType != null)
61-
{
62-
discriminatorMapping.type = persistentType.Name;
63-
}
60+
if (persistentType is not IDiscriminatorType discriminatorType)
61+
throw new ArgumentOutOfRangeException(nameof(persistentType), "Expected type implementing " + nameof(IDiscriminatorType));
62+
63+
Type(discriminatorType);
6464
}
6565

6666
public void Type(IDiscriminatorType persistentType)
6767
{
68-
Type(persistentType.GetType());
68+
discriminatorMapping.type = persistentType.Name;
6969
}
7070

7171
public void Type<TPersistentType>() where TPersistentType : IDiscriminatorType
@@ -142,4 +142,4 @@ private void ResetColumnPlainValues()
142142
discriminatorMapping.formula = null;
143143
}
144144
}
145-
}
145+
}

0 commit comments

Comments
 (0)