Skip to content

Commit

Permalink
Fix CheckInverseList without property comparer to call invalid overlo…
Browse files Browse the repository at this point in the history
…ad (#688)

Closes #212
+semver:fix
  • Loading branch information
hazzik authored May 8, 2024
1 parent 4f88b7a commit 3f91650
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using FluentNHibernate.Mapping;
using FluentNHibernate.Testing.Cfg;
using NUnit.Framework;

namespace FluentNHibernate.Testing.DomainModel;

public class UnidirectionalOneToManyTester
{
[SetUp]
public void SetUp()
{
var properties = SQLiteFrameworkConfigurationFactory
.CreateStandardConfiguration()
.UseOuterJoin()
.ShowSql()
.InMemory()
.ToProperties();

var model = new PersistenceModel();
model.Add(typeof(OrderMap));
model.Add(typeof(LineItemMap));
_source = new SingleConnectionSessionSourceForSQLiteInMemoryTesting(properties, model);
_source.BuildSchema();
}

ISessionSource _source;

[Test]
public void ShouldHandleUnidirectionalCollections()
{
var order = new Order() { LineItems = new List<LineItem>() };
order.LineItems.Add(new LineItem());
new PersistenceSpecification<Order>(_source)
/* NHibernate.PropertyValueException: not-null property references
* a null or transient value LineItem._Order.LineItemsBackref */
.CheckInverseList(o => o.LineItems, order.LineItems, i => i.Id)
.VerifyTheMappings();
}
}

public class Order
{
public virtual Guid Id { get; set; }
public virtual ICollection<LineItem> LineItems { get; set; }
}

public class LineItem
{
public virtual Guid Id { get; set; }
}

public class OrderMap : ClassMap<Order>
{
public OrderMap()
{
Id(x => x.Id).GeneratedBy.GuidComb();
HasMany(x => x.LineItems)
.Not.Inverse()
.Not.KeyNullable()
.Not.KeyUpdate()
.Cascade.AllDeleteOrphan();
}
}

public class LineItemMap : ClassMap<LineItem>
{
public LineItemMap()
{
Id(x => x.Id).GeneratedBy.GuidComb();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public static PersistenceSpecification<T> CheckInverseList<T, TListElement>(this
// Because of the params keyword, the compiler can select this overload
// instead of the one above, even when no funcs are supplied in the method call.
if (propertiesToCompare is null || propertiesToCompare.Length == 0)
return spec.CheckList(expression, propertyValue, (IEqualityComparer)null);
return spec.CheckInverseList(expression, propertyValue, (IEqualityComparer)null);

return spec.CheckInverseList(expression, propertyValue, new FuncEqualityComparer<TListElement>(propertiesToCompare));
}
Expand Down

0 comments on commit 3f91650

Please sign in to comment.