Skip to content

Commit

Permalink
Merge pull request #1970 from hazzik/gh-1964
Browse files Browse the repository at this point in the history
Fix session no more serializable with entities having a field interceptor
Fix #1964
  • Loading branch information
fredericDelaporte authored Jan 13, 2019
2 parents f490609 + 02d9bd7 commit b73861b
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 18 deletions.
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 5.2.2.{build}
version: 5.2.3.{build}
image: Visual Studio 2017
environment:
matrix:
Expand Down
2 changes: 1 addition & 1 deletion build-common/NHibernate.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<VersionMajor Condition="'$(VersionMajor)' == ''">5</VersionMajor>
<VersionMinor Condition="'$(VersionMinor)' == ''">2</VersionMinor>
<VersionPatch Condition="'$(VersionPatch)' == ''">2</VersionPatch>
<VersionPatch Condition="'$(VersionPatch)' == ''">3</VersionPatch>
<VersionSuffix Condition="'$(VersionSuffix)' == ''"></VersionSuffix>

<VersionPrefix>$(VersionMajor).$(VersionMinor).$(VersionPatch)</VersionPrefix>
Expand Down
4 changes: 2 additions & 2 deletions build-common/common.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

<!-- This is used only for build folder -->
<!-- TODO: Either remove or refactor to use NHibernate.props -->
<property name="project.version" value="5.2.2" overwrite="false" />
<property name="project.version.numeric" value="5.2.2" overwrite="false" />
<property name="project.version" value="5.2.3" overwrite="false" />
<property name="project.version.numeric" value="5.2.3" overwrite="false" />

<!-- properties used to connect to database for testing -->
<include buildfile="nhibernate-properties.xml" />
Expand Down
11 changes: 11 additions & 0 deletions releasenotes.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
Build 5.2.3
=============================

Release notes - NHibernate - Version 5.2.3

1 issue was resolved in this release.

** Bug

* #1964 Unable to serialize session because SerializationFieldInfo is not marked as serializable

Build 5.2.2
=============================

Expand Down
8 changes: 7 additions & 1 deletion src/NHibernate.Test/StaticProxyTest/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,19 @@ public class InterfacedEntity : IEntity
}

[Serializable]
public class LazyTextEntity
public class LazyTextEntity : BaseTextEntity
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
public virtual string Text { get; set; }
}

[Serializable]
public class BaseTextEntity
{
public virtual int Test { get; set; }
}

[Serializable]
public class InterfacedLazyTextEntity : ILazyTextEntity
{
Expand Down
20 changes: 7 additions & 13 deletions src/NHibernate/Proxy/FieldInterceptorObjectReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public sealed class FieldInterceptorObjectReference : IObjectReference, ISeriali
private readonly object _deserializedProxy;

private const string HasAdditionalDataName = "proxy$hasAdditionalData";
private const string AdditionalMemberName = "proxy$additionalMembers";

public FieldInterceptorObjectReference(NHibernateProxyFactoryInfo proxyFactoryInfo, IFieldInterceptor fieldInterceptorField)
{
Expand All @@ -30,39 +29,36 @@ private FieldInterceptorObjectReference(SerializationInfo info, StreamingContext
_proxyFactoryInfo = info.GetValue<NHibernateProxyFactoryInfo>(nameof(_proxyFactoryInfo));
_fieldInterceptor = info.GetValue<IFieldInterceptor>(nameof(_fieldInterceptor));

var proxy = _proxyFactoryInfo.CreateProxyFactory().GetFieldInterceptionProxy(null);
if (info.GetBoolean(HasAdditionalDataName))
{
_deserializedProxy = _proxyFactoryInfo.CreateProxyFactory().GetFieldInterceptionProxy(null);

var additionalMembers = info.GetValue<MemberInfo[]>(AdditionalMemberName);
if (additionalMembers == null)
return;

foreach (var member in additionalMembers)
var members = FormatterServices.GetSerializableMembers(_proxyFactoryInfo.PersistentClass, context);
foreach (var member in members)
{
switch (member)
{
case FieldInfo field:
field.SetValue(
_deserializedProxy,
proxy,
info.GetValue(GetAdditionalMemberName(field), field.FieldType));
break;
case PropertyInfo property:
property.SetValue(
_deserializedProxy,
proxy,
info.GetValue(GetAdditionalMemberName(property), property.PropertyType));
break;
default:
throw new NotSupportedException(
$"Deserializing a member of type {member.GetType()} is not supported.");
}
}
_deserializedProxy = proxy;
}
else
{
// Base type has a custom serialization, we need to call the proxy deserialization for deserializing
// base type members too.
var proxyType = _proxyFactoryInfo.CreateProxyFactory().GetFieldInterceptionProxy(null).GetType();
var proxyType = proxy.GetType();
var deserializationConstructor = proxyType.GetConstructor(
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public,
null,
Expand Down Expand Up @@ -95,8 +91,6 @@ public void GetBaseData(SerializationInfo info, StreamingContext context, object
info.AddValue(HasAdditionalDataName, true);

var members = FormatterServices.GetSerializableMembers(proxyBaseType, context);
info.AddValue(AdditionalMemberName, members);

foreach (var member in members)
{
switch (member)
Expand Down
2 changes: 2 additions & 0 deletions src/NHibernate/Proxy/NHibernateProxyFactoryInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ internal NHibernateProxyFactoryInfo(string entityName, System.Type persistentCla
_componentIdType = componentIdType;
}

internal System.Type PersistentClass => _persistentClass;

public IProxyFactory CreateProxyFactory()
{
var factory = new StaticProxyFactory();
Expand Down

0 comments on commit b73861b

Please sign in to comment.