RestCode / WebApiProxy

MIT License
199 stars 91 forks source link

Error with Nullable DateTimes #112

Open ngm opened 8 years ago

ngm commented 8 years ago

If I create a method on an ApiController with a nullable DateTime as a parameter, e.g.:

[Route("nullabledate")]
public void NullableDateTest(DateTime? nullableDate)
{
    Console.Write(nullableDate);
}

When generating the C# client with WebApiProxy-Generate-CSharp, I get the following code in WebApiProxy.generated.cs:

/// <summary>
/// 
/// </summary>
/// <param name="nullableDate"></param>
/// <returns></returns>
protected virtual async Task<HttpResponseMessage> NullableDateTestAsyncMsg(Nullable<DateTime> nullableDate)
{
    return await HttpClient.PostAsJsonAsync("profiles/nullabledate?nullableDate=" + nullableDate.ToString("o"), default(HttpResponseMessage));
}

/// <summary>
/// 
/// </summary>
/// <param name="nullableDate"></param>
/// <returns></returns>
public virtual async Task<HttpResponseMessage> NullableDateTestAsync(Nullable<DateTime> nullableDate)
{
    return await HttpClient.PostAsJsonAsync("profiles/nullabledate?nullableDate=" + nullableDate.ToString("o"), default(HttpResponseMessage));
}

The calls to nullableDate.ToString("o") produce the following error:

No overload for method 'ToString' takes 1 arguments

Is there a way to get around this?

ngm commented 8 years ago

I haven't tested it very thoroughly, but the following change works for me for now:

https://github.com/ngm/WebApiProxy/commit/babd3d2b163880505a1ea77d9704cf77d02ae72a

Basically - if it's a Nullable<DateTime>, then ToString("o") its .Value, otherwise return the empty string.