Skip to content
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

Issue 83667 #83820

Merged
merged 4 commits into from
Mar 27, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ internal static JsonConverter GetConverterForType(Type typeToConvert, JsonSerial
converter = options.ExpandConverterFactory(converter, typeToConvert);
if (!converter.TypeToConvert.IsInSubtypeRelationshipWith(typeToConvert))
{
ThrowHelper.ThrowInvalidOperationException_SerializationConverterNotCompatible(converter.GetType(), converter.TypeToConvert);
ThrowHelper.ThrowInvalidOperationException_SerializationConverterNotCompatible(converter.GetType(), typeToConvert);
}

JsonSerializerOptions.CheckConverterNullabilityIsSameAsPropertyType(converter, typeToConvert);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,5 +221,25 @@ public static void GetConverterTypeToConvertNull()
{
Assert.Throws<ArgumentNullException>(() => (new JsonSerializerOptions()).GetConverter(typeToConvert: null!));
}

[Fact]
public static void ErrorMessageContainsExpectedType()
{
JsonSerializerOptions options = new();
options.Converters.Add(new InvalidJsonConverterFactory());
var ex = Assert.Throws<InvalidOperationException>(() =>
JsonSerializer.Serialize(new InvalidTestInfo("Hello"), options));
Assert.Contains(typeof(InvalidTestInfo).Name, ex.Message);
}

private sealed record InvalidTestInfo(string Name);

private sealed class InvalidJsonConverterFactory : JsonConverterFactory
{
public override bool CanConvert(Type typeToConvert) => true;

public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options)
=> new MyBoolEnumConverter();
}
}
}