Skip to content

Commit 170fbba

Browse files
committed
Throw for greedy RegisterFileConverter<object>
RegisterFileConverter<object> matches every verified value via the default `target is T` canConvert, silently hijacking all serialization. Guard the generic overloads and point users to a concrete type or the non-generic overload that requires an explicit canConvert. Fixes #1656
1 parent 109c888 commit 170fbba

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
public class RegisterFileConverterTests :
2+
BaseTest
3+
{
4+
[Fact]
5+
public void ObjectThrows()
6+
{
7+
var exception = Assert.Throws<Exception>(
8+
() => VerifierSettings.RegisterFileConverter<object>(
9+
(_, _) => new(null, "txt", "value")));
10+
Assert.Contains("too greedy", exception.Message);
11+
}
12+
13+
[Fact]
14+
public void ObjectAsyncThrows()
15+
{
16+
var exception = Assert.Throws<Exception>(
17+
() => VerifierSettings.RegisterFileConverter<object>(
18+
(_, _) => Task.FromResult<ConversionResult>(new(null, "txt", "value"))));
19+
Assert.Contains("too greedy", exception.Message);
20+
}
21+
}

src/Verify/Splitters/Settings_Typed.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public static void RegisterFileConverter<T>(
2626
CanConvert<T>? canConvert = null)
2727
{
2828
InnerVerifier.ThrowIfVerifyHasBeenRun();
29+
ThrowIfObjectConverter<T>();
2930
RegisterFileConverter(
3031
(target, context) => Task.FromResult(conversion(target, context)),
3132
canConvert);
@@ -36,10 +37,19 @@ public static void RegisterFileConverter<T>(
3637
CanConvert<T>? canConvert = null)
3738
{
3839
InnerVerifier.ThrowIfVerifyHasBeenRun();
40+
ThrowIfObjectConverter<T>();
3941
var converter = new TypeConverter((target, context) => conversion((T) target, context), DefaultCanConvert(canConvert));
4042
typedConverters.Add(converter);
4143
}
4244

45+
static void ThrowIfObjectConverter<T>()
46+
{
47+
if (typeof(T) == typeof(object))
48+
{
49+
throw new("RegisterFileConverter<object> is too greedy since object matches all verified values. Instead use a more specific type, or use the non-generic RegisterFileConverter overload that takes an explicit `canConvert`.");
50+
}
51+
}
52+
4353
public static void RegisterFileConverter(
4454
Conversion conversion,
4555
CanConvert canConvert)

0 commit comments

Comments
 (0)