Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ByCode OneToOne XML serialization #3613

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
hazzik marked this conversation as resolved.
Show resolved Hide resolved
[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;
bahusoid marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down
Loading