Skip to content

Commit beab8d9

Browse files
committed
#1916: Don't try to serialize generic argument types (v2)
1 parent 27c70a7 commit beab8d9

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

src/common/XunitSerializationInfo.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ internal static bool CanSerializeObject(object value)
166166

167167
Type typeToCheck = valueType;
168168
if (valueType.IsEnum() || valueType.IsNullableEnum() || (typeToCheck = value as Type) != null)
169-
return typeToCheck.IsFromLocalAssembly();
169+
return typeToCheck.FullName != null && typeToCheck.IsFromLocalAssembly();
170170

171171
return false;
172172
}
@@ -463,7 +463,12 @@ public static string Serialize(object value)
463463

464464
var typeData = value as Type;
465465
if (typeData != null)
466+
{
467+
if (typeData.FullName == null)
468+
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "We don't know how to serialize value typeof({0}) (no full name)", typeData.Name), nameof(value));
469+
466470
return SerializationHelper.GetTypeNameForSerialization(typeData);
471+
}
467472

468473
if (valueType.IsEnum())
469474
return SerializeEnum(value, valueType);

test/test.xunit.execution/Common/SerializationHelperTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,23 @@ public void InvalidValue()
120120
Assert.StartsWith("We don't know how to serialize type System.Object", argEx.Message);
121121
}
122122

123+
[Fact]
124+
public void CannotSerializeGenericArgumentType()
125+
{
126+
var type = typeof(ClassWithGenericMethod).GetMethod(nameof(ClassWithGenericMethod.GenericMethod)).GetGenericArguments()[0];
127+
128+
Assert.False(SerializationHelper.IsSerializable(type));
129+
var ex = Record.Exception(() => SerializationHelper.Serialize(type));
130+
var argEx = Assert.IsType<ArgumentException>(ex);
131+
Assert.Equal("value", argEx.ParamName);
132+
Assert.StartsWith("We don't know how to serialize value typeof(U) (no full name)", argEx.Message);
133+
}
134+
135+
class ClassWithGenericMethod
136+
{
137+
public void GenericMethod<U>() { }
138+
}
139+
123140
class MySerializable : IXunitSerializable, IEquatable<MySerializable>
124141
{
125142
int value;

0 commit comments

Comments
 (0)