Skip to content

Commit

Permalink
Merge pull request #1276 from autofac/feature/1275
Browse files Browse the repository at this point in the history
Fix #1275: Filter out 'value' named parameters during property injection
  • Loading branch information
alistairjevans authored May 14, 2021
2 parents 14dcb07 + 9e348f3 commit 14796de
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,12 @@ public static void InjectProperties(IComponentContext context, object instance,
// SetMethod will be non-null if GetInjectableProperties included it.
var setParameter = property.SetMethod!.GetParameters()[0];
var valueProvider = (Func<object?>?)null;
var parameter = resolveParameters.FirstOrDefault(p => p.CanSupplyValue(setParameter, context, out valueProvider));

// Issue #1275: If a delegate factory has a named parameter 'value' it will try to
// inject into any property being autowired.
var parameter = resolveParameters.FirstOrDefault(p =>
p.CanSupplyValue(setParameter, context, out valueProvider) &&
!(p is NamedParameter n && n.Name.Equals("value", StringComparison.Ordinal)));
if (parameter != null)
{
var setter = PropertySetters.GetOrAdd(property, MakeFastPropertySetter);
Expand Down
2 changes: 1 addition & 1 deletion src/Autofac/Features/Metadata/MetadataViewProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ internal static class MetadataViewProvider

if (defaultValue is object)
{
return (TValue)defaultValue.Value;
return (TValue?)defaultValue.Value;
}

throw new DependencyResolutionException(
Expand Down
2 changes: 1 addition & 1 deletion src/Autofac/Util/FallbackDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public TValue this[TKey key]
{
get
{
if (_localValues.TryGetValue(key, out TValue value))
if (_localValues.TryGetValue(key, out TValue? value))
{
return value;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Autofac Project. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

namespace Autofac.Specification.Test.Features.PropertyInjection
{
public class CtorWithValueParameter
{
public delegate CtorWithValueParameter Factory(string value);

// The testing with this class has to do with a constructor
// parameter that is named `value` - this property doesn't
// need to be filled in, it just needs to exist and not be
// a simple `object` or `string` or something.
public HasMixedVisibilityProperties Dummy { get; set; }

public CtorWithValueParameter(string value)
{
}
}
}
12 changes: 12 additions & 0 deletions test/Autofac.Specification.Test/Features/PropertyInjectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,18 @@ public void PropertiesAutowiredUsingPropertySelector()
Assert.Equal(str, obj.PrivateStringAccessor());
}

[Fact]
public void PropertiesAutowiredWithDelegateFactory()
{
// Issue #1275: A constructor parameter with the name 'value' messes
// up property injection when used in conjunction with delegate factories.
var cb = new ContainerBuilder();
cb.RegisterType<CtorWithValueParameter>().PropertiesAutowired();
var c = cb.Build();
var factory = c.Resolve<CtorWithValueParameter.Factory>();
Assert.NotNull(factory("test"));
}

[Fact]
public void PropertiesNotSetIfNotSpecified()
{
Expand Down

0 comments on commit 14796de

Please sign in to comment.