Skip to content

Commit 9dee962

Browse files
NHV-135 - fix serialization of exception.
- Also fix a message issue as of NHV-118
1 parent a19e55d commit 9dee962

File tree

6 files changed

+160
-35
lines changed

6 files changed

+160
-35
lines changed

src/NHibernate.Validator.Tests/NHibernate.Validator.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@
214214
<Compile Include="SerializationHelper.cs" />
215215
<Compile Include="Serialization\ClassValidatorSerializationFixture.cs" />
216216
<Compile Include="Serialization\EventsSerializationFixture.cs" />
217+
<Compile Include="Serialization\ExceptionSerializationFixture.cs" />
217218
<Compile Include="Serialization\RuleArgsSerializationFixture.cs" />
218219
<Compile Include="Serialization\ValidatorEngineSerializationFixture.cs" />
219220
<Compile Include="Serialization\ValidatorsSerializationFixture.cs" />
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System;
2+
using NHibernate.Validator.Engine;
3+
using NHibernate.Validator.Exceptions;
4+
using NUnit.Framework;
5+
6+
namespace NHibernate.Validator.Tests.Serialization
7+
{
8+
[TestFixture]
9+
public class ExceptionSerializationFixture
10+
{
11+
[Test]
12+
public void AssertionFailure()
13+
{
14+
var ex = new AssertionFailure();
15+
Assert.That(
16+
() => SerializationHelper.Deserialize(SerializationHelper.Serialize(ex)),
17+
Throws.Nothing);
18+
}
19+
20+
[Test]
21+
public void InvalidAttributeNameException()
22+
{
23+
var ex = new InvalidAttributeNameException("blah", typeof(ExceptionSerializationFixture));
24+
var dex = default(InvalidAttributeNameException);
25+
Assert.That(
26+
() => { dex = (InvalidAttributeNameException) SerializationHelper.Deserialize(SerializationHelper.Serialize(ex)); },
27+
Throws.Nothing);
28+
Assert.That(dex, Is.Not.Null);
29+
Assert.That(dex.AttributeName, Is.EqualTo("blah"));
30+
Assert.That(dex.Class, Is.EqualTo(typeof(ExceptionSerializationFixture)));
31+
}
32+
33+
[Test]
34+
public void InvalidPropertyNameException()
35+
{
36+
var ex = new InvalidPropertyNameException("blah", typeof(ExceptionSerializationFixture));
37+
var dex = default(InvalidPropertyNameException);
38+
Assert.That(
39+
() => { dex = (InvalidPropertyNameException) SerializationHelper.Deserialize(SerializationHelper.Serialize(ex)); },
40+
Throws.Nothing);
41+
Assert.That(dex, Is.Not.Null);
42+
Assert.That(dex.PropertyName, Is.EqualTo("blah"));
43+
Assert.That(dex.Class, Is.EqualTo(typeof(ExceptionSerializationFixture)));
44+
}
45+
46+
[Test]
47+
public void InvalidStateException()
48+
{
49+
var state = new[] { new InvalidValue("blah", typeof(ExceptionSerializationFixture), "prop", null, null, null) };
50+
var ex = new InvalidStateException(state);
51+
var dex = default(InvalidStateException);
52+
Assert.That(
53+
() => { dex = (InvalidStateException) SerializationHelper.Deserialize(SerializationHelper.Serialize(ex)); },
54+
Throws.Nothing);
55+
Assert.That(dex, Is.Not.Null);
56+
Assert.That(dex.InvalidValues, Has.Length.EqualTo(1));
57+
Assert.That(
58+
dex.InvalidValues[0],
59+
Is.Not.Null.And.Property(nameof(InvalidValue.Message)).EqualTo("blah"));
60+
Assert.That(dex.InvalidValues[0].EntityType, Is.EqualTo(typeof(ExceptionSerializationFixture)));
61+
Assert.That(dex.InvalidValues[0].PropertyName, Is.EqualTo("prop"));
62+
}
63+
}
64+
}

src/NHibernate.Validator/Exceptions/AssertionFailureException.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace NHibernate.Validator.Exceptions
77
/// <summary>
88
/// Indicates failure of an assertion: a possible bug in NHibernate.Validator
99
/// </summary>
10+
// 6.0 TODO: cease deriving from ApplicationException
1011
[Serializable]
1112
public class AssertionFailureException : ApplicationException
1213
{
Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,49 @@
11
using System;
2+
using System.Runtime.Serialization;
3+
using System.Security;
24

35
namespace NHibernate.Validator.Exceptions
46
{
57
[Serializable]
68
public class InvalidAttributeNameException : ValidatorConfigurationException
79
{
8-
private readonly string attributeName;
9-
private readonly System.Type clazz;
10-
11-
public InvalidAttributeNameException(string attributeName, System.Type clazz)
10+
public InvalidAttributeNameException(string attributeName, System.Type @class)
1211
: base(
1312
string.Format(
14-
@"Attribute '{0}' was not found for the class {1};
13+
@"Attribute '{0}' was not found for the class {1};
1514
Cause:
1615
- typo
1716
- Wrong namespace (the attribute must stay in the same namespace of the related class)",
18-
attributeName, clazz.FullName))
17+
attributeName,
18+
@class.FullName))
1919
{
20-
this.attributeName = attributeName;
21-
this.clazz = clazz;
20+
AttributeName = attributeName;
21+
Class = @class;
2222
}
2323

24-
public string AttributeName
24+
protected InvalidAttributeNameException(SerializationInfo info, StreamingContext context)
25+
: base(info, context)
2526
{
26-
get { return attributeName; }
27+
AttributeName = info.GetString(nameof(InvalidAttributeNameException) + "." + nameof(AttributeName));
28+
var className = info.GetString(nameof(InvalidAttributeNameException) + "." + nameof(Class));
29+
if (!string.IsNullOrEmpty(className))
30+
Class = System.Type.GetType(className, true);
2731
}
2832

29-
public System.Type Clazz
33+
[SecurityCritical]
34+
public override void GetObjectData(SerializationInfo info, StreamingContext context)
3035
{
31-
get { return clazz; }
36+
base.GetObjectData(info, context);
37+
info.AddValue(
38+
nameof(InvalidAttributeNameException) + "." + nameof(AttributeName),
39+
AttributeName);
40+
info.AddValue(
41+
nameof(InvalidAttributeNameException) + "." + nameof(Class),
42+
Class?.AssemblyQualifiedName);
3243
}
44+
45+
public string AttributeName { get; }
46+
47+
public System.Type Class { get; }
3348
}
34-
}
49+
}
Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,54 @@
11
using System;
2+
using System.Runtime.Serialization;
3+
using System.Security;
24

35
namespace NHibernate.Validator.Exceptions
46
{
57
[Serializable]
68
public class InvalidPropertyNameException : ValidatorConfigurationException
79
{
8-
private readonly string propertyName;
9-
private readonly System.Type clazz;
10-
11-
public InvalidPropertyNameException(string propertyName, System.Type clazz)
12-
: this(string.Format("Property or field \"{0}\" was not found in the class: \"{1}\" ", propertyName, clazz.FullName), propertyName, clazz)
10+
public InvalidPropertyNameException(string propertyName, System.Type @class)
11+
: this(
12+
string.Format("Property or field \"{0}\" was not found in the class: \"{1}\" ", propertyName, @class.FullName),
13+
propertyName,
14+
@class)
1315
{
1416
}
1517

16-
public InvalidPropertyNameException(string message, string propertyName, System.Type clazz)
18+
public InvalidPropertyNameException(string message, string propertyName, System.Type @class)
1719
: base(message)
1820
{
19-
this.propertyName = propertyName;
20-
this.clazz = clazz;
21+
PropertyName = propertyName;
22+
Class = @class;
2123
}
2224

23-
public string PropertyName
25+
protected InvalidPropertyNameException(SerializationInfo info, StreamingContext context)
26+
: base(info, context)
2427
{
25-
get { return propertyName; }
28+
PropertyName = info.GetString(nameof(InvalidPropertyNameException) + "." + nameof(PropertyName));
29+
var className = info.GetString(nameof(InvalidPropertyNameException) + "." + nameof(Class));
30+
if (!string.IsNullOrEmpty(className))
31+
Class = System.Type.GetType(className, true);
2632
}
2733

28-
public System.Type Clazz
34+
[SecurityCritical]
35+
public override void GetObjectData(SerializationInfo info, StreamingContext context)
2936
{
30-
get { return clazz; }
37+
base.GetObjectData(info, context);
38+
info.AddValue(
39+
nameof(InvalidPropertyNameException) + "." + nameof(PropertyName),
40+
PropertyName);
41+
info.AddValue(
42+
nameof(InvalidPropertyNameException) + "." + nameof(Class),
43+
Class?.AssemblyQualifiedName);
3144
}
45+
46+
public string PropertyName { get; }
47+
48+
public System.Type Class { get; }
49+
50+
// Since v5.1
51+
[Obsolete("Please use Class instead.")]
52+
public System.Type Clazz => Class;
3253
}
3354
}
Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,52 @@
11
using System;
2+
using System.Runtime.Serialization;
3+
using System.Security;
24
using NHibernate.Validator.Engine;
35

46
namespace NHibernate.Validator.Exceptions
57
{
68
[Serializable]
79
public class InvalidStateException : HibernateException
810
{
9-
private readonly InvalidValue[] _invalidValues;
10-
11-
public InvalidStateException(string message, Exception inner) : base(message, inner) { }
11+
public InvalidStateException(string message, Exception inner) : base(message, inner)
12+
{
13+
}
1214

1315
public InvalidStateException(InvalidValue[] invalidValues)
14-
: this(invalidValues, invalidValues[0].GetType().Name)
16+
: this(invalidValues, invalidValues[0].EntityType.Name)
1517
{
1618
}
1719

18-
public InvalidStateException(InvalidValue[] invalidValues, String className)
20+
public InvalidStateException(InvalidValue[] invalidValues, string className)
1921
: base("validation failed for: " + className)
2022
{
21-
_invalidValues = invalidValues;
23+
InvalidValues = invalidValues;
24+
}
25+
26+
protected InvalidStateException(SerializationInfo info, StreamingContext context)
27+
: base(info, context)
28+
{
29+
InvalidValues = (InvalidValue[]) info.GetValue(
30+
nameof(InvalidStateException) + "." + nameof(InvalidValues),
31+
typeof(InvalidValue[]));
2232
}
23-
24-
public InvalidValue[] GetInvalidValues()
33+
34+
[SecurityCritical]
35+
public override void GetObjectData(SerializationInfo info, StreamingContext context)
36+
{
37+
base.GetObjectData(info, context);
38+
info.AddValue(
39+
nameof(InvalidStateException) + "." + nameof(InvalidValues),
40+
InvalidValues);
41+
}
42+
43+
public InvalidValue[] InvalidValues { get; }
44+
45+
// Since 5.1
46+
[Obsolete("Use InvalidValues property instead.")]
47+
public InvalidValue[] GetInvalidValues()
2548
{
26-
return _invalidValues;
49+
return InvalidValues;
2750
}
2851
}
29-
}
52+
}

0 commit comments

Comments
 (0)