Skip to content

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
242-methods-generated-by-generateinterfacemembers-dont-match-default-values-for-optional-arguments-with-the-original-method
Dec 11, 2025
Merged

fix(source-generators): Methods generated by [GenerateInterfaceMembers] don't match default values for optional arguments with the original method#244
DecSmith42 merged 4 commits intomainfrom
242-methods-generated-by-generateinterfacemembers-dont-match-default-values-for-optional-arguments-with-the-original-method

Conversation

@DecSmith42
Copy link
Owner

This pull request enhances the GenerateInterfaceMembersSourceGenerator by 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

  • Updated the GenerateMethodLine logic 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.
  • Changed GeneratePropertyLine and GenerateMethodLine to be private methods for better encapsulation and code clarity.

Testing Enhancements

  • Added a new unit test MinimalBuildDefinition_WithGeneratedInterfaceMember_GeneratesSource in GenerateInterfaceMembersGeneratorTests.cs to 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.
  • Included a verified output file to demonstrate the expected generated code for the test scenario, covering all supported member types.

…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.
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 GenerateMethodLine to 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.

@DecSmith42 DecSmith42 disabled auto-merge December 11, 2025 10:42
…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.
@DecSmith42 DecSmith42 merged commit 1493d8e into main Dec 11, 2025
33 checks passed
@DecSmith42 DecSmith42 deleted the 242-methods-generated-by-generateinterfacemembers-dont-match-default-values-for-optional-arguments-with-the-original-method branch December 11, 2025 11:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Methods generated by [GenerateInterfaceMembers] don't match default values for optional arguments with the original method

2 participants