-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Exempt parameters resolved from DI from validation #61895
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
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.
Pull Request Overview
This pull request improves the handling of service-resolved parameters by excluding those marked with [FromServices] or resolved from dependency injection from the validation process. Key changes include:
- In ValidationEndpointFilterFactory.cs, a new check is added to bypass DI-resolved parameters.
- Test files have been updated to include cases for service parameters and to ensure generated validation info skips these parameters.
- The generated TypesParser now excludes parameters implementing IFromServiceMetadata during the extraction process.
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
File | Description |
---|---|
src/Http/Routing/src/ValidationEndpointFilterFactory.cs | Excludes DI-resolved parameters from validation via added helper methods and comments. |
src/Http/Http.Extensions/test/ValidationsGenerator/*.cs | Updates tests to cover new behavior for service parameters. |
src/Http/Http.Extensions/gen/Microsoft.AspNetCore.Http.ValidationsGenerator/Parsers/ValidationsGenerator.TypesParser.cs | Adjusts parsing logic to skip parameters using the IFromServiceMetadata convention. |
Comments suppressed due to low confidence (1)
src/Http/Routing/src/ValidationEndpointFilterFactory.cs:87
- Consider adding a comment here to explain how using OfType() effectively identifies DI-resolved parameters to aid future maintainability.
private static bool HasFromServicesAttribute(ParameterInfo parameterInfo) => parameterInfo.CustomAttributes.OfType<IFromServiceMetadata>().Any();
...n/Microsoft.AspNetCore.Http.ValidationsGenerator/Parsers/ValidationsGenerator.TypesParser.cs
Outdated
Show resolved
Hide resolved
…ionsGenerator/Parsers/ValidationsGenerator.TypesParser.cs Co-authored-by: Copilot <[email protected]>
foreach (var parameter in parameters) | ||
{ | ||
// Skip attributes that implement the IFromServiceMetadata interface. | ||
// These attributes are used for dependency injection (DI) purposes and do not require validation. | ||
if (parameter.GetAttributes().Any(attr => attr.AttributeClass is not null && attr.AttributeClass.ImplementsInterface(fromServiceMetadataSymbol))) |
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.
Should also add a check for [FromKeyedServicesAttribute]
(and test of course)
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.
Yep -- added a test to check for the [FromKeyedServices]
.
This pull request introduces changes to improve the handling of service-resolved parameters in ASP.NET Core's validation system. The updates ensure that parameters marked with
[FromServices]
or resolved from the dependency injection (DI) container are excluded from validation.Fixes #61392
Fixes #61770