Skip to content

Commit 40fee49

Browse files
committed
#241 QueryString DateTime / DateTimeOffset
1 parent e06dcb4 commit 40fee49

3 files changed

Lines changed: 36 additions & 6 deletions

File tree

src/FluentRest/PostBuilder.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,15 @@ public TBuilder FormValue(string name, string? value)
5555
/// <exception cref="System.ArgumentNullException"><paramref name="name" /> is <see langword="null" />.</exception>
5656
public TBuilder FormValue<TValue>(string name, TValue? value)
5757
{
58-
var v = value?.ToString();
58+
var v = value switch
59+
{
60+
DateTime dateTime => dateTime.ToString("o"),
61+
DateTimeOffset dateTimeOffset => dateTimeOffset.ToString("o"),
62+
#if NET6_0_OR_GREATER
63+
DateOnly dateOnly => dateOnly.ToString("o"),
64+
#endif
65+
_ => value?.ToString()
66+
};
5967
return FormValue(name, v);
6068
}
6169

src/FluentRest/QueryBuilder.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,16 @@ public TBuilder QueryString<TValue>(string name, TValue? value)
370370
if (name is null)
371371
throw new ArgumentNullException(nameof(name));
372372

373-
var v = value != null ? value.ToString() : string.Empty;
373+
var v = value switch
374+
{
375+
DateTime dateTime => dateTime.ToString("o"),
376+
DateTimeOffset dateTimeOffset => dateTimeOffset.ToString("o"),
377+
#if NET6_0_OR_GREATER
378+
DateOnly dateOnly => dateOnly.ToString("o"),
379+
#endif
380+
_ => value?.ToString()
381+
};
382+
374383
return QueryString(name, v);
375384
}
376385

@@ -393,10 +402,7 @@ public TBuilder QueryStrings<TValue>(string name, IEnumerable<TValue>? values)
393402
return (TBuilder)this;
394403

395404
foreach (var value in values)
396-
{
397-
var v = value != null ? value.ToString() : string.Empty;
398-
QueryString(name, v);
399-
}
405+
QueryString(name, value);
400406

401407
return (TBuilder)this;
402408
}

test/FluentRest.Tests/QueryBuilderTest.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Linq;
34
using System.Net.Http;
@@ -23,6 +24,21 @@ public void QueryStringNull()
2324
Assert.Equal("http://test.com/?Test=", uri.ToString());
2425
}
2526

27+
[Fact]
28+
public void QueryStringDateTime()
29+
{
30+
var request = new HttpRequestMessage();
31+
var builder = new QueryBuilder(request);
32+
33+
DateTime value = new(2025, 10, 15, 11, 11, 30, DateTimeKind.Utc);
34+
builder.BaseUri("http://test.com/");
35+
builder.QueryString("Test", value);
36+
37+
var uri = request.GetUrlBuilder();
38+
39+
Assert.Equal("http://test.com/?Test=2025-10-15T11%3A11%3A30.0000000Z", uri.ToString());
40+
}
41+
2642
[Fact]
2743
public void QueryStringMultipleValue()
2844
{

0 commit comments

Comments
 (0)