Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Refit.Tests/RequestBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ public interface IRestMethodInfoTests

[Get("/api/{id}")]
Task IEnumerableThrowingError([Query(CollectionFormat.Multi)] IEnumerable<string> values);

[Get("/foo")]
List<string> InvalidGenericReturnType();
}

public enum TestEnum { A, B, C }
Expand Down Expand Up @@ -1146,6 +1149,14 @@ public void ParameterMappingWithHeaderQueryParamAndQueryArrayParam()
Assert.Single(fixture.HeaderParameterMap);
Assert.Single(fixture.PropertyParameterMap);
}

[Fact]
public void GenericReturnTypeIsNotTaskOrObservableShouldThrow()
{
var input = typeof(IRestMethodInfoTests);
Assert.Throws<ArgumentException>(() => new RestMethodInfo(input,
input.GetMethods().First(x => x.Name == nameof(IRestMethodInfoTests.InvalidGenericReturnType))));
}
}

[Headers("User-Agent: RefitTestClient", "Api-Version: 1")]
Expand Down
7 changes: 4 additions & 3 deletions Refit/RestMethodInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,9 @@ static Dictionary<int, string> BuildHeaderParameterMap(List<ParameterInfo> param
void DetermineReturnTypeInfo(MethodInfo methodInfo)
{
var returnType = methodInfo.ReturnType;
if (returnType.IsGenericType && (methodInfo.ReturnType.GetGenericTypeDefinition() != typeof(Task<>)
|| methodInfo.ReturnType.GetGenericTypeDefinition() != typeof(IObservable<>)))
if (returnType.IsGenericType && (methodInfo.ReturnType.GetGenericTypeDefinition() == typeof(Task<>)
|| methodInfo.ReturnType.GetGenericTypeDefinition() == typeof(ValueTask<>)
|| methodInfo.ReturnType.GetGenericTypeDefinition() == typeof(IObservable<>)))
{
ReturnType = returnType;
ReturnResultType = returnType.GetGenericArguments()[0];
Expand All @@ -488,7 +489,7 @@ void DetermineReturnTypeInfo(MethodInfo methodInfo)
DeserializedResultType = typeof(void);
}
else
throw new ArgumentException($"Method \"{methodInfo.Name}\" is invalid. All REST Methods must return either Task<T> or IObservable<T>");
throw new ArgumentException($"Method \"{methodInfo.Name}\" is invalid. All REST Methods must return either Task<T> or ValueTask<T> or IObservable<T>");
}

void DetermineIfResponseMustBeDisposed()
Expand Down