[DRAFT] Add initial validations generator for minimal APIs #59795
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR introduces support for parameter validation based on
System.ComponentModelannotations to minimal APIs. This support is implemented via compile-time source generation and takes a dependency on invocation interceptors and endpoint filters.Enabling and disabling validation support
Parameter validation can be enabled at the global level via a
WithValidationinvocation on anIEndpointConventionBuilderinstance. Behind the scenes, this invocation serves as the source of an interceptedWithValidationcall that will be generated by the source generator at compile-time.Since invoking
WithValidationon every endpoint that requires validation can be tedious, it is also possible to enable parameter validation globally for all endpoints in the application by building on-top of the global conventions API proposed in #59755.When validation is enabled globally in an application, it is possible to disable it on a per-endpoint basis via the
DisableValidationextension method.Discovering types that require validation
The implementation discovers types that are validatable by analyzing the parameter list for all handlers in the
Mapinvocations of a given application. Types are considered validatable if they:IValidateObjectinterfaceValidationAttributeson themParameters are considered validatable if they contain
ValidationAttributeson them.Validation on polymorphic and inherited types
The implementation builds on top of the
[JsonDerived]type attributes in System.Text.Json namespace to discover polymorphic variants of a type. The implementation accounts for base types that are validatable when generatingValidateinvocations.Recursion depth checks
The implementation currently re-uses the
JsonSerializerOptions.MaxDepthproperty for setting the max depth that should be respected when recurring in the validation hierarchy. For most cases, this might end up being moot because the JsonSerializer will kick on the MaxDepth being violated when serializing before the validation has the chance to kick in. This will have the biggest impact for types with custom binding sources that don't go through the serializer.This choice is largely taken to avoid introduced new API, but we can consider an explicit and distinct max depth value specifically for the validation logic. This will require the introduction of a new ValidationOptions object somewhere in the Http namespace.
Areas to improvement
WithValidationcall.