Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Options validation source generator doesn't consider constructing ValidationContext with an IServiceProvider #115347

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

Open
jander-msft opened this issue May 6, 2025 · 3 comments
Labels
area-Extensions-Options source-generator Indicates an issue with a source generator feature
Milestone

Comments

@jander-msft
Copy link
Member

jander-msft commented May 6, 2025

Using the following example source:

public interface IMyValidationService { }

public sealed class MyOptions : IValidatableObject
{
    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        // This will throw since an IServiceProvider is not available in the ValidationContext
        _ = validationContext.GetRequiredService<IMyValidationService>();
        return [];
    }
}

[OptionsValidator]
public sealed partial class MyValidateOptions :
    IValidateOptions<MyOptions>
{
}

would generate an options validation implementation that looks like:

partial class MyValidateOptions
{
    /// <summary>
    /// Validates a specific named options instance (or all when <paramref name="name"/> is <see langword="null" />).
    /// </summary>
    /// <param name="name">The name of the options instance being validated.</param>
    /// <param name="options">The options instance.</param>
    /// <returns>Validation result.</returns>
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Options.SourceGeneration", "10.0.12.23009")]
    #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::Microsoft.Diagnostics.Tools.Monitor.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

        context.MemberName = "Validate";
        context.DisplayName = string.IsNullOrEmpty(name) ? "MyOptions.Validate" : $"{name}.Validate";
        (builder ??= new()).AddResults(((global::System.ComponentModel.DataAnnotations.IValidatableObject)options).Validate(context));

        return builder is null ? global::Microsoft.Extensions.Options.ValidateOptionsResult.Success : builder.Build();
    }
}

Note that the construction of the ValidationContext passes null for the IServiceProvider parameter.

It appears that the generator does not consider if an IServiceProvider is available: https://github.com/dotnet/runtime/blob/main/src/libraries/Microsoft.Extensions.Options/gen/Emitter.cs#L697

This prevents IValidatableObject implementations from using the ValidationContext.GetService method because it will always return null. Here are a few examples where .NET Monitor uses this and would prevent it from adopting options validation source generation:

Not to necessarily prescribe the solution, but it would be nice if the source generator would detect an IServiceProvider parameter from the primary constructor or possibly a field from the IValidateOptions<T> implementation and pass that instance when constructing the ValidationContext.

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label May 6, 2025
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-componentmodel-dataannotations
See info in area-owners.md if you want to be subscribed.

@tarekgh tarekgh added area-Extensions-Options source-generator Indicates an issue with a source generator feature and removed area-System.ComponentModel.DataAnnotations untriaged New issue has not been triaged by the area owner labels May 6, 2025
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-extensions-options
See info in area-owners.md if you want to be subscribed.

@tarekgh tarekgh added this to the Future milestone May 6, 2025
@tarekgh
Copy link
Member

tarekgh commented May 6, 2025

This is currently a limitation in the options source gen. I am wondering, can't you have the MyOptions created with the service provider?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-Extensions-Options source-generator Indicates an issue with a source generator feature
Projects
None yet
Development

No branches or pull requests

2 participants