Skip to content

Commit 6b06884

Browse files
feat: fix failiure when multiple generic overloads
1 parent 88e1c24 commit 6b06884

2 files changed

Lines changed: 20 additions & 12 deletions

File tree

src/Refit/RequestBuilderImplementation.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,9 +352,16 @@ private RestMethodInfoInternal FindMatchingRestMethodInfo(
352352

353353
foreach (var method in possibleMethods)
354354
{
355-
if (ParametersMatch(method.MethodInfo.GetParameters(), parameterTypes))
355+
try
356+
{
357+
var closedMethod = CloseGenericMethodIfNeeded(method, genericArgumentTypes);
358+
if (ParametersMatch(closedMethod.MethodInfo.GetParameters(), parameterTypes))
359+
{
360+
return closedMethod;
361+
}
362+
}
363+
catch (Exception exception) when (exception.Message.Contains("violates the constraint", StringComparison.CurrentCultureIgnoreCase))
356364
{
357-
return CloseGenericMethodIfNeeded(method, genericArgumentTypes);
358365
}
359366
}
360367

src/tests/Refit.Tests/ReflectionTests.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,20 @@ public interface IGenericMethodAndInterface<in TInterface>
6868
/// <summary>Request with a generic route parameter and additional parameter.</summary>
6969
/// <param name="value1">Generic route parameter.</param>
7070
/// <param name="value2">Integer route parameter.</param>
71-
/// <typeparam name="T1">Generic type of route parameter.</typeparam>
71+
/// <typeparam name="T1">Generic type of route parameter, constrained to value types.</typeparam>
7272
/// <returns>A task that completes when the request finishes.</returns>
7373
[Get("/{value1}/{value2}")]
74-
Task GetGenericParameter<T1>(T1 value1, int value2);
74+
Task GetGenericParameter<T1>(T1 value1, int value2)
75+
where T1 : struct;
7576

76-
// This should be renamed to GetGenericParameter once #2197 is fixed.
7777
/// <summary>Request with two generic route parameters.</summary>
7878
/// <param name="value1">First generic route parameter.</param>
7979
/// <param name="value2">Second generic route parameter.</param>
80-
/// <typeparam name="T1">Generic type of first route parameter.</typeparam>
80+
/// <typeparam name="T1">Generic type of first route parameter, constrained to reference types.</typeparam>
8181
/// <returns>A task that completes when the request finishes.</returns>
8282
[Get("/{value1}/{value2}")]
83-
Task GetClassGenericParameter<T1>(T1 value1, TInterface value2);
83+
Task GetGenericParameter<T1>(T1 value1, TInterface value2)
84+
where T1 : class;
8485
}
8586

8687
/// <summary>Verifies a simple string path parameter is formatted with the expected metadata.</summary>
@@ -517,29 +518,29 @@ public async Task GenericOverloadedInterfaceWithTwoGenericParameterShouldBeExpec
517518
{
518519
{
519520
Route.Get("https://foo/10/Empty"),
520-
Reply.Json(nameof(IGenericMethodAndInterface<>.GetClassGenericParameter))
521+
Reply.Json(nameof(IGenericMethodAndInterface<>.GetGenericParameter))
521522
},
522523
};
523524

524525
var methodInfo = typeof(IGenericMethodAndInterface<string>).GetMethod(
525-
nameof(IGenericMethodAndInterface<>.GetClassGenericParameter),
526+
nameof(IGenericMethodAndInterface<>.GetGenericParameter),
526527
1,
527528
[Type.MakeGenericMethodParameter(0), typeof(string)])!;
528-
methodInfo = methodInfo.MakeGenericMethod(typeof(long));
529+
methodInfo = methodInfo.MakeGenericMethod(typeof(string));
529530
var parameterInfo1 = methodInfo.GetParameters()[0];
530531
var parameterInfo2 = methodInfo.GetParameters()[1];
531532

532533
var formatter = new TestUrlFormatter(
533534
[parameterInfo1, parameterInfo2],
534-
[typeof(long), typeof(string)]);
535+
[typeof(string), typeof(string)]);
535536
var settings = new RefitSettings
536537
{
537538
HttpMessageHandlerFactory = () => handler,
538539
UrlParameterFormatter = formatter
539540
};
540541
var service = RestService.For<IGenericMethodAndInterface<string>>(BaseUrl, settings);
541542

542-
await service.GetClassGenericParameter(LongValue, "Empty");
543+
await service.GetGenericParameter<string>("10", "Empty");
543544
await formatter.AssertNoOutstandingAssertions();
544545
}
545546
}

0 commit comments

Comments
 (0)