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 lazy property handling with field accessors #3345

Merged
merged 5 commits into from
Jul 1, 2023
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
56 changes: 55 additions & 1 deletion src/NHibernate.Test/Async/LazyProperty/LazyPropertyFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
using System.Linq;
using NHibernate.Cfg;
using NHibernate.Intercept;
using NHibernate.Linq;
using NHibernate.Tuple.Entity;
using NUnit.Framework;
using NUnit.Framework.Constraints;
using NHibernate.Linq;

namespace NHibernate.Test.LazyProperty
{
Expand Down Expand Up @@ -69,6 +69,7 @@ protected override void OnSetUp()
Id = 1,
ALotOfText = "a lot of text ...",
Image = new byte[10],
NoSetterImage = new byte[10],
FieldInterceptor = "Why not that name?"
});
tx.Commit();
Expand Down Expand Up @@ -393,5 +394,58 @@ public async Task CanMergeTransientWithLazyPropertyInCollectionAsync()
Assert.That(book.Words.First().Content, Is.EqualTo(new byte[1] { 0 }));
}
}

[Test(Description = "GH-3333")]
public async Task GetLazyPropertyWithNoSetterAccessor_PropertyShouldBeInitializedAsync()
{
using (ISession s = OpenSession())
{
var book = await (s.GetAsync<Book>(1));
var image = book.NoSetterImage;
// Fails. Property remains uninitialized after it has been accessed.
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "NoSetterImage"), Is.True);
}
}

[Test(Description = "GH-3333")]
public async Task GetLazyPropertyWithNoSetterAccessorTwice_ResultsAreSameObjectAsync()
{
using (ISession s = OpenSession())
{
var book = await (s.GetAsync<Book>(1));
var image = book.NoSetterImage;
var sameImage = book.NoSetterImage;
// Fails. Each call to a property getter returns a new object.
Assert.That(ReferenceEquals(image, sameImage), Is.True);
}
}

[Test]
public async Task CanSetValueForLazyPropertyNoSetterAsync()
{
Book book;
using (ISession s = OpenSession())
{
book = await (s.GetAsync<Book>(1));
book.NoSetterImage = new byte[]{10};
}

Assert.That(NHibernateUtil.IsPropertyInitialized(book, nameof(book.NoSetterImage)), Is.True);
CollectionAssert.AreEqual(book.NoSetterImage, new byte[] { 10 });
}

[Test]
public async Task CanFetchLazyPropertyNoSetterAsync()
{
using (ISession s = OpenSession())
{
var book = await (s
.Query<Book>()
.Fetch(x => x.NoSetterImage)
.FirstAsync(x => x.Id == 1));

Assert.That(NHibernateUtil.IsPropertyInitialized(book, nameof(book.NoSetterImage)), Is.True);
}
}
}
}
8 changes: 8 additions & 0 deletions src/NHibernate.Test/LazyProperty/Book.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ public virtual string ALotOfText

public virtual byte[] Image { get; set; }

private byte[] _NoSetterImage;

public virtual byte[] NoSetterImage
{
get { return _NoSetterImage; }
set { _NoSetterImage = value; }
}

public virtual string FieldInterceptor { get; set; }

public virtual IList<Word> Words { get; set; }
Expand Down
55 changes: 55 additions & 0 deletions src/NHibernate.Test/LazyProperty/LazyPropertyFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using NHibernate.Cfg;
using NHibernate.Intercept;
using NHibernate.Linq;
using NHibernate.Tuple.Entity;
using NUnit.Framework;
using NUnit.Framework.Constraints;
Expand Down Expand Up @@ -57,6 +58,7 @@ protected override void OnSetUp()
Id = 1,
ALotOfText = "a lot of text ...",
Image = new byte[10],
NoSetterImage = new byte[10],
FieldInterceptor = "Why not that name?"
});
tx.Commit();
Expand Down Expand Up @@ -387,5 +389,58 @@ public void CanMergeTransientWithLazyPropertyInCollection()
Assert.That(book.Words.First().Content, Is.EqualTo(new byte[1] { 0 }));
}
}

[Test(Description = "GH-3333")]
public void GetLazyPropertyWithNoSetterAccessor_PropertyShouldBeInitialized()
{
using (ISession s = OpenSession())
{
var book = s.Get<Book>(1);
var image = book.NoSetterImage;
// Fails. Property remains uninitialized after it has been accessed.
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "NoSetterImage"), Is.True);
}
}

[Test(Description = "GH-3333")]
public void GetLazyPropertyWithNoSetterAccessorTwice_ResultsAreSameObject()
{
using (ISession s = OpenSession())
{
var book = s.Get<Book>(1);
var image = book.NoSetterImage;
var sameImage = book.NoSetterImage;
// Fails. Each call to a property getter returns a new object.
Assert.That(ReferenceEquals(image, sameImage), Is.True);
}
}

[Test]
public void CanSetValueForLazyPropertyNoSetter()
{
Book book;
using (ISession s = OpenSession())
{
book = s.Get<Book>(1);
book.NoSetterImage = new byte[]{10};
}

Assert.That(NHibernateUtil.IsPropertyInitialized(book, nameof(book.NoSetterImage)), Is.True);
CollectionAssert.AreEqual(book.NoSetterImage, new byte[] { 10 });
}

[Test]
public void CanFetchLazyPropertyNoSetter()
{
using (ISession s = OpenSession())
{
var book = s
.Query<Book>()
.Fetch(x => x.NoSetterImage)
.First(x => x.Id == 1);

Assert.That(NHibernateUtil.IsPropertyInitialized(book, nameof(book.NoSetterImage)), Is.True);
}
}
}
}
1 change: 1 addition & 0 deletions src/NHibernate.Test/LazyProperty/Mappings.hbm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<property name="Name" />
<property name="ALotOfText" lazy="true" />
<property name="Image" lazy="true" />
<property name="NoSetterImage" access="nosetter.pascalcase-underscore" lazy="true" />
<property name="FieldInterceptor" />
<bag name="Words" inverse="true" generic="true" cascade="all-delete-orphan" lazy="true" >
<key column="ParentId" />
Expand Down
11 changes: 11 additions & 0 deletions src/NHibernate/Tuple/Entity/PocoEntityTuplizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using NHibernate.Util;
using System.Runtime.Serialization;
using NHibernate.Bytecode.Lightweight;
using NHibernate.Intercept;

namespace NHibernate.Tuple.Entity
{
Expand Down Expand Up @@ -306,6 +307,16 @@ public override bool IsLifecycleImplementor

public override void SetPropertyValue(object entity, int i, object value)
{
// If there is no property setter we need to manually intercept value for proper lazy property handling.
if (IsInstrumented && setters[i].PropertyName == null)
{
IFieldInterceptor interceptor = _enhancementMetadata.ExtractInterceptor(entity);
if (interceptor != null)
{
value = interceptor.Intercept(entity, EntityMetamodel.PropertyNames[i], value, true);
}
}

if (isBytecodeProviderImpl && optimizer?.AccessOptimizer != null)
{
optimizer.AccessOptimizer.SetPropertyValue(entity, i, value);
Expand Down