Skip to content

Fix deserialization with initialized proxies in associations #2719

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

Closed
wants to merge 1 commit into from
Closed
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
138 changes: 138 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/GH2673/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------


using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using NHibernate.Util;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH2673
{
using System.Threading.Tasks;
[TestFixture]
public class FixtureAsync : BugTestCase
{
private readonly BinaryFormatter _formatter;

public FixtureAsync()
{
_formatter = new BinaryFormatter
{
#if !NETFX
SurrogateSelector = new SerializationHelper.SurrogateSelector()
#endif
};
}

protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var role1 = new Role
{
Name = "Role1",
Key = 11
};
var role2 = new Role
{
Name = "Role2",
Key = 22
};
var resource1 = new Resource
{
Key = 1,
Name = "Resource1",
ResourceRole = role1
};
var resource2 = new Resource
{
Key = 2,
Name = "Resource2",
ResourceRole = role2,
Manager = resource1
};
session.Save(role1);
session.Save(role2);
session.Save(resource1);
session.Save(resource2);

transaction.Commit();
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.CreateQuery("delete from Resource").ExecuteUpdate();
session.CreateQuery("delete from Role").ExecuteUpdate();
session.CreateQuery("delete from System.Object").ExecuteUpdate();

transaction.Commit();
}
}

[Test]
public async Task TestSerializationAsync()
{
using (var session = OpenSession())
using (var t = session.BeginTransaction())
{
var resource = await (session.GetAsync<Resource>(2));
await (session.GetAsync<Resource>(1));

var serialized = Serialize(resource);
Deserialize(serialized);

await (t.CommitAsync());
}
}

[Test]
public async Task TestSerialization2Async()
{
using (var session = OpenSession())
using (var t = session.BeginTransaction())
{
await (session.GetAsync<Resource>(1));
var resource = await (session.GetAsync<Resource>(2));

var serialized = Serialize(resource);
Deserialize(serialized);

await (t.CommitAsync());
}
}
private byte[] Serialize(object value)
{
using (var stream = new MemoryStream())
{
_formatter.Serialize(stream, value);
var result = new byte[stream.Length];
stream.Position = 0;
stream.Read(result, 0, (int) stream.Length);
stream.Close();
return result;
}
}

private object Deserialize(byte[] state)
{
using (var stream = new MemoryStream())
{
stream.Write(state, 0, state.Length);
stream.Position = 0;
return _formatter.Deserialize(stream);
}
}
}
}
127 changes: 127 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2673/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using NHibernate.Util;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH2673
{
[TestFixture]
public class Fixture : BugTestCase
{
private readonly BinaryFormatter _formatter;

public Fixture()
{
_formatter = new BinaryFormatter
{
#if !NETFX
SurrogateSelector = new SerializationHelper.SurrogateSelector()
#endif
};
}

protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var role1 = new Role
{
Name = "Role1",
Key = 11
};
var role2 = new Role
{
Name = "Role2",
Key = 22
};
var resource1 = new Resource
{
Key = 1,
Name = "Resource1",
ResourceRole = role1
};
var resource2 = new Resource
{
Key = 2,
Name = "Resource2",
ResourceRole = role2,
Manager = resource1
};
session.Save(role1);
session.Save(role2);
session.Save(resource1);
session.Save(resource2);

transaction.Commit();
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.CreateQuery("delete from Resource").ExecuteUpdate();
session.CreateQuery("delete from Role").ExecuteUpdate();
session.CreateQuery("delete from System.Object").ExecuteUpdate();

transaction.Commit();
}
}

[Test]
public void TestSerialization()
{
using (var session = OpenSession())
using (var t = session.BeginTransaction())
{
var resource = session.Get<Resource>(2);
session.Get<Resource>(1);

var serialized = Serialize(resource);
Deserialize(serialized);

t.Commit();
}
}

[Test]
public void TestSerialization2()
{
using (var session = OpenSession())
using (var t = session.BeginTransaction())
{
session.Get<Resource>(1);
var resource = session.Get<Resource>(2);

var serialized = Serialize(resource);
Deserialize(serialized);

t.Commit();
}
}
private byte[] Serialize(object value)
{
using (var stream = new MemoryStream())
{
_formatter.Serialize(stream, value);
var result = new byte[stream.Length];
stream.Position = 0;
stream.Read(result, 0, (int) stream.Length);
stream.Close();
return result;
}
}

private object Deserialize(byte[] state)
{
using (var stream = new MemoryStream())
{
stream.Write(state, 0, state.Length);
stream.Position = 0;
return _formatter.Deserialize(stream);
}
}
}
}
22 changes: 22 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2673/Mappings.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test"
namespace="NHibernate.Test.NHSpecificTest.GH2673">

<class name="Role" table="Roles">
<id name="Key">
<column name="RoleID" not-null="true"/>
<generator class="assigned" />
</id>
<property name="Name"/>
</class>

<class name="Resource" table="Resources" dynamic-insert="true">
<id name="Key" column="ResourceID" >
<generator class="assigned" />
</id>
<property name="Name"/>
<many-to-one name="Manager" class="Resource" column="ManagerID"/>
<many-to-one name="ResourceRole" class="Role" column="RoleID"/>
</class>

</hibernate-mapping>
13 changes: 13 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2673/Resource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

namespace NHibernate.Test.NHSpecificTest.GH2673
{
[Serializable]
public class Resource
{
public virtual int Key { get; set; }
public virtual Resource Manager { get; set; }
public virtual string Name { get; set; }
public virtual Role ResourceRole { get; set; }
}
}
11 changes: 11 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2673/Role.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace NHibernate.Test.NHSpecificTest.GH2673
{
[Serializable]
public class Role
{
public virtual string Name { get; set; }
public virtual int Key { get; set; }
}
}
30 changes: 18 additions & 12 deletions src/NHibernate/Proxy/StaticProxyFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,23 @@ private static readonly ConcurrentDictionary<ProxyCacheEntry, Func<ILazyInitiali

private static readonly INHibernateLogger Log = NHibernateLogger.For(typeof(StaticProxyFactory));

private NHibernateProxyFactoryInfo _proxyFactoryInfo;
private ProxyCacheEntry _cacheEntry;

public override INHibernateProxy GetProxy(object id, ISessionImplementor session)
{
try
{
var proxyActivator = Cache.GetOrAdd(_cacheEntry, pke => CreateProxyActivator(pke));
var proxyActivator = Cache.GetOrAdd(_cacheEntry, CreateProxyActivator);
return proxyActivator(
new LiteLazyInitializer(EntityName, id, session, PersistentClass),
_proxyFactoryInfo);
new NHibernateProxyFactoryInfo(
EntityName,
PersistentClass,
Interfaces,
GetIdentifierMethod,
SetIdentifierMethod,
ComponentIdType,
IsClassProxy));
}
catch (Exception ex)
{
Expand All @@ -48,14 +54,6 @@ public override void PostInstantiate(
{
base.PostInstantiate(entityName, persistentClass, interfaces, getIdentifierMethod, setIdentifierMethod, componentIdType, isClassProxy);

_proxyFactoryInfo = new NHibernateProxyFactoryInfo(
EntityName,
PersistentClass,
Interfaces,
GetIdentifierMethod,
SetIdentifierMethod,
ComponentIdType,
IsClassProxy);
_cacheEntry = new ProxyCacheEntry(IsClassProxy ? PersistentClass : typeof(object), Interfaces);
}

Expand All @@ -79,7 +77,15 @@ public override object GetFieldInterceptionProxy(object instanceToWrap)
public object GetFieldInterceptionProxy()
{
var proxyActivator = FieldInterceptorCache.GetOrAdd(PersistentClass, CreateFieldInterceptionProxyActivator);
return proxyActivator(_proxyFactoryInfo);
return proxyActivator(
new NHibernateProxyFactoryInfo(
EntityName,
PersistentClass,
Interfaces,
GetIdentifierMethod,
SetIdentifierMethod,
ComponentIdType,
IsClassProxy));
}

private Func<NHibernateProxyFactoryInfo, IFieldInterceptorAccessor> CreateFieldInterceptionProxyActivator(System.Type baseType)
Expand Down