Skip to content

Add trim-safe ValidationContext constructor #113426

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 6 commits into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/libraries/Microsoft.Extensions.Options/gen/Emitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -681,15 +681,23 @@ private void GenModelValidationMethod(
if (_symbolHolder.UnconditionalSuppressMessageAttributeSymbol is not null)
{
// We disable the warning on `new ValidationContext(object)` usage as we use it in a safe way that not require executing the reflection code.
// This is done by initializing the DisplayName in the context which is the part trigger reflection if it is not initialized.
// This is done by initializing the DisplayName in the context which is the part trigger reflection if it is not initialized. For
// projects targeting .NET 10 and above, we can avoid the suppression since we use the new trim-safe constructor.
OutLn("#if !NET10_0_OR_GREATER");
OutLn($"[System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage(\"Trimming\", \"IL2026:RequiresUnreferencedCode\",");
OutLn($" Justification = \"The created ValidationContext object is used in a way that never call reflection\")]");
OutLn("#endif");
}

OutLn($"public {(makeStatic ? "static " : string.Empty)}global::Microsoft.Extensions.Options.ValidateOptionsResult Validate(string? name, {modelToValidate.Name} options)");
OutOpenBrace();
OutLn($"global::Microsoft.Extensions.Options.ValidateOptionsResultBuilder? builder = null;");
OutLn("#if NET10_0_OR_GREATER");
OutLn($"string displayName = string.IsNullOrEmpty(name) ? \"{modelToValidate.SimpleName}.Validate\" : $\"{{name}}.Validate\";");
OutLn($"var context = new {StaticValidationContextType}(options, displayName, null, null);");
OutLn("#else");
OutLn($"var context = new {StaticValidationContextType}(options);");
OutLn("#endif");

int capacity = modelToValidate.MembersToValidate.Count == 0 ? 0 : modelToValidate.MembersToValidate.Max(static vm => vm.ValidationAttributes.Count);
if (capacity > 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,19 @@ partial class MyOptionsValidator
/// <param name="options">The options instance.</param>
/// <returns>Validation result.</returns>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Options.SourceGeneration", "42.42.42.42")]
#if !NET10_0_OR_GREATER
[System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage("Trimming", "IL2026:RequiresUnreferencedCode",
Justification = "The created ValidationContext object is used in a way that never call reflection")]
#endif
public global::Microsoft.Extensions.Options.ValidateOptionsResult Validate(string? name, global::Test.MyOptions options)
{
global::Microsoft.Extensions.Options.ValidateOptionsResultBuilder? builder = null;
#if NET10_0_OR_GREATER
string displayName = string.IsNullOrEmpty(name) ? "MyOptions.Validate" : $"{name}.Validate";
var context = new global::System.ComponentModel.DataAnnotations.ValidationContext(options, displayName, null, null);
#else
var context = new global::System.ComponentModel.DataAnnotations.ValidationContext(options);
#endif
var validationResults = new global::System.Collections.Generic.List<global::System.ComponentModel.DataAnnotations.ValidationResult>();
var validationAttributes = new global::System.Collections.Generic.List<global::System.ComponentModel.DataAnnotations.ValidationAttribute>(1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,19 @@ partial struct MyOptionsValidator
/// <param name="options">The options instance.</param>
/// <returns>Validation result.</returns>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Options.SourceGeneration", "42.42.42.42")]
#if !NET10_0_OR_GREATER
[System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage("Trimming", "IL2026:RequiresUnreferencedCode",
Justification = "The created ValidationContext object is used in a way that never call reflection")]
#endif
public global::Microsoft.Extensions.Options.ValidateOptionsResult Validate(string? name, global::HelloWorld.MyOptions options)
{
global::Microsoft.Extensions.Options.ValidateOptionsResultBuilder? builder = null;
#if NET10_0_OR_GREATER
string displayName = string.IsNullOrEmpty(name) ? "MyOptions.Validate" : $"{name}.Validate";
var context = new global::System.ComponentModel.DataAnnotations.ValidationContext(options, displayName, null, null);
#else
var context = new global::System.ComponentModel.DataAnnotations.ValidationContext(options);
#endif
var validationResults = new global::System.Collections.Generic.List<global::System.ComponentModel.DataAnnotations.ValidationResult>();
var validationAttributes = new global::System.Collections.Generic.List<global::System.ComponentModel.DataAnnotations.ValidationAttribute>(1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ partial struct MyOptionsValidator
public global::Microsoft.Extensions.Options.ValidateOptionsResult Validate(string? name, global::HelloWorld.MyOptions options)
{
global::Microsoft.Extensions.Options.ValidateOptionsResultBuilder? builder = null;
#if NET10_0_OR_GREATER
string displayName = string.IsNullOrEmpty(name) ? "MyOptions.Validate" : $"{name}.Validate";
var context = new global::System.ComponentModel.DataAnnotations.ValidationContext(options, displayName, null, null);
#else
var context = new global::System.ComponentModel.DataAnnotations.ValidationContext(options);
#endif
var validationResults = new global::System.Collections.Generic.List<global::System.ComponentModel.DataAnnotations.ValidationResult>();
var validationAttributes = new global::System.Collections.Generic.List<global::System.ComponentModel.DataAnnotations.ValidationAttribute>(1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,19 @@ partial class OptionsUsingGeneratedAttributesValidator
/// <param name="options">The options instance.</param>
/// <returns>Validation result.</returns>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Options.SourceGeneration", "42.42.42.42")]
#if !NET10_0_OR_GREATER
[System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage("Trimming", "IL2026:RequiresUnreferencedCode",
Justification = "The created ValidationContext object is used in a way that never call reflection")]
#endif
public global::Microsoft.Extensions.Options.ValidateOptionsResult Validate(string? name, global::ValidationTest.OptionsUsingGeneratedAttributes options)
{
global::Microsoft.Extensions.Options.ValidateOptionsResultBuilder? builder = null;
#if NET10_0_OR_GREATER
string displayName = string.IsNullOrEmpty(name) ? "OptionsUsingGeneratedAttributes.Validate" : $"{name}.Validate";
var context = new global::System.ComponentModel.DataAnnotations.ValidationContext(options, displayName, null, null);
#else
var context = new global::System.ComponentModel.DataAnnotations.ValidationContext(options);
#endif
var validationResults = new global::System.Collections.Generic.List<global::System.ComponentModel.DataAnnotations.ValidationResult>();
var validationAttributes = new global::System.Collections.Generic.List<global::System.ComponentModel.DataAnnotations.ValidationAttribute>(1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,19 @@ partial class OptionsUsingGeneratedAttributesValidator
/// <param name="options">The options instance.</param>
/// <returns>Validation result.</returns>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Options.SourceGeneration", "42.42.42.42")]
#if !NET10_0_OR_GREATER
[System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage("Trimming", "IL2026:RequiresUnreferencedCode",
Justification = "The created ValidationContext object is used in a way that never call reflection")]
#endif
public global::Microsoft.Extensions.Options.ValidateOptionsResult Validate(string? name, global::ValidationTest.OptionsUsingGeneratedAttributes options)
{
global::Microsoft.Extensions.Options.ValidateOptionsResultBuilder? builder = null;
#if NET10_0_OR_GREATER
string displayName = string.IsNullOrEmpty(name) ? "OptionsUsingGeneratedAttributes.Validate" : $"{name}.Validate";
var context = new global::System.ComponentModel.DataAnnotations.ValidationContext(options, displayName, null, null);
#else
var context = new global::System.ComponentModel.DataAnnotations.ValidationContext(options);
#endif
var validationResults = new global::System.Collections.Generic.List<global::System.ComponentModel.DataAnnotations.ValidationResult>();
var validationAttributes = new global::System.Collections.Generic.List<global::System.ComponentModel.DataAnnotations.ValidationAttribute>(1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ partial class OptionsUsingGeneratedAttributesValidator
public global::Microsoft.Extensions.Options.ValidateOptionsResult Validate(string? name, global::ValidationTest.OptionsUsingGeneratedAttributes options)
{
global::Microsoft.Extensions.Options.ValidateOptionsResultBuilder? builder = null;
#if NET10_0_OR_GREATER
string displayName = string.IsNullOrEmpty(name) ? "OptionsUsingGeneratedAttributes.Validate" : $"{name}.Validate";
var context = new global::System.ComponentModel.DataAnnotations.ValidationContext(options, displayName, null, null);
#else
var context = new global::System.ComponentModel.DataAnnotations.ValidationContext(options);
#endif
var validationResults = new global::System.Collections.Generic.List<global::System.ComponentModel.DataAnnotations.ValidationResult>();
var validationAttributes = new global::System.Collections.Generic.List<global::System.ComponentModel.DataAnnotations.ValidationAttribute>(1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ partial class OptionsUsingGeneratedAttributesValidator
public global::Microsoft.Extensions.Options.ValidateOptionsResult Validate(string? name, global::ValidationTest.OptionsUsingGeneratedAttributes options)
{
global::Microsoft.Extensions.Options.ValidateOptionsResultBuilder? builder = null;
#if NET10_0_OR_GREATER
string displayName = string.IsNullOrEmpty(name) ? "OptionsUsingGeneratedAttributes.Validate" : $"{name}.Validate";
var context = new global::System.ComponentModel.DataAnnotations.ValidationContext(options, displayName, null, null);
#else
var context = new global::System.ComponentModel.DataAnnotations.ValidationContext(options);
#endif
var validationResults = new global::System.Collections.Generic.List<global::System.ComponentModel.DataAnnotations.ValidationResult>();
var validationAttributes = new global::System.Collections.Generic.List<global::System.ComponentModel.DataAnnotations.ValidationAttribute>(1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,19 @@ internal sealed partial class __EndPointsOptionsValidator__
/// <param name="options">The options instance.</param>
/// <returns>Validation result.</returns>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Options.SourceGeneration", "42.42.42.42")]
#if !NET10_0_OR_GREATER
[System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage("Trimming", "IL2026:RequiresUnreferencedCode",
Justification = "The created ValidationContext object is used in a way that never call reflection")]
#endif
public static global::Microsoft.Extensions.Options.ValidateOptionsResult Validate(string? name, global::Test.EndPointsOptions options)
{
global::Microsoft.Extensions.Options.ValidateOptionsResultBuilder? builder = null;
#if NET10_0_OR_GREATER
string displayName = string.IsNullOrEmpty(name) ? "EndPointsOptions.Validate" : $"{name}.Validate";
var context = new global::System.ComponentModel.DataAnnotations.ValidationContext(options, displayName, null, null);
#else
var context = new global::System.ComponentModel.DataAnnotations.ValidationContext(options);
#endif
var validationResults = new global::System.Collections.Generic.List<global::System.ComponentModel.DataAnnotations.ValidationResult>();
var validationAttributes = new global::System.Collections.Generic.List<global::System.ComponentModel.DataAnnotations.ValidationAttribute>(2);

Expand Down Expand Up @@ -58,12 +65,19 @@ internal sealed partial class __RedisClientOptionsValidator__
/// <param name="options">The options instance.</param>
/// <returns>Validation result.</returns>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Options.SourceGeneration", "42.42.42.42")]
#if !NET10_0_OR_GREATER
[System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage("Trimming", "IL2026:RequiresUnreferencedCode",
Justification = "The created ValidationContext object is used in a way that never call reflection")]
#endif
public static global::Microsoft.Extensions.Options.ValidateOptionsResult Validate(string? name, global::Test.RedisClientOptions options)
{
global::Microsoft.Extensions.Options.ValidateOptionsResultBuilder? builder = null;
#if NET10_0_OR_GREATER
string displayName = string.IsNullOrEmpty(name) ? "RedisClientOptions.Validate" : $"{name}.Validate";
var context = new global::System.ComponentModel.DataAnnotations.ValidationContext(options, displayName, null, null);
#else
var context = new global::System.ComponentModel.DataAnnotations.ValidationContext(options);
#endif
var validationResults = new global::System.Collections.Generic.List<global::System.ComponentModel.DataAnnotations.ValidationResult>();
var validationAttributes = new global::System.Collections.Generic.List<global::System.ComponentModel.DataAnnotations.ValidationAttribute>(1);

Expand Down Expand Up @@ -107,12 +121,19 @@ partial class RedisNamedClientOptions
/// <param name="options">The options instance.</param>
/// <returns>Validation result.</returns>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Options.SourceGeneration", "42.42.42.42")]
#if !NET10_0_OR_GREATER
[System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage("Trimming", "IL2026:RequiresUnreferencedCode",
Justification = "The created ValidationContext object is used in a way that never call reflection")]
#endif
public global::Microsoft.Extensions.Options.ValidateOptionsResult Validate(string? name, global::Test.RedisNamedClientOptions options)
{
global::Microsoft.Extensions.Options.ValidateOptionsResultBuilder? builder = null;
#if NET10_0_OR_GREATER
string displayName = string.IsNullOrEmpty(name) ? "RedisNamedClientOptions.Validate" : $"{name}.Validate";
var context = new global::System.ComponentModel.DataAnnotations.ValidationContext(options, displayName, null, null);
#else
var context = new global::System.ComponentModel.DataAnnotations.ValidationContext(options);
#endif
var validationResults = new global::System.Collections.Generic.List<global::System.ComponentModel.DataAnnotations.ValidationResult>();
var validationAttributes = new global::System.Collections.Generic.List<global::System.ComponentModel.DataAnnotations.ValidationAttribute>(1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ internal sealed partial class __EndPointsOptionsValidator__
public static global::Microsoft.Extensions.Options.ValidateOptionsResult Validate(string? name, global::Test.EndPointsOptions options)
{
global::Microsoft.Extensions.Options.ValidateOptionsResultBuilder? builder = null;
#if NET10_0_OR_GREATER
string displayName = string.IsNullOrEmpty(name) ? "EndPointsOptions.Validate" : $"{name}.Validate";
var context = new global::System.ComponentModel.DataAnnotations.ValidationContext(options, displayName, null, null);
#else
var context = new global::System.ComponentModel.DataAnnotations.ValidationContext(options);
#endif
var validationResults = new global::System.Collections.Generic.List<global::System.ComponentModel.DataAnnotations.ValidationResult>();
var validationAttributes = new global::System.Collections.Generic.List<global::System.ComponentModel.DataAnnotations.ValidationAttribute>(2);

Expand Down Expand Up @@ -59,7 +64,12 @@ internal sealed partial class __RedisClientOptionsValidator__
public static global::Microsoft.Extensions.Options.ValidateOptionsResult Validate(string? name, global::Test.RedisClientOptions options)
{
global::Microsoft.Extensions.Options.ValidateOptionsResultBuilder? builder = null;
#if NET10_0_OR_GREATER
string displayName = string.IsNullOrEmpty(name) ? "RedisClientOptions.Validate" : $"{name}.Validate";
var context = new global::System.ComponentModel.DataAnnotations.ValidationContext(options, displayName, null, null);
#else
var context = new global::System.ComponentModel.DataAnnotations.ValidationContext(options);
#endif
var validationResults = new global::System.Collections.Generic.List<global::System.ComponentModel.DataAnnotations.ValidationResult>();
var validationAttributes = new global::System.Collections.Generic.List<global::System.ComponentModel.DataAnnotations.ValidationAttribute>(1);

Expand Down Expand Up @@ -106,7 +116,12 @@ partial class RedisNamedClientOptions
public global::Microsoft.Extensions.Options.ValidateOptionsResult Validate(string? name, global::Test.RedisNamedClientOptions options)
{
global::Microsoft.Extensions.Options.ValidateOptionsResultBuilder? builder = null;
#if NET10_0_OR_GREATER
string displayName = string.IsNullOrEmpty(name) ? "RedisNamedClientOptions.Validate" : $"{name}.Validate";
var context = new global::System.ComponentModel.DataAnnotations.ValidationContext(options, displayName, null, null);
#else
var context = new global::System.ComponentModel.DataAnnotations.ValidationContext(options);
#endif
var validationResults = new global::System.Collections.Generic.List<global::System.ComponentModel.DataAnnotations.ValidationResult>();
var validationAttributes = new global::System.Collections.Generic.List<global::System.ComponentModel.DataAnnotations.ValidationAttribute>(1);

Expand Down
Loading
Loading