Skip to content
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

Fix #1427: Ensure WithProperty registrations consistently allow null values; update nullable annotations #1428

Merged
merged 3 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Autofac/Core/NamedPropertyParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class NamedPropertyParameter : ConstantParameter
/// </summary>
/// <param name="name">The name of the property.</param>
/// <param name="value">The property value.</param>
public NamedPropertyParameter(string name, object value)
public NamedPropertyParameter(string name, object? value)
: base(value, pi =>
{
return pi.TryGetDeclaringProperty(out PropertyInfo? prop) &&
Expand Down
2 changes: 1 addition & 1 deletion src/Autofac/NamedParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class NamedParameter : ConstantParameter
/// </summary>
/// <param name="name">The name of the parameter.</param>
/// <param name="value">The parameter value.</param>
public NamedParameter(string name, object value)
public NamedParameter(string name, object? value)
: base(value, pi => pi.Name == name) =>
Name = Enforce.ArgumentNotNullOrEmpty(name, "name");
}
2 changes: 1 addition & 1 deletion src/Autofac/PositionalParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class PositionalParameter : ConstantParameter
/// </summary>
/// <param name="position">The zero-based position of the parameter.</param>
/// <param name="value">The parameter value.</param>
public PositionalParameter(int position, object value)
public PositionalParameter(int position, object? value)
: base(value, pi => pi.Position == position && (pi.Member is ConstructorInfo))
{
if (position < 0)
Expand Down
7 changes: 1 addition & 6 deletions src/Autofac/RegistrationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ public static IRegistrationBuilder<TLimit, TReflectionActivatorData, TStyle>
WithProperty<TLimit, TReflectionActivatorData, TStyle>(
this IRegistrationBuilder<TLimit, TReflectionActivatorData, TStyle> registration,
string propertyName,
object propertyValue)
object? propertyValue)
where TReflectionActivatorData : ReflectionActivatorData
{
return registration.WithProperty(new NamedPropertyParameter(propertyName, propertyValue));
Expand Down Expand Up @@ -684,11 +684,6 @@ public static IRegistrationBuilder<TLimit, TReflectionActivatorData, TStyle>
throw new ArgumentNullException(nameof(propertyExpression));
}

if (propertyValue == null)
{
throw new ArgumentNullException(nameof(propertyValue));
}

var propertyInfo = (propertyExpression.Body as MemberExpression)?.Member as PropertyInfo ?? throw new ArgumentOutOfRangeException(nameof(propertyExpression), RegistrationExtensionsResources.ExpressionDoesNotReferToProperty);
return registration.WithProperty(new NamedPropertyParameter(propertyInfo.Name, propertyValue));
}
Expand Down
33 changes: 33 additions & 0 deletions test/Autofac.Specification.Test/Features/PropertyInjectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,39 @@ public void DecoratedInstanceWithPropertyInjectionAllowingCircularReferencesStil
instance.AssertProp();
}

[Fact]
public void WithPropertyDelegateAllowsNullValue()
{
// Issue 1427: WithProperty should consistently allow null values.
var builder = new ContainerBuilder();
builder.RegisterType<HasPublicSetter>().WithProperty(t => t.Val, null);
var container = builder.Build();
var instance = container.Resolve<HasPublicSetter>();
Assert.Null(instance.Val);
}

[Fact]
public void WithPropertyNamedAllowsNullValue()
{
// Issue 1427: WithProperty should consistently allow null values.
var builder = new ContainerBuilder();
builder.RegisterType<HasPublicSetter>().WithProperty(nameof(HasPublicSetter.Val), null);
var container = builder.Build();
var instance = container.Resolve<HasPublicSetter>();
Assert.Null(instance.Val);
}

[Fact]
public void WithPropertyTypedAllowsNullValue()
{
// Issue 1427: WithProperty should consistently allow null values.
var builder = new ContainerBuilder();
builder.RegisterType<HasPublicSetter>().WithProperty(TypedParameter.From<string>(null));
var container = builder.Build();
var instance = container.Resolve<HasPublicSetter>();
Assert.Null(instance.Val);
}

private class ConstructorParamNotAttachedToProperty
{
[SuppressMessage("SA1401", "SA1401")]
Expand Down