Add getter-only configuration binding parity tests - #131593
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
|
Tagging subscribers to this area: @dotnet/area-extensions-configuration |
There was a problem hiding this comment.
Pull request overview
Adds new xUnit coverage to ensure configuration binding behavior for getter-only properties stays consistent (and non-throwing) across the binder implementations, focusing on binding into existing instances vs ignoring null getter-only properties.
Changes:
- Added a new test model type with getter-only nested/collection/abstract-typed properties that can be pre-initialized or left null.
- Added tests verifying binding mutates existing non-null instances and ignores null getter-only properties without throwing.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.TestClasses.cs | Adds ClassWithGetterOnlyProperties test type used to exercise getter-only binding scenarios. |
| src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs | Adds two new [Fact] tests validating binding behavior for getter-only properties when values are initialized vs null. |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (3)
src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.TestClasses.cs:1097
- To make the null-values case a stronger regression test for “skip binding when getter-only property value is null”, it helps if the collection element type can actually fail to bind. With
List<string>, virtually any configuration value binds successfully, so a regression that allocates/binds a temporary list (then discards it) would still pass. Consider switching this toList<int>so the test can use non-parsable values and fail if binding is attempted.
public NestedOptions? Nested { get; }
public List<string>? Collection { get; }
public AbstractBase? Abstract { get; }
src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs:2965
- If
Collectionis switched toList<int>, update the non-null case JSON and assertion accordingly; this keeps the test validating that binding into an existing getter-only collection instance actually happened (and didn’t just no-op).
{
"Nested": { "Integer": 1 },
"Collection": [ "item" ],
"Abstract": { "Value": 2 }
}
src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs:2978
- The null-values test currently uses bindable values for
NestedandCollection, so a regression that still tries to bind a temporary instance for null getter-only properties (then discards it) could pass without being detected. Consider using deliberately non-parsable values so the test fails if binding is attempted (this becomes especially effective onceCollectionisList<int>).
IConfiguration configuration = TestHelpers.GetConfigurationFromJsonString(
"""
{
"Nested": { "Integer": 1 },
"Collection": [ "item" ],
"Abstract": { "Value": 2 }
}
""");
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Adds the shared reflection/source-generator parity tests requested in this review comment: #131045 (review).
The tests cover get-only nested objects, collections, and abstract-typed properties when their existing values are both non-null and null. They verify that existing instances are bound and null properties are ignored without throwing.
Note
This pull request was created with GitHub Copilot.