Skip to content

Commit

Permalink
Fix ByCode OneToOne XML serialization
Browse files Browse the repository at this point in the history
fix #3607
  • Loading branch information
fredericDelaporte committed Sep 29, 2024
1 parent ac2ff3a commit 87c72bb
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH3607/FixtureByCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using NHibernate.Cfg;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH3607
{
/// <summary>
/// By code mapping serialization failure since v5.4.1. Adapted from <see href="https://github.com/craigfowler/NHibernate.XmlConversionBug" />.
/// </summary>
[TestFixture]
public class FixtureByCode : TestCaseMappingByCode
{
protected override HbmMapping GetMappings()
{
var mapper = new ModelMapper();
mapper.AddMappings(new[] { typeof(OrderMapping), typeof(LineItemMapping), typeof(LineItemDataMapping) });
return mapper.CompileMappingForAllExplicitlyAddedEntities();
}

[Test]
public void SerializeMappingToXml()
{
var mapping = GetMappings();
string serialized = "";
Assert.That(() => serialized = mapping.AsString(), Throws.Nothing, "Mapping serialization failure");
var config = new Configuration();
Assert.That(() => config.AddXml(serialized), Throws.Nothing, "Configuration with serialized mapping has failed");
}
}
}
15 changes: 15 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH3607/LineItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace NHibernate.Test.NHSpecificTest.GH3607
{
public class LineItem
{
public virtual int Id { get; set; }

public virtual Order ParentOrder { get; set; }

public virtual string ItemName { get; set; }

public virtual decimal Amount { get; set; }

public virtual LineItemData Data { get; set; }
}
}
9 changes: 9 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH3607/LineItemData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace NHibernate.Test.NHSpecificTest.GH3607
{
public class LineItemData
{
public virtual LineItem LineItem { get; set; }

public virtual string Data { get; set; }
}
}
14 changes: 14 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH3607/LineItemDataMapping.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using NHibernate.Mapping.ByCode.Conformist;

namespace NHibernate.Test.NHSpecificTest.GH3607
{
public class LineItemDataMapping : ClassMapping<LineItemData>
{
public LineItemDataMapping()
{
OneToOne(x => x.LineItem, m => m.Constrained(true));

Property(x => x.Data);
}
}
}
21 changes: 21 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH3607/LineItemMapping.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using NHibernate.Mapping.ByCode;
using NHibernate.Mapping.ByCode.Conformist;

namespace NHibernate.Test.NHSpecificTest.GH3607
{
public class LineItemMapping : ClassMapping<LineItem>
{
public LineItemMapping()
{
Id(x => x.Id, m => m.Generator(new IdentityGeneratorDef()));

Property(x => x.ItemName);

Property(x => x.Amount);

ManyToOne(x => x.ParentOrder);

ManyToOne(x => x.Data);
}
}
}
14 changes: 14 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH3607/Order.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;

namespace NHibernate.Test.NHSpecificTest.GH3607
{
public class Order
{
public virtual int Id { get; set; }

public virtual DateTime CreatedDate { get; set; }

public virtual ISet<LineItem> Items { get; protected set; } = new HashSet<LineItem>();
}
}
22 changes: 22 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH3607/OrderMapping.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using NHibernate.Mapping.ByCode;
using NHibernate.Mapping.ByCode.Conformist;

namespace NHibernate.Test.NHSpecificTest.GH3607
{
public class OrderMapping : ClassMapping<Order>
{
public OrderMapping()
{
Table("`Order`");
Id(x => x.Id, m => m.Generator(new IdentityGeneratorDef()));

Property(x => x.CreatedDate);

Set(x => x.Items, m =>
{
m.Inverse(true);
m.OptimisticLock(true);
}, a => a.OneToMany());
}
}
}
4 changes: 4 additions & 0 deletions src/NHibernate/Cfg/MappingSchema/HbmOneToOne.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ public string Access
get { return access; }
}

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

Expand Down

0 comments on commit 87c72bb

Please sign in to comment.