-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Fix IServiceProvider resolution for DataAnnotations validation on Options #125651
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -21,7 +21,8 @@ public static class OptionsBuilderDataAnnotationsExtensions | |||||||||||
| " members may be trimmed.")] | ||||||||||||
| public static OptionsBuilder<TOptions> ValidateDataAnnotations<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)] TOptions>(this OptionsBuilder<TOptions> optionsBuilder) where TOptions : class | ||||||||||||
| { | ||||||||||||
| optionsBuilder.Services.AddSingleton<IValidateOptions<TOptions>>(new DataAnnotationValidateOptions<TOptions>(optionsBuilder.Name)); | ||||||||||||
| optionsBuilder.Services.AddSingleton<IValidateOptions<TOptions>>( | ||||||||||||
| sp => new DataAnnotationValidateOptions<TOptions>(optionsBuilder.Name, sp)); | ||||||||||||
|
Comment on lines
+24
to
+25
|
||||||||||||
| optionsBuilder.Services.AddSingleton<IValidateOptions<TOptions>>( | |
| sp => new DataAnnotationValidateOptions<TOptions>(optionsBuilder.Name, sp)); | |
| string? name = optionsBuilder.Name; | |
| optionsBuilder.Services.AddSingleton<IValidateOptions<TOptions>>( | |
| sp => new DataAnnotationValidateOptions<TOptions>(name, sp)); |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -814,5 +814,53 @@ public void ValidateOnStart_ConfigureBasedOnDataAnnotationRestrictions_Validatio | |||||
|
|
||||||
| Assert.NotNull(value); | ||||||
| } | ||||||
|
|
||||||
| [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)] | ||||||
| public class RequiresServiceAttribute : ValidationAttribute | ||||||
| { | ||||||
| protected override ValidationResult IsValid(object value, ValidationContext validationContext) | ||||||
|
||||||
| protected override ValidationResult IsValid(object value, ValidationContext validationContext) | |
| protected override ValidationResult? IsValid(object? value, ValidationContext validationContext) |
Copilot
AI
Apr 22, 2026
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.
This test asserts on OptionsValidationException.Message, which is derived formatting over Failures and can be brittle if message formatting changes. Prefer asserting on error.Failures (or using the existing ValidateFailure<TOptions> helper in this file) to validate the exact failure(s) without depending on Message formatting.
| Assert.Contains("SomeService not resolved correctly", error.Message); | |
| Assert.Contains("SomeService not resolved correctly", error.Failures); |
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.
XML doc comment has an extra space before the period after the
<see cref="DataAnnotationValidateOptions{TOptions}"/>reference; this shows up in generated docs and should be removed.