Skip to content

Commit 506060e

Browse files
Keboojonsequitur
authored andcommitted
Fix so that if two paraters have the same type and different defaults they are properly resolved.
1 parent a409f24 commit 506060e

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

src/System.CommandLine.DragonFruit.Tests/ConfigureFromMethodTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,17 @@ public async Task Method_parameters_on_the_invoked_member_method_are_bound_to_ma
235235
_receivedValues.Should().BeEquivalentTo(true);
236236
}
237237

238+
[Fact]
239+
public async Task Method_with_multiple_parameters_with_default_values_are_resolved_correctly()
240+
{
241+
var command = new Command("test");
242+
command.ConfigureFromMethod(GetMethodInfo(nameof(Method_with_multiple_default_values)), this);
243+
244+
await command.InvokeAsync("");
245+
246+
_receivedValues.Should().BeEquivalentTo(1, 2);
247+
}
248+
238249
internal void Method_taking_bool(bool value = false)
239250
{
240251
_receivedValues = new object[] { value };
@@ -294,6 +305,11 @@ internal void Method_having_FileInfo_array_args(string stringOption, int intOpti
294305
{
295306
}
296307

308+
internal void Method_with_multiple_default_values(int firstValue = 1, int secondValue = 2)
309+
{
310+
_receivedValues = new object[] { firstValue, secondValue };
311+
}
312+
297313
private MethodInfo GetMethodInfo(string name)
298314
{
299315
return typeof(ConfigureFromMethodTests)

src/System.CommandLine.DragonFruit/CommandLine.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation and contributors. All rights reserved.
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using System.Collections.Generic;
@@ -265,9 +265,9 @@ internal static string BuildAlias(string parameterName)
265265
: $"-{parameterName.ToLowerInvariant()}";
266266
}
267267

268-
public static IEnumerable<Option> BuildOptions(this MethodInfo type)
268+
public static IEnumerable<Option> BuildOptions(this MethodInfo method)
269269
{
270-
var descriptor = HandlerDescriptor.FromMethodInfo(type);
270+
var descriptor = HandlerDescriptor.FromMethodInfo(method);
271271

272272
var omittedTypes = new[]
273273
{

src/System.CommandLine/Binding/BindingContext.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,14 @@ public IConsole Console
5353
public void AddModelBinder(ModelBinder binder) =>
5454
_modelBindersByValueDescriptor.Add(binder.ValueDescriptor.ValueType, binder);
5555

56-
public ModelBinder GetModelBinder(IValueDescriptor valueDescriptor) =>
57-
_modelBindersByValueDescriptor.GetOrAdd(
58-
valueDescriptor.ValueType,
59-
_ => new ModelBinder(valueDescriptor));
56+
public ModelBinder GetModelBinder(IValueDescriptor valueDescriptor)
57+
{
58+
if (_modelBindersByValueDescriptor.TryGetValue(valueDescriptor.ValueType, out ModelBinder binder))
59+
{
60+
return binder;
61+
}
62+
return new ModelBinder(valueDescriptor);
63+
}
6064

6165
public void AddService(Type serviceType, Func<IServiceProvider, object> factory)
6266
{

0 commit comments

Comments
 (0)