New issue

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

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

Already on GitHub? Sign in to your account

System.Net.HttpClient does not have a 'PatchAsync' built-in method. #107

Closed
shaunol opened this Issue May 16, 2016 · 2 comments

Comments

Projects
None yet
2 participants
@shaunol

shaunol commented May 16, 2016

The CSharp code generator ClientTemplate t4 template uses the SwaggerOperationMethod enum converted to a string to determine which http method and System.Net.HttpClient method to use when calling operations.

This means the C# HttpClient is used like so:

client_.GetAsync
client_.PostAsync
client_.PutAsync

Unfortunately the System.Net.HttpClient class has no built-in PatchAsync method, therefore we need to provide a polyfill (via extension methods) or change the convention for emitting the method calls.

See: https://github.com/NSwag/NSwag/blob/master/src/NSwag.CodeGeneration/CodeGenerators/CSharp/Templates/ClientTemplate.tt#L106

I don't have a solution at this time but am posting a work-around as seen here: http://stackoverflow.com/a/26218765/134869

public static async Task<HttpResponseMessage> PatchAsync(this HttpClient client, string requestUri, HttpContent content, CancellationToken cancellationToken)
{
    var method = new HttpMethod("PATCH");
    var request = new HttpRequestMessage(method, requestUri)
    {
        Content = content
    };

    var response = await client.SendAsync(request, cancellationToken);
    return response;
}

Work-around is not exhaustive of all possibilities (ie. no content in request/response). I have also not checked for existence of the other operations on the HttpClient class either.

Right now this is causing invalid C# code to be generated when generating code from schemas' containing PATCH requests.

Cheers for the great library.

@RSuter RSuter added the type: bug label May 16, 2016

@RSuter

This comment has been minimized.

Show comment
Hide comment
@RSuter

RSuter May 19, 2016

Owner

Thanks, I think generating the extension methods in a private class would be the simplest solution... (with check if method is required)

Owner

RSuter commented May 19, 2016

Thanks, I think generating the extension methods in a private class would be the simplest solution... (with check if method is required)

@RSuter

This comment has been minimized.

Show comment
Hide comment
@RSuter

RSuter May 22, 2016

Owner

Fixed in 389caf4

Owner

RSuter commented May 22, 2016

Fixed in 389caf4

@RSuter RSuter closed this May 22, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment