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

Fix unsupported type detection when serializing polymorphic System.Type instances #67618

Merged
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 @@ -14,7 +14,7 @@ public override bool CanConvert(Type type)

return
// There's no safe way to construct a Type from untrusted user input.
type == typeof(Type) ||
typeof(Type).IsAssignableFrom(type) ||
// (De)serialization of SerializationInfo is already disallowed due to Type being disallowed
// (the two ctors on SerializationInfo take a Type, and a Type member is present when serializing).
// Explicitly disallowing this type provides a clear exception when ctors with
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ async Task RunTest<T>(string json)
[Fact]
public async Task SerializeUnsupportedType()
{
// TODO refactor to Xunit theory
Copy link
Member

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?

Copy link
Member Author

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.

await RunTest(typeof(int));
await RunTest(new SerializationInfo(typeof(Type), new FormatterConverter()));
await RunTest((IntPtr)123);
Expand All @@ -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);
Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why isn't this being run in source-gen mode?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Polymorphic scenaria will throw InvalidOperationException before we get the chance to exercise the changed codepath.

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

Choose a reason for hiding this comment

The 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. if (this.SerializerWrapper.IsSourceGenerated) ...)

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
}
}

Expand Down