Fix handling of implicitly declared EqualityContract on record types in validation generator #62511
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.
When a record class being inspected comes from metadata (i.e., a referenced assembly or a prior incremental step), its compiler-synthesized
EqualityContract
property is not flagged withIsImplicitlyDeclared == true
. The current filter inExtractValidatableMembers
relies solely on that flag, so the property slips through and aGeneratedValidatablePropertyInfo
is produced for it.As a result, every metadata-based record type ends up with a spurious
ValidatablePropertyInfo
forEqualityContract
which the runtime resolver will attempt to apply and fail for.Changes Made
This PR adds a helper method that recognizes
EqualityContract
by its signature, not just byIsImplicitlyDeclared
, and uses it in the skip logic:The member filtering loop is updated to use this helper:
Testing
Added a specific test
RecordTypes_DoNotIncludeEqualityContractProperty
that verifies record types from metadata don't incorrectly include theEqualityContract
property in generated validation info. The test shows that only the actual record properties (likeName
with[Required]
) are included, not the compiler-generatedEqualityContract
.All existing tests continue to pass, ensuring no regressions.
Fixes #62510.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.