-
Notifications
You must be signed in to change notification settings - Fork 10.2k
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
[DRAFT] Add initial validations generator for minimal APIs #59795
base: main
Are you sure you want to change the base?
Conversation
22f7b3d
to
f0ddc70
Compare
/// A marker interface which can be used to identify metadata that disables validation | ||
/// for a specific endpoint. |
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.
/// A marker interface which can be used to identify metadata that disables validation | |
/// for a specific endpoint. | |
/// A marker interface that identifies metadata used to disable validation for a specific API endpoint. |
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.
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.
I think the product team has more particular rules around line length than we (content) do.
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.
I think the product team has more particular rules around line length than we (content) do.
Right, random newlines, depending on the width of you editor screen :)
This PR introduces support for parameter validation based on
System.ComponentModel
annotations 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
WithValidation
invocation on anIEndpointConventionBuilder
instance. Behind the scenes, this invocation serves as the source of an interceptedWithValidation
call that will be generated by the source generator at compile-time.Since invoking
WithValidation
on 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
DisableValidation
extension method.Discovering types that require validation
The implementation discovers types that are validatable by analyzing the parameter list for all handlers in the
Map
invocations of a given application. Types are considered validatable if they:IValidateObject
interfaceValidationAttributes
on themParameters are considered validatable if they contain
ValidationAttributes
on 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 generatingValidate
invocations.Recursion depth checks
The implementation currently re-uses the
JsonSerializerOptions.MaxDepth
property 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
WithValidation
call.