Skip to content

Commit d94bcbd

Browse files
tarekghCopilot
andauthored
Fix config source gen with Dictionary with nullable Enum value (#115350)
* Fix config source gen with Dictionary with nullable Enum value * Update src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/CoreBindingHelpers.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update the fix to match the collection binding --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 8d07196 commit d94bcbd

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/CoreBindingHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ private void EmitBindCoreImplForDictionary(DictionarySpec type)
746746
Emit_Foreach_Section_In_ConfigChildren_StartBlock();
747747

748748
ParsableFromStringSpec keyType = (ParsableFromStringSpec)_typeIndex.GetEffectiveTypeSpec(type.KeyTypeRef);
749-
TypeSpec elementType = _typeIndex.GetTypeSpec(type.ElementTypeRef);
749+
TypeSpec elementType = _typeIndex.GetEffectiveTypeSpec(type.ElementTypeRef);
750750

751751
// Parse key
752752
EmitBindingLogic(

src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.TestClasses.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,5 +1131,21 @@ public class ParsableValuesClass
11311131
public Guid? GuidValue { get; set; }
11321132
public StringComparison? StringComparisonValue { get; set; }
11331133
}
1134+
1135+
public class OptionsWithCollectionsWithNullableEnum
1136+
{
1137+
// uses MyValue? dictionary values
1138+
public Dictionary<string, MyValue?> Dictionary { get; set; } = new();
1139+
1140+
// uses MyValue? List values
1141+
public List<MyValue?> List { get; set; } = new();
1142+
}
1143+
1144+
public enum MyValue
1145+
{
1146+
Value1,
1147+
Value2,
1148+
Value3
1149+
}
11341150
}
11351151
}

src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2816,6 +2816,29 @@ public void CanGetEnumerableNotCollection()
28162816
Assert.Equal(new [] { "new", "class", "rosebud"}, result.Keywords);
28172817
}
28182818

2819+
[Fact]
2820+
public void TestDictionaryWithNullableEnumValueType()
2821+
{
2822+
var builder = new ConfigurationBuilder()
2823+
.AddInMemoryCollection(new Dictionary<string, string?>
2824+
{
2825+
{ "Settings:Dictionary:Key1", "Value2" },
2826+
{ "Settings:List:1", "Value3" },
2827+
});
2828+
var config = builder.Build();
2829+
2830+
var settingsSection = config.GetSection("Settings");
2831+
OptionsWithCollectionsWithNullableEnum settings = settingsSection.Get<OptionsWithCollectionsWithNullableEnum>()!;
2832+
2833+
Assert.NotNull(settings.Dictionary);
2834+
Assert.Equal(1, settings.Dictionary.Count);
2835+
Assert.Equal(MyValue.Value2, settings.Dictionary["Key1"]);
2836+
2837+
Assert.NotNull(settings.List);
2838+
Assert.Equal(1, settings.List.Count);
2839+
Assert.Equal(MyValue.Value3, settings.List[0]);
2840+
}
2841+
28192842
#if !BUILDING_SOURCE_GENERATOR_TESTS
28202843
[Fact]
28212844
public void EnsureThrowingWithCollectionAndErrorOnUnknownConfigurationOption()

0 commit comments

Comments
 (0)