Skip to content
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

Support input validation for minimal APIs via generic resolver model #60724

Open
wants to merge 31 commits into
base: main
Choose a base branch
from

Conversation

captainsafia
Copy link
Member

@captainsafia captainsafia commented Mar 3, 2025

Implements generic DataAnnotations-based validations resolver model and consumption in minimal APIs.

Guide for reviewers:

  • Code in Http.Abstractions contains the default implementation of methods that execute validation
  • Code in ValidationsGenerator is the source generator that statically discoves validatable types and emits a compile-time generated IValidatableInfoResolver
  • Tests in Http.Extensions/test validate end-to-end behavior in minimal APIs and include snapshots for generated code
  • Tests in Http.Abstractions/test validate the runtime behavior of the validate APIs

Address #46349.

@Copilot Copilot bot review requested due to automatic review settings March 3, 2025 20:23
@dotnet-issue-labeler dotnet-issue-labeler bot added the area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions label Mar 3, 2025
@captainsafia captainsafia added area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions labels Mar 3, 2025
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.

PR Overview

This PR implements a generic DataAnnotations‐based validation resolver model for minimal APIs. It introduces new types and interfaces for representing validatable types and parameters, registers validation services via DI, and adds tests to confirm the resolver chain order.

Reviewed Changes

File Description
src/Http/Http.Extensions/gen/Microsoft.AspNetCore.Http.ValidationsGenerator/Extensions/ISymbolExtensions.cs Provides an extension to extract display names from attributes.
src/Http/Http.Abstractions/src/Validation/ValidatableTypeInfo.cs Implements core validation logic for types including recursive validation with depth tracking.
src/Http/Http.Abstractions/src/Validation/ValidatableParameterInfo.cs Adds parameter validation logic using DataAnnotations attributes.
src/Http/Http.Extensions/gen/Microsoft.AspNetCore.Http.ValidationsGenerator/Extensions/IncrementalValuesProviderExtensions.cs Introduces extension methods (Distinct, Concat) for incremental values.
src/Http/Http.Abstractions/src/Validation/ValidationServiceCollectionExtensions.cs Provides DI registration extension for validation services.
src/Http/Http.Abstractions/src/Validation/ValidatableContext.cs Manages validation error storage and tracking of nested validation depth.
src/Http/Http.Abstractions/src/Validation/IValidatableInfoResolver.cs Defines the interface for resolving validation metadata.
src/Http/Http.Abstractions/src/Validation/ValidatableTypeAttribute.cs Introduces an attribute to mark validatable types.
src/Http/Http.Abstractions/src/Validation/ValidatablePropertyInfo.cs Encapsulates member validation logic.
src/Http/Http.Abstractions/test/ValidatableInfoResolverTests.cs Adds tests to verify resolver chain behavior and proper resolution of metadata.
src/Http/Http.Abstractions/src/Validation/ValidationOptions.cs Configures maximum depth and chains resolvers for validation.
(Other generated model files) Define source-generated types for validation metadata.

Copilot reviewed 44 out of 44 changed files in this pull request and generated 2 comments.

Comments suppressed due to low confidence (1)

src/Http/Http.Abstractions/src/Validation/ValidatablePropertyInfo.cs:121

  • The use of [result!.ErrorMessage!] to create an array is not standard C# syntax. Please update this to use a proper array initializer like 'new string[] { result!.ErrorMessage! }'.
context.AddValidationError(context.Prefix, [result!.ErrorMessage!]);

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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants