Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: support nullable value type collection in queries #1926

Merged
merged 1 commit into from
Nov 9, 2024
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
21 changes: 21 additions & 0 deletions Refit.Tests/RestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,9 @@ public interface IQueryApi

[Get("/foo?{key}={value}")]
Task ParameterMappedQuery(string key, string value);

[Get("/foo")]
Task NullableIntCollectionQuery([Query] int?[] values);
}

public interface IFragmentApi
Expand Down Expand Up @@ -2456,6 +2459,24 @@ public async Task ParameterMappedQueryShouldEscape()
mockHttp.VerifyNoOutstandingExpectation();
}

[Fact]
public async Task NullableIntCollectionQuery()
{
var mockHttp = new MockHttpMessageHandler();
var settings = new RefitSettings { HttpMessageHandlerFactory = () => mockHttp, };

mockHttp
.Expect(HttpMethod.Get, "https://github.com/foo")
.WithExactQueryString("values=3%2C4%2C")
.Respond(HttpStatusCode.OK);

var fixture = RestService.For<IQueryApi>("https://github.com", settings);

await fixture.NullableIntCollectionQuery([3, 4, null]);

mockHttp.VerifyNoOutstandingExpectation();
}

[Fact]
public async Task ShouldStripFragment()
{
Expand Down
13 changes: 8 additions & 5 deletions Refit/RequestBuilderImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1349,12 +1349,15 @@ static bool DoNotConvertToQueryMap(object? value)
type = intType.GetGenericArguments()[0];
return ShouldReturn(type);

// Check if type is a simple string or IFormattable type, check underlying type if Nullable<T>
static bool ShouldReturn(Type type) =>
type == typeof(string)
|| type == typeof(bool)
|| type == typeof(char)
|| typeof(IFormattable).IsAssignableFrom(type)
|| type == typeof(Uri);
Nullable.GetUnderlyingType(type) is { } underlyingType
? ShouldReturn(underlyingType)
: type == typeof(string)
|| type == typeof(bool)
|| type == typeof(char)
|| typeof(IFormattable).IsAssignableFrom(type)
|| type == typeof(Uri);
}

static void SetHeader(HttpRequestMessage request, string name, string? value)
Expand Down
Loading