Skip to content

Fix DAM attributes in ValidationAttributeCache to include PublicConstructors #62656

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
merged 8 commits into from
Jul 11, 2025

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 10, 2025

The Validations source generator was emitting IL2075 warnings because the GetValidationAttributes method calls Type.GetConstructors() but the Type parameters only had DynamicallyAccessedMemberTypes.PublicProperties annotations.

The warning was:

ValidatableInfoResolver.g.cs(323,45): warning IL2075: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors' in call to 'System.Type.GetConstructors()'. The return value of method 'Microsoft.Extensions.Validation.Generated.ValidationAttributeCache.CacheKey.ContainingType.get' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.

Root Cause

In ValidationsGenerator.Emitter.cs, the ValidationAttributeCache.GetValidationAttributes method and related types used DynamicallyAccessedMemberTypes.PublicProperties but the generated code also calls GetConstructors() to handle record scenarios where validation attributes may be on constructor parameters.

Changes Made

Updated the DynamicallyAccessedMembers attributes in 4 locations to include both PublicProperties | PublicConstructors:

  1. CacheKey record property annotation
  2. GetValidationAttributes method parameter annotation
  3. GeneratedValidatablePropertyInfo constructor parameter annotation
  4. GeneratedValidatablePropertyInfo.ContainingType property annotation

This ensures the Type parameters have the correct annotations that match their actual usage pattern of accessing both properties (via GetProperty) and constructors (via GetConstructors).

Testing

  • All existing tests pass
  • Updated test snapshots to reflect the corrected generated code
  • Verified the IL2075 warning is resolved

Fixes #62655.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@Copilot Copilot AI changed the title [WIP] GetValidationAttributes needs updated DAM attributes in Validations source generator Fix DAM attributes in ValidationAttributeCache to include PublicConstructors Jul 10, 2025
Copilot finished work on behalf of captainsafia July 10, 2025 14:36
@Copilot Copilot AI requested a review from captainsafia July 10, 2025 14:36
@captainsafia captainsafia marked this pull request as ready for review July 10, 2025 20:26
@captainsafia captainsafia requested review from wtgodbe and a team as code owners July 10, 2025 20:26
@captainsafia captainsafia requested a review from javiercn July 10, 2025 20:26
@captainsafia captainsafia enabled auto-merge (squash) July 10, 2025 22:42
@captainsafia captainsafia merged commit f9a5ddf into main Jul 11, 2025
29 checks passed
@captainsafia captainsafia deleted the copilot/fix-62655 branch July 11, 2025 20:53
@dotnet-policy-service dotnet-policy-service bot added this to the 10.0-preview7 milestone Jul 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

GetValidationAttributes needs updated DAM attributes in Validations source generator
3 participants