Skip to content

Commit

Permalink
added Should_skip_required_property_injection_when_using_ctor_with_Se…
Browse files Browse the repository at this point in the history
…tsRequiredProperties for #563
  • Loading branch information
dadhi committed Apr 18, 2023
1 parent 7e0f63f commit b288578
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/DryIoc/Container.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace DryIoc
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics; // for StackTrace
using System.Diagnostics.CodeAnalysis; // for SuppressMessage
using System.Diagnostics.CodeAnalysis; // for SuppressMessage, SetsRequiredPropertiesAttribute
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
Expand Down
31 changes: 29 additions & 2 deletions test/DryIoc.UnitTests/RequiredPropertiesTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NUnit.Framework;
using System.Diagnostics.CodeAnalysis;
using NUnit.Framework;

namespace DryIoc.UnitTests
{
Expand All @@ -12,7 +13,8 @@ public int Run()
{
Can_inject_required_properties();
Should_throw_for_unresolved_required_property();
return 2;
Should_skip_required_property_injection_when_using_ctor_with_SetsRequiredProperties();
return 3;
}

[Test]
Expand Down Expand Up @@ -46,6 +48,22 @@ public void Should_throw_for_unresolved_required_property()
Assert.AreEqual(Error.NameOf(Error.UnableToResolveUnknownService), ex.ErrorName);
}

[Test]
public void Should_skip_required_property_injection_when_using_ctor_with_SetsRequiredProperties()
{
var c = new Container(Rules.Default.With(propertiesAndFields: PropertiesAndFields.RequiredProperties()));

c.Register<SS>();

c.Register<A>();
c.Register<B>();

var x = c.Resolve<SS>();

Assert.Null(x.A);
Assert.NotNull(x.B);
}

public class A {}
public class B {}
public class C {}
Expand All @@ -63,6 +81,15 @@ public class BS
{
public required D D { protected get; set; }
}

public class SS
{
public required A A { get; init; }
public B B { get; private set; }

[SetsRequiredMembers]
public SS(B b) => B = b;
}
#endif
}
}

0 comments on commit b288578

Please sign in to comment.