Skip to content

Commit 2d2819d

Browse files
svickCopilot
andcommitted
Address review: bind-existing-instance and required-nested coverage
Add the tests requested in review of dotnet#95006: - CanBindExistingInstance_InitOnlyCollectionOnParameterlessConstructorType: binds onto an already-existing instance of a parameterless-ctor type with an init-only collection, asserting the configured items are appended into the existing collection exactly once (not duplicated). This exercises the !boundThroughConstructor branch of BindCore, which only runs for existing instances. Runs under both binders. - CanBind_RequiredNestedComplexOnParameterlessConstructorType_SettableSetter and _InitOnlySetter (net only, since required needs RequiredMemberAttribute): assert the bound runtime values of a required nested complex property for both a settable and an init-only setter, complementing the compile-only RequiredPropertyOnParameterlessConstructorType generator test. Also document the known non-public-setter limitation: SetOnInit only considers public setters, so a required or init-only property with a non-public (e.g. internal) setter is not routed through the object initializer and a required property in that shape still produces CS9035, left out of scope here. Co-authored-by: Copilot <[email protected]> Copilot-Session: 1a0d7ac2-f2a3-4526-93ab-36bf1a23933f
1 parent c92b8d6 commit 2d2819d

3 files changed

Lines changed: 83 additions & 0 deletions

File tree

‎src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Specs/Members/PropertySpec.cs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ public PropertySpec(IPropertySymbol property, TypeRef typeRef) : base(property,
1515
bool isInitOnly = setMethod?.IsInitOnly is true;
1616

1717
IsStatic = property.IsStatic;
18+
// Only public setters are considered here, consistent with CanSet. A required or init-only property with a
19+
// non-public (e.g. internal) setter is therefore not treated as SetOnInit, so the generator does not emit
20+
// an object initializer for it; a required property in that shape still produces CS9035.
1821
SetOnInit = setterIsPublic && (property.IsRequired || isInitOnly);
1922
CanSet = setterIsPublic && !isInitOnly;
2023
CanGet = property.GetMethod?.DeclaredAccessibility is Accessibility.Public;

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,20 @@ public sealed class InitOnlyPropertiesWithNonNullDefaults
318318
public List<string> Items { get; init; } = new() { "preset" };
319319
}
320320

321+
#if NET
322+
public sealed class RequiredPropertiesParameterlessCtor
323+
{
324+
public required string Name { get; set; }
325+
public required NestedForInitOnly Child { get; set; }
326+
}
327+
328+
public sealed class RequiredInitPropertiesParameterlessCtor
329+
{
330+
public required string Name { get; init; }
331+
public required NestedForInitOnly Child { get; init; }
332+
}
333+
#endif
334+
321335
public readonly record struct ReadonlyRecordStructTypeOptions(string Color, int Length);
322336

323337
public class ContainerWithNestedImmutableObject

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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1918,6 +1918,72 @@ public void CanBind_InitOnlyCollectionOnParameterlessConstructorType()
19181918
Assert.Equal(new[] { "a", "b" }, result.Items);
19191919
}
19201920

1921+
/// <summary>
1922+
/// When binding onto an already-existing instance of a parameterless-constructor type with an init-only
1923+
/// collection, the instance is not created through the generated Initialize method, so the collection is bound
1924+
/// in BindCore (through the !boundThroughConstructor branch). Its items must be appended into the existing
1925+
/// collection exactly once, not duplicated, matching the reflection binder.
1926+
/// </summary>
1927+
[Fact]
1928+
public void CanBindExistingInstance_InitOnlyCollectionOnParameterlessConstructorType()
1929+
{
1930+
string json = """
1931+
{
1932+
"Items": [ "a", "b" ]
1933+
}
1934+
""";
1935+
1936+
IConfiguration config = TestHelpers.GetConfigurationFromJsonString(json);
1937+
var instance = new ClassWithInitOnlyCollectionParameterlessCtor { Items = new List<string> { "existing" } };
1938+
1939+
config.Bind(instance);
1940+
1941+
Assert.Equal(new[] { "existing", "a", "b" }, instance.Items);
1942+
}
1943+
1944+
#if NET
1945+
/// <summary>
1946+
/// A required nested complex property on a parameterless-constructor type is assigned in the generated
1947+
/// Initialize method's object initializer and must be bound to the configured values, whether the required
1948+
/// property has a settable or an init-only setter.
1949+
/// </summary>
1950+
[Fact]
1951+
public void CanBind_RequiredNestedComplexOnParameterlessConstructorType_SettableSetter()
1952+
{
1953+
string json = """
1954+
{
1955+
"Name": "n",
1956+
"Child": { "Value": "hello" }
1957+
}
1958+
""";
1959+
1960+
IConfiguration config = TestHelpers.GetConfigurationFromJsonString(json);
1961+
1962+
RequiredPropertiesParameterlessCtor result = config.Get<RequiredPropertiesParameterlessCtor>();
1963+
1964+
Assert.Equal("n", result.Name);
1965+
Assert.Equal("hello", result.Child.Value);
1966+
}
1967+
1968+
[Fact]
1969+
public void CanBind_RequiredNestedComplexOnParameterlessConstructorType_InitOnlySetter()
1970+
{
1971+
string json = """
1972+
{
1973+
"Name": "n",
1974+
"Child": { "Value": "hello" }
1975+
}
1976+
""";
1977+
1978+
IConfiguration config = TestHelpers.GetConfigurationFromJsonString(json);
1979+
1980+
RequiredInitPropertiesParameterlessCtor result = config.Get<RequiredInitPropertiesParameterlessCtor>();
1981+
1982+
Assert.Equal("n", result.Name);
1983+
Assert.Equal("hello", result.Child.Value);
1984+
}
1985+
#endif
1986+
19211987
/// <summary>
19221988
/// Documents a known difference between the reflection binder and the source generator for an init-only
19231989
/// property that has a non-null field-initializer default.

0 commit comments

Comments
 (0)