-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Fix config binder source gen for a sole read-only collection ctor param #131358
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
TemRevil
wants to merge
2
commits into
dotnet:main
Choose a base branch
from
TemRevil:fix/config-binder-solo-readonly-collection-ctor-param
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+125
−4
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -542,6 +542,97 @@ public record Options(string Name, {{collectionType}}<string> Values); | |
| AssertCanCreateAssemblyImage(result.OutputCompilation); | ||
| } | ||
|
|
||
| [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsNetCore))] | ||
| [InlineData("IReadOnlyList")] | ||
| [InlineData("IReadOnlyCollection")] | ||
| [InlineData("IReadOnlySet")] | ||
| [InlineData("IEnumerable")] | ||
| public async Task SoleReadOnlyCollectionConstructorParameterIsBindable(string collectionType) | ||
| { | ||
| // Regression test: a type whose only member is a non-bindable, read-only collection | ||
| // constructor parameter (no other bindable property) used to make the generator emit a | ||
| // call to an Initialize method that was never generated, producing CS0103 at compile time. | ||
| string source = $$""" | ||
| using Microsoft.Extensions.Configuration; | ||
| using System.Collections.Generic; | ||
|
|
||
| public class Program | ||
| { | ||
| public static object? Result; | ||
|
|
||
| public static void Main() | ||
| { | ||
| ConfigurationBuilder configurationBuilder = new(); | ||
| configurationBuilder.AddInMemoryCollection(new Dictionary<string, string?> | ||
| { | ||
| ["Values:0"] = "a", | ||
| ["Values:1"] = "b", | ||
| }); | ||
| IConfiguration config = configurationBuilder.Build(); | ||
| Options options = config.Get<Options>(); | ||
| Result = options.Values; | ||
| } | ||
| } | ||
|
|
||
| public record Options({{collectionType}}<string> Values); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two coverage gaps around this shape:
|
||
| """; | ||
|
|
||
| ConfigBindingGenRunResult result = await RunGeneratorAndUpdateCompilation(source, assemblyReferences: GetAssemblyRefsWithAdditional(typeof(ConfigurationBuilder), typeof(List<>))); | ||
| Assert.NotNull(result.GeneratedSource); | ||
| Assert.Empty(result.Diagnostics); | ||
|
|
||
| // Compiling only proves the Initialize method the fix registers is emitted; loading and | ||
| // running the assembly proves it also binds the right values, not just compilable code. | ||
| var boundValues = (IEnumerable<string>)LoadAndInvokeMain(result.OutputCompilation, "Result")!; | ||
| Assert.Equal(new[] { "a", "b" }, boundValues); | ||
| } | ||
|
|
||
| [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNetCore))] | ||
| public async Task SoleReadOnlyCollectionConstructorParameterOfComplexElementIsBindable() | ||
| { | ||
| // Same regression as SoleReadOnlyCollectionConstructorParameterIsBindable, but the element | ||
| // type is itself a bindable object rather than a string. ComplexReadOnlyListConstructorParameterIsBindable | ||
| // below covers the complex-element case, but always pairs it with a second, ordinarily-bindable | ||
| // property, so it never exercises the fixed code path (the type has bindable members either way). | ||
| string source = """ | ||
| using Microsoft.Extensions.Configuration; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
|
|
||
| public class Program | ||
| { | ||
| public static object? Result; | ||
|
|
||
| public static void Main() | ||
| { | ||
| ConfigurationBuilder configurationBuilder = new(); | ||
| configurationBuilder.AddInMemoryCollection(new Dictionary<string, string?> | ||
| { | ||
| ["Values:0:Value"] = "a", | ||
| ["Values:1:Value"] = "b", | ||
| }); | ||
| IConfiguration config = configurationBuilder.Build(); | ||
| Options options = config.Get<Options>(); | ||
| Result = options.Values.Select(v => v.Value).ToArray(); | ||
| } | ||
| } | ||
|
|
||
| public class Child | ||
| { | ||
| public string Value { get; set; } | ||
| } | ||
|
|
||
| public record Options(IReadOnlyList<Child> Values); | ||
| """; | ||
|
|
||
| ConfigBindingGenRunResult result = await RunGeneratorAndUpdateCompilation(source, assemblyReferences: GetAssemblyRefsWithAdditional(typeof(ConfigurationBuilder), typeof(List<>))); | ||
| Assert.NotNull(result.GeneratedSource); | ||
| Assert.Empty(result.Diagnostics); | ||
|
|
||
| var boundValues = (string[])LoadAndInvokeMain(result.OutputCompilation, "Result")!; | ||
| Assert.Equal(new[] { "a", "b" }, boundValues); | ||
| } | ||
|
|
||
| [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNetCore))] | ||
| public async Task ComplexReadOnlyListConstructorParameterIsBindable() | ||
| { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.