Skip to content

Explicit named arguments with null value do not work #663

@avonwyss

Description

@avonwyss

I know StructureMap is sunsetted and this may not get fixed, but just in case someone has the same problem:

When adding explicit arguments either via ExplicitArguments on GetInstance<>(args) or defining them in the registry via .Ctor("name"), StructureMap thinks that the argument has not been defined at all. This actually used to work in 2.6.

Here are some test cases which all fail:

[Fact]
public void supply_named_arguments_null_ctor()
{
    var container = new Container(x => { x.For<IWidget>().Use<ColorWidget>().Ctor<string>().Is(default(string)); });

    container.With("color").EqualTo(null)
        .GetInstance<IWidget>()
        .ShouldBeOfType<ColorWidget>()
        .Color.ShouldBeNull();
}

[Fact]
public void supply_named_arguments_null_arg1()
{
    var container = new Container(x => { x.For<IWidget>().Use<ColorWidget>().Ctor<string>().Is("Red"); });

    container.GetInstance<IWidget>()
        .ShouldBeOfType<ColorWidget>()
        .Color.ShouldBe("Red");

    container.With("color").EqualTo(null)
        .GetInstance<IWidget>()
        .ShouldBeOfType<ColorWidget>()
        .Color.ShouldBeNull();
}


[Fact]
public void supply_named_arguments_null_arg2()
{
    var container = new Container(x => { x.For<IWidget>().Use<ColorWidget>().Ctor<string>().Is("Red"); });

    container.GetInstance<IWidget>()
        .ShouldBeOfType<ColorWidget>()
        .Color.ShouldBe("Red");

    var args = new ExplicitArguments();
    args.SetArg("color", null);
    container.GetInstance<IWidget>(args)
        .ShouldBeOfType<ColorWidget>()
        .Color.ShouldBeNull();
}

I would expect these to be a valid setup, but the following error occurs in the first two:

StructureMap.StructureMapBuildPlanException : Unable to create a build plan for concrete type StructureMap.Testing.Acceptance.ColorWidget

new ColorWidget(String color)
  ┗ String color = Required primitive dependency is not explicitly defined

1.) Attempting to create a BuildPlan for Instance of StructureMap.Testing.Acceptance.IWidget (a7a59459-f11f-4554-9dec-6fd067bf1b83) -- StructureMap.Testing.Acceptance.ColorWidget ('a7a59459-f11f-4554-9dec-6fd067bf1b83')
2.) Container.GetInstance<StructureMap.Testing.Acceptance.IWidget>({color=})

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions