Skip to content

Commit

Permalink
Fix parenthesis bug in IsApiResponse expression, which caused IApiRes…
Browse files Browse the repository at this point in the history
…ponse-returning methods to throw ApiException on error, rather than assigning the exception to IApiResponse.Error
  • Loading branch information
DouglasKSmith committed Jan 15, 2022
1 parent ed78b92 commit 397ada2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
23 changes: 23 additions & 0 deletions Refit.Tests/ResponseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public interface IMyAliasService

[Get("/GetApiResponseTestObject")]
Task<ApiResponse<TestAliasObject>> GetApiResponseTestObject();

[Get("/GetIApiResponse")]
Task<IApiResponse> GetIApiResponse();
}

[Fact]
Expand Down Expand Up @@ -250,6 +253,26 @@ public async Task BadRequestWithEmptyContent_ShouldReturnApiResponse()
Assert.Equal("Hello world", apiResponse.Error.Content);
}

[Fact]
public async Task BadRequestWithStringContent_ShouldReturnIApiResponse()
{
var expectedResponse = new HttpResponseMessage(HttpStatusCode.BadRequest)
{
Content = new StringContent("Hello world")
};
expectedResponse.Content.Headers.Clear();

mockHandler.Expect(HttpMethod.Get, $"http://api/{nameof(fixture.GetIApiResponse)}")
.Respond(req => expectedResponse);

var apiResponse = await fixture.GetIApiResponse();

Assert.NotNull(apiResponse);
Assert.NotNull(apiResponse.Error);
Assert.NotNull(apiResponse.Error.Content);
Assert.Equal("Hello world", apiResponse.Error.Content);
}

[Fact]
public async Task ValidationApiException_HydratesBaseContent()
{
Expand Down
6 changes: 3 additions & 3 deletions Refit/RestMethodInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ public RestMethodInfo(Type targetInterface, MethodInfo methodInfo, RefitSettings
CancellationToken = ctParams.FirstOrDefault();

IsApiResponse = ReturnResultType!.GetTypeInfo().IsGenericType &&
(ReturnResultType!.GetGenericTypeDefinition() == typeof(ApiResponse<>)
|| ReturnResultType.GetGenericTypeDefinition() == typeof(IApiResponse<>)
|| ReturnResultType == typeof(IApiResponse));
(ReturnResultType!.GetGenericTypeDefinition() == typeof(ApiResponse<>)
|| ReturnResultType.GetGenericTypeDefinition() == typeof(IApiResponse<>))
|| ReturnResultType == typeof(IApiResponse);
}

private ISet<int> BuildHeaderCollectionParameterMap(List<ParameterInfo> parameterList)
Expand Down

0 comments on commit 397ada2

Please sign in to comment.