Skip to content

Commit d6bd25a

Browse files
Fix ByCode OneToOne XML serialization
fix #3607
1 parent ac2ff3a commit d6bd25a

File tree

8 files changed

+125
-0
lines changed

8 files changed

+125
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System.Linq;
2+
using NHibernate.Cfg;
3+
using NHibernate.Cfg.MappingSchema;
4+
using NHibernate.Mapping.ByCode;
5+
using NUnit.Framework;
6+
7+
namespace NHibernate.Test.NHSpecificTest.GH3607
8+
{
9+
/// <summary>
10+
/// By code mapping serialization failure since v5.4.1. Adapted from <see href="https://github.com/craigfowler/NHibernate.XmlConversionBug" />.
11+
/// </summary>
12+
[TestFixture]
13+
public class FixtureByCode : TestCaseMappingByCode
14+
{
15+
protected override HbmMapping GetMappings()
16+
{
17+
var mapper = new ModelMapper();
18+
mapper.AddMappings(new[] { typeof(OrderMapping), typeof(LineItemMapping), typeof(LineItemDataMapping) });
19+
return mapper.CompileMappingForAllExplicitlyAddedEntities();
20+
}
21+
22+
[Test]
23+
public void SerializeMappingToXml()
24+
{
25+
var mapping = GetMappings();
26+
string serialized = "";
27+
Assert.That(() => serialized = mapping.AsString(), Throws.Nothing, "Mapping serialization failure");
28+
var config = new Configuration();
29+
Assert.That(() => config.AddXml(serialized), Throws.Nothing, "Configuration with serialized mapping has failed");
30+
}
31+
}
32+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace NHibernate.Test.NHSpecificTest.GH3607;
2+
3+
public class LineItem
4+
{
5+
public virtual int Id { get; set; }
6+
7+
public virtual Order ParentOrder { get; set; }
8+
9+
public virtual string ItemName { get; set; }
10+
11+
public virtual decimal Amount { get; set; }
12+
13+
public virtual LineItemData Data { get; set; }
14+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace NHibernate.Test.NHSpecificTest.GH3607;
2+
3+
public class LineItemData
4+
{
5+
public virtual LineItem LineItem { get; set; }
6+
7+
public virtual string Data { get; set; }
8+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using NHibernate.Mapping.ByCode.Conformist;
2+
3+
namespace NHibernate.Test.NHSpecificTest.GH3607;
4+
5+
public class LineItemDataMapping : ClassMapping<LineItemData>
6+
{
7+
public LineItemDataMapping()
8+
{
9+
OneToOne(x => x.LineItem, m => m.Constrained(true));
10+
11+
Property(x => x.Data);
12+
}
13+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using NHibernate.Mapping.ByCode;
2+
using NHibernate.Mapping.ByCode.Conformist;
3+
4+
namespace NHibernate.Test.NHSpecificTest.GH3607;
5+
6+
public class LineItemMapping : ClassMapping<LineItem>
7+
{
8+
public LineItemMapping()
9+
{
10+
Id(x => x.Id, m => m.Generator(new IdentityGeneratorDef()));
11+
12+
Property(x => x.ItemName);
13+
14+
Property(x => x.Amount);
15+
16+
ManyToOne(x => x.ParentOrder);
17+
18+
ManyToOne(x => x.Data);
19+
}
20+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace NHibernate.Test.NHSpecificTest.GH3607;
5+
6+
public class Order
7+
{
8+
public virtual int Id { get; set; }
9+
10+
public virtual DateTime CreatedDate { get; set; }
11+
12+
public virtual ISet<LineItem> Items { get; protected set; } = new HashSet<LineItem>();
13+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using NHibernate.Mapping.ByCode;
2+
using NHibernate.Mapping.ByCode.Conformist;
3+
4+
namespace NHibernate.Test.NHSpecificTest.GH3607;
5+
6+
public class OrderMapping : ClassMapping<Order>
7+
{
8+
public OrderMapping()
9+
{
10+
Table("`Order`");
11+
Id(x => x.Id, m => m.Generator(new IdentityGeneratorDef()));
12+
13+
Property(x => x.CreatedDate);
14+
15+
Set(x => x.Items, m =>
16+
{
17+
m.Inverse(true);
18+
m.OptimisticLock(true);
19+
}, a => a.OneToMany());
20+
}
21+
}

src/NHibernate/Cfg/MappingSchema/HbmOneToOne.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@ public string Access
1818
get { return access; }
1919
}
2020

21+
// 6.0 Todo : remove XmlIgnore after removing the setter. See #3607 fix.
22+
[XmlIgnore]
2123
public bool OptimisticLock
2224
{
2325
get => optimisticlock;
26+
// Since v5.4.10
27+
[Obsolete("Providing a setter for OptimisticLock was unintended and will be removed.")]
2428
set => optimisticlock = value;
2529
}
2630

0 commit comments

Comments
 (0)