fix(source-generators): Methods generated by [GenerateInterfaceMembers] don't match default values for optional arguments with the original method#244
Merged
DecSmith42 merged 4 commits intomainfrom Dec 11, 2025
Conversation
…rocessing Enhanced property and method generation logic for better readability and maintainability. Updated parameter handling to ensure proper syntax generation, including support for `ref`, `in`, and `out` parameters.
Contributor
There was a problem hiding this comment.
Pull request overview
This pull request fixes a bug in the GenerateInterfaceMembersSourceGenerator where generated method signatures did not properly preserve optional parameter default values, ref/in/out modifiers, and parameter attributes. The fix changes the parameter generation approach from using Roslyn's symbol API to directly extracting parameter syntax via DeclaringSyntaxReferences, ensuring the full parameter declaration (including default values and attributes) is preserved in the generated code.
Key Changes
- Modified
GenerateMethodLineto use syntax-based parameter extraction (ToFullString()) instead of symbol-based construction to preserve default values, attributes, and all parameter metadata - Updated method call parameter generation to correctly handle ref/in/out modifiers
- Added comprehensive test coverage validating the generator works correctly with properties, methods with various parameter types (optional, ref/in/out, attributed), generics, and collections
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| DecSm.Atom.SourceGenerators/GenerateInterfaceMembersSourceGenerator.cs | Refactored GenerateMethodLine and GeneratePropertyLine from local functions to private static methods; changed parameter generation from symbol-based to syntax-based approach to preserve all parameter metadata |
| DecSm.Atom.SourceGenerators.Tests/Tests/GenerateInterfaceMembersGeneratorTests.cs | Added comprehensive test covering interface member generation for properties, methods with various parameter types (optional, ref/in/out, attributed, generic, collections) |
| DecSm.Atom.SourceGenerators.Tests/Tests/GenerateInterfaceMembersGeneratorTests.MinimalBuildDefinition_WithGeneratedInterfaceMember_GeneratesSource.verified.txt | Verified test output demonstrating correct generation of all tested member types |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
DecSm.Atom.SourceGenerators/GenerateInterfaceMembersSourceGenerator.cs
Outdated
Show resolved
Hide resolved
DecSm.Atom.SourceGenerators/GenerateInterfaceMembersSourceGenerator.cs
Outdated
Show resolved
Hide resolved
…tting Refined parameter processing in method generation to improve handling of attributes, `ref`, `in`, `out` keywords, and default values. Enhanced readability of generated source code by updating formatting logic. Added necessary namespace imports in test files.
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
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.
This pull request enhances the
GenerateInterfaceMembersSourceGeneratorby improving how interface members are generated and adds a comprehensive test to verify the output. The main changes focus on more accurate handling of method parameters—including ref, in, out, and attributed parameters—when generating class member implementations, ensuring that the generated code matches the interface contract precisely.Improvements to Source Generation
GenerateMethodLinelogic to correctly handle all parameter types (including ref, in, out, and attributed parameters), ensuring the generated method signatures and calls accurately reflect the interface definition.GeneratePropertyLineandGenerateMethodLineto be private methods for better encapsulation and code clarity.Testing Enhancements
MinimalBuildDefinition_WithGeneratedInterfaceMember_GeneratesSourceinGenerateInterfaceMembersGeneratorTests.csto verify that the generator produces correct source code for a variety of interface member types, including properties, methods with parameters, generics, collections, optional parameters, ref/in/out parameters, and attributed parameters.