Skip to content
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

Configuration binding source generator doesn't handle generics correctly #89273

Closed
eerhardt opened this issue Jul 20, 2023 · 3 comments · Fixed by #89537
Closed

Configuration binding source generator doesn't handle generics correctly #89273

eerhardt opened this issue Jul 20, 2023 · 3 comments · Fixed by #89537
Assignees
Labels
area-Extensions-Configuration blocking-release bug source-generator Indicates an issue with a source generator feature
Milestone

Comments

@eerhardt
Copy link
Member

When trying to use the configuration binder source generator with an expression that uses a closed-generic type, I'm getting a warning saying it isn't supported.

However, since the expression is a closed-generic type, the type is able to be understood. In this case, the type is OidcProviderOptions.

We have this case in ASP.NET Auth. Specifically the Blazor WASM project templates use this exact pattern.

https://github.com/dotnet/aspnetcore/blob/24bbfb6d8398684091cc970bd342df185c8f9421/src/ProjectTemplates/Web.ProjectTemplates/content/ComponentsWebAssembly-CSharp/Program.cs#L15C1-L21

Repro steps

dotnet build the following app.

using Microsoft.Extensions.Configuration;

var config = new ConfigurationBuilder().Build();

AddOidcAuthentication(options =>
{
    config.Bind("Local", options.ProviderOptions);  // warning HERE
});

static void AddOidcAuthentication(Action<RemoteAuthenticationOptions<OidcProviderOptions>> configure)
{
    configure(new());
}

public class RemoteAuthenticationOptions<TRemoteAuthenticationProviderOptions> where TRemoteAuthenticationProviderOptions : new()
{
    public TRemoteAuthenticationProviderOptions ProviderOptions { get; } = new TRemoteAuthenticationProviderOptions();
}

public class OidcProviderOptions
{
    public string? Authority { get; set; }
}
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0-preview.7.23370.3" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.0-preview.7.23370.3" />
  </ItemGroup>

</Project>

Expected results

The above app should build successfully with no warnings or errors.

Actual results

Program.cs(7,5): warning SYSLIB1104: Binding logic was not generated for a binder call. Unsupported input patterns include generic calls and passing boxed objects.
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Jul 20, 2023
@ghost
Copy link

ghost commented Jul 20, 2023

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

Issue Details

When trying to use the configuration binder source generator with an expression that uses a closed-generic type, I'm getting a warning saying it isn't supported.

However, since the expression is a closed-generic type, the type is able to be understood. In this case, the type is OidcProviderOptions.

We have this case in ASP.NET Auth. Specifically the Blazor WASM project templates use this exact pattern.

https://github.com/dotnet/aspnetcore/blob/24bbfb6d8398684091cc970bd342df185c8f9421/src/ProjectTemplates/Web.ProjectTemplates/content/ComponentsWebAssembly-CSharp/Program.cs#L15C1-L21

Repro steps

dotnet build the following app.

using Microsoft.Extensions.Configuration;

var config = new ConfigurationBuilder().Build();

AddOidcAuthentication(options =>
{
    config.Bind("Local", options.ProviderOptions);  // warning HERE
});

static void AddOidcAuthentication(Action<RemoteAuthenticationOptions<OidcProviderOptions>> configure)
{
    configure(new());
}

public class RemoteAuthenticationOptions<TRemoteAuthenticationProviderOptions> where TRemoteAuthenticationProviderOptions : new()
{
    public TRemoteAuthenticationProviderOptions ProviderOptions { get; } = new TRemoteAuthenticationProviderOptions();
}

public class OidcProviderOptions
{
    public string? Authority { get; set; }
}
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0-preview.7.23370.3" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.0-preview.7.23370.3" />
  </ItemGroup>

</Project>

Expected results

The above app should build successfully with no warnings or errors.

Actual results

Program.cs(7,5): warning SYSLIB1104: Binding logic was not generated for a binder call. Unsupported input patterns include generic calls and passing boxed objects.
Author: eerhardt
Assignees: -
Labels:

area-Extensions-Configuration

Milestone: -

@tarekgh tarekgh added this to the 8.0.0 milestone Jul 20, 2023
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Jul 20, 2023
@tarekgh tarekgh added untriaged New issue has not been triaged by the area owner source-generator Indicates an issue with a source generator feature labels Jul 20, 2023
@tarekgh tarekgh removed the untriaged New issue has not been triaged by the area owner label Jul 20, 2023
@layomia
Copy link
Contributor

layomia commented Jul 20, 2023

Thanks, I'll take a look. I think I fixed something similar in e4e0810#diff-c941b7a8491ae8b0d73a97808f886062a388c3d59da08b281d56eb0c2afbc2c5R126.

@layomia
Copy link
Contributor

layomia commented Jul 20, 2023

We have this case in ASP.NET Auth. Specifically the Blazor WASM project templates use this exact pattern.

@eerhardt per our offline discussion, there's a workaround for the templates (go back to disabling the generator for now) and we don't need a fix for this in Preview 7. We'll make a fix in RC.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-Extensions-Configuration blocking-release bug source-generator Indicates an issue with a source generator feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants