-
Notifications
You must be signed in to change notification settings - Fork 5.3k
ConfigurationBinder should support binding private properties from ba… #71039
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
Changes from all commits
d0cecde
03d1d1d
1db4e96
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1751,20 +1751,45 @@ public void CanBindNullableNestedStructProperties() | |
| } | ||
|
|
||
| [Fact] | ||
| public void CanBindVirtualPropertiesWithoutDuplicates() | ||
| public void CanBindVirtualProperties() | ||
| { | ||
| ConfigurationBuilder configurationBuilder = new(); | ||
| configurationBuilder.AddInMemoryCollection(new Dictionary<string, string> | ||
| { | ||
| { "Test:0", "1" } | ||
| { $"{nameof(BaseClassWithVirtualProperty.Test)}:0", "1" }, | ||
| { $"{nameof(BaseClassWithVirtualProperty.TestGetSetOverriden)}", "2" }, | ||
| { $"{nameof(BaseClassWithVirtualProperty.TestGetOverriden)}", "3" }, | ||
| { $"{nameof(BaseClassWithVirtualProperty.TestSetOverriden)}", "4" }, | ||
| { $"{nameof(BaseClassWithVirtualProperty.TestNoOverriden)}", "5" }, | ||
| { $"{nameof(BaseClassWithVirtualProperty.TestVirtualSet)}", "6" } | ||
| }); | ||
| IConfiguration config = configurationBuilder.Build(); | ||
|
|
||
| var test = new ClassOverridingVirtualProperty(); | ||
| config.Bind(test); | ||
|
|
||
| Assert.Equal("1", Assert.Single(test.Test)); | ||
| Assert.Equal("2", test.TestGetSetOverriden); | ||
| Assert.Equal("3", test.TestGetOverriden); | ||
| Assert.Equal("4", test.TestSetOverriden); | ||
| Assert.Equal("5", test.TestNoOverriden); | ||
| Assert.Null(test.ExposeTestVirtualSet()); | ||
|
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. Why is this Null instead of
Contributor
Author
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. Because set-only property was not bound, which was existing behavior (it is not about code in |
||
| } | ||
|
|
||
| [Fact] | ||
| public void CanBindPrivatePropertiesFromBaseClass() | ||
| { | ||
| ConfigurationBuilder configurationBuilder = new(); | ||
| configurationBuilder.AddInMemoryCollection(new Dictionary<string, string> | ||
| { | ||
| { "PrivateProperty", "a" } | ||
| }); | ||
| IConfiguration config = configurationBuilder.Build(); | ||
|
|
||
| var test = new ClassOverridingVirtualProperty(); | ||
| config.Bind(test, b => b.BindNonPublicProperties = true); | ||
| Assert.Equal("a", test.ExposePrivatePropertyValue()); | ||
| } | ||
|
|
||
| private interface ISomeInterface | ||
| { | ||
|
|
@@ -1845,12 +1870,43 @@ public struct DeeplyNested | |
|
|
||
| public class BaseClassWithVirtualProperty | ||
| { | ||
| private string? PrivateProperty { get; set; } | ||
|
|
||
| public virtual string[] Test { get; set; } = System.Array.Empty<string>(); | ||
|
|
||
| public virtual string? TestGetSetOverriden { get; set; } | ||
| public virtual string? TestGetOverriden { get; set; } | ||
| public virtual string? TestSetOverriden { get; set; } | ||
|
|
||
| private string? _testVirtualSet; | ||
| public virtual string? TestVirtualSet | ||
| { | ||
| set => _testVirtualSet = value; | ||
| } | ||
|
|
||
| public virtual string? TestNoOverriden { get; set; } | ||
|
|
||
| public string? ExposePrivatePropertyValue() => PrivateProperty; | ||
|
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. We should also add tests with different get/set combinations. For example:
Contributor
Author
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. Fixed, thanks. |
||
| } | ||
|
|
||
| public class ClassOverridingVirtualProperty : BaseClassWithVirtualProperty | ||
| { | ||
| public override string[] Test { get => base.Test; set => base.Test = value; } | ||
|
|
||
| public override string? TestGetSetOverriden { get; set; } | ||
| public override string? TestGetOverriden => base.TestGetOverriden; | ||
| public override string? TestSetOverriden | ||
| { | ||
| set => base.TestSetOverriden = value; | ||
| } | ||
|
|
||
| private string? _testVirtualSet; | ||
| public override string? TestVirtualSet | ||
| { | ||
| set => _testVirtualSet = value; | ||
| } | ||
|
|
||
| public string? ExposeTestVirtualSet() => _testVirtualSet; | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this just be:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed, I just realize Reflection use dynamic lookup so that base-most virtual properties can also be ok, thanks .