-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Fix unsupported type detection when serializing polymorphic System.Type instances #67618
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,6 +72,7 @@ async Task RunTest<T>(string json) | |
[Fact] | ||
public async Task SerializeUnsupportedType() | ||
{ | ||
// TODO refactor to Xunit theory | ||
await RunTest(typeof(int)); | ||
await RunTest(new SerializationInfo(typeof(Type), new FormatterConverter())); | ||
await RunTest((IntPtr)123); | ||
|
@@ -96,7 +97,6 @@ async Task RunTest<T>(T value) | |
Assert.Contains("$", exAsStr); | ||
|
||
ClassWithType<T> obj = new ClassWithType<T> { Prop = value }; | ||
|
||
ex = await Assert.ThrowsAsync<NotSupportedException>(async () => await Serializer.SerializeWrapper(obj)); | ||
exAsStr = ex.ToString(); | ||
Assert.Contains(fullName, exAsStr); | ||
|
@@ -124,6 +124,20 @@ async Task RunTest<T>(T value) | |
serialized = await Serializer.SerializeWrapper(obj, new JsonSerializerOptions { IgnoreNullValues = true }); | ||
Assert.Equal(@"{}", serialized); | ||
} | ||
|
||
#if !BUILDING_SOURCE_GENERATOR_TESTS | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why isn't this being run in source-gen mode? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Polymorphic scenaria will throw There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it. FWIW when there are consequential divergences in source-gen/reflection behavior, we add a separate test variant for source-gen, e.g. here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it. I'm familiar with that pattern but FWIW it seems to encourage repetition of test definitions (or even divergence because only one implementation gets updated like in this case). Going forward I'd prefer it if we could avoid overriding test methods and either use compile conditionals or runtime parameterization (e.g. |
||
Type runtimeType = GetNullableOfTUnderlyingType(value.GetType(), out bool _); | ||
|
||
ex = await Assert.ThrowsAsync<NotSupportedException>(async () => await Serializer.SerializeWrapper<object>(value)); | ||
exAsStr = ex.ToString(); | ||
Assert.Contains(runtimeType.FullName, exAsStr); | ||
Assert.Contains("$", exAsStr); | ||
|
||
ClassWithType<object> polyObj = new ClassWithType<object> { Prop = value }; | ||
ex = await Assert.ThrowsAsync<NotSupportedException>(async () => await Serializer.SerializeWrapper(polyObj)); | ||
exAsStr = ex.ToString(); | ||
Assert.Contains(runtimeType.FullName, exAsStr); | ||
#endif | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could this be done as part of this PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a pervasive pattern so I'd rather it be done in a separate PR.