Skip to content

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

Merged
merged 4 commits into from
May 14, 2025

Conversation

captainsafia
Copy link
Member

@captainsafia captainsafia commented May 13, 2025

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

@github-actions github-actions bot added the needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically label May 13, 2025
@captainsafia captainsafia added area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-validation Issues related to model validation in minimal and controller-based APIs and removed needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically labels May 13, 2025
@captainsafia captainsafia requested a review from Copilot May 13, 2025 14:26
Copy link
Contributor

@Copilot 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 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();

captainsafia and others added 2 commits May 13, 2025 07:28
…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)))
Copy link
Member

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)

Copy link
Member Author

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].

@captainsafia captainsafia enabled auto-merge (squash) May 14, 2025 21:35
@captainsafia captainsafia merged commit 343594e into main May 14, 2025
27 checks passed
@captainsafia captainsafia deleted the vsg-fromservice-exempt branch May 14, 2025 23:31
@dotnet-policy-service dotnet-policy-service bot added this to the 10.0-preview5 milestone May 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-validation Issues related to model validation in minimal and controller-based APIs
Projects
None yet
2 participants