Skip to content

The configuration binder source generator fails to add the correct namespace when dealing with a dictionary root type #118630

@ZzZombo

Description

@ZzZombo

Description

var dict = conf.GetSection(sectionPrefix).Get<IDictionary<string, string>>();

generates code w/o the correct System.Collections.Generic namespace for public static void ValidateConfigurationKeys(Type type, Lazy<HashSet<string>> keys, IConfiguration configuration, BinderOptions? binderOptions) where it declares a Lazy<HashSet<string>> argument.

Reproduction Steps

See above.

Expected behavior

The generator produces compliable code.

Actual behavior

But it doesn't.

Regression?

No response

Known Workarounds

Global/implicit using directives can be used as a workaround.

Configuration

Which version of .NET is the code running on? .NET 9.0.
What OS and version, and what distro if applicable? Windows 10.
What is the architecture (x64, x86, ARM, ARM64)? x64.
Do you know whether it is specific to that configuration? No idea.
If you're using Blazor, which web browser(s) do you see this issue in? N/A.

Other information

The line

adds this namespace to the generated output only in one case: ObjectSpec root type, but it generates the validation method assuming the namespace is added unconditionally. I think just making it do so should fix this, worst case scenario it adds a redundant used namespace declaration, which I think is negligible.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions