-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Fix handling of implicitly declared EqualityContract on record types in validation generator #62511
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
Conversation
…in validation generator Co-authored-by: captainsafia <1857993+captainsafia@users.noreply.github.com>
@@ -370,4 +370,31 @@ async Task ValidInputProducesNoWarnings(Endpoint endpoint) | |||
}); | |||
|
|||
} | |||
|
|||
[Fact] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed the test as requested. a6e7085
@@ -0,0 +1,160 @@ | |||
//HintName: ValidatableInfoResolver.g.cs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed the snapshot file as requested. a6e7085
@@ -105,6 +105,11 @@ internal bool TryExtractValidatableType(ITypeSymbol incomingTypeSymbol, WellKnow | |||
return true; | |||
} | |||
|
|||
private static bool IsEqualityContract(IPropertySymbol prop) => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move this method to src/Validation/gen/Extensions/ISymbolExtensions.cs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved the method to ISymbolExtensions.cs as requested and updated it to use WellKnownTypes for type comparison. a6e7085
@@ -105,6 +105,11 @@ internal bool TryExtractValidatableType(ITypeSymbol incomingTypeSymbol, WellKnow | |||
return true; | |||
} | |||
|
|||
private static bool IsEqualityContract(IPropertySymbol prop) => | |||
prop.Name == "EqualityContract" | |||
&& prop.Type.ToDisplayString() == "System.Type" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add System.Type to https://github.com/dotnet/aspnetcore/blob/main/src/Shared/RoslynUtils/WellKnownTypeData.cs then use WellKnownTypes to do the type comparison here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added System.Type to WellKnownTypeData.cs and updated the method to use WellKnownTypes for type comparison instead of string comparison. a6e7085
…sions and use WellKnownTypes Co-authored-by: captainsafia <1857993+captainsafia@users.noreply.github.com>
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.