From 061efb44ba96762145f519fd61282ad4124acce9 Mon Sep 17 00:00:00 2001 From: Travis Illig Date: Sun, 15 Sep 2024 16:09:56 -0700 Subject: [PATCH] Add tests to validate fix for #1427. --- .../Features/PropertyInjectionTests.cs | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/test/Autofac.Specification.Test/Features/PropertyInjectionTests.cs b/test/Autofac.Specification.Test/Features/PropertyInjectionTests.cs index 0165a495f..b95b74bc8 100644 --- a/test/Autofac.Specification.Test/Features/PropertyInjectionTests.cs +++ b/test/Autofac.Specification.Test/Features/PropertyInjectionTests.cs @@ -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().WithProperty(t => t.Val, null); + var container = builder.Build(); + var instance = container.Resolve(); + Assert.Null(instance.Val); + } + + [Fact] + public void WithPropertyNamedAllowsNullValue() + { + // Issue 1427: WithProperty should consistently allow null values. + var builder = new ContainerBuilder(); + builder.RegisterType().WithProperty(nameof(HasPublicSetter.Val), null); + var container = builder.Build(); + var instance = container.Resolve(); + Assert.Null(instance.Val); + } + + [Fact] + public void WithPropertyTypedAllowsNullValue() + { + // Issue 1427: WithProperty should consistently allow null values. + var builder = new ContainerBuilder(); + builder.RegisterType().WithProperty(TypedParameter.From(null)); + var container = builder.Build(); + var instance = container.Resolve(); + Assert.Null(instance.Val); + } + private class ConstructorParamNotAttachedToProperty { [SuppressMessage("SA1401", "SA1401")]