-
-
Notifications
You must be signed in to change notification settings - Fork 96
Fix IAsyncEnumerable detection in MethodDataSource source generator #2991
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
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Co-authored-by: thomhurst <30480171+thomhurst@users.noreply.github.com>
Co-authored-by: thomhurst <30480171+thomhurst@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] MethodDataSources with IAsyncEnumerable<T> throw invalid cast exception
Fix IAsyncEnumerable detection in MethodDataSource source generator
Aug 23, 2025
Copilot finished work on behalf of
thomhurst
August 23, 2025 20:08
1 task
This was referenced Sep 1, 2025
Merged
This was referenced Oct 6, 2025
This was referenced Oct 13, 2025
This was referenced Oct 20, 2025
This was referenced Oct 27, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The source generator was not correctly detecting when a method returns
async IAsyncEnumerable<T>, causingMethodDataSourceattributes with async enumerable data sources to throwInvalidCastExceptionat runtime.Problem
When using
MethodDataSourcewith anIAsyncEnumerable, TUnit would attempt to cast the compiler-generated async iterator (e.g.,<GetAsyncTestData>d__0) directly to the test data type instead of iterating through the async enumerable and yielding individual values.Root Cause
The
IsAsyncEnumerable()method inTestMetadataGenerator.csonly checkedtype.AllInterfacesforIAsyncEnumerable<T>implementations, but didn't check if the type itself was anIAsyncEnumerable<T>. For methods returningasync IAsyncEnumerable<T>, the return type is directlyIAsyncEnumerable<T>, not a type that implements it.Solution
Updated the detection logic to check both:
IAsyncEnumerable<T>(for direct returns)IAsyncEnumerable<T>(for inherited implementations)This ensures the source generator correctly generates the async enumerable iteration code:
Instead of incorrectly treating the async iterator as a single value.
Testing
Added comprehensive test cases covering:
IAsyncEnumerable<Func<CustomType>>(matching documentation examples)IAsyncEnumerable<(int, string)>(tuple returns)IAsyncEnumerable<object[]>(existing patterns)All async enumerable tests now pass, and existing functionality remains unaffected.
Fixes #2990.
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.