RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.67k stars 1.23k forks source link

NSwagGeneratePrepareRequestAndProcessResponseAsAsyncMethods does not generate async partial methods #4724

Closed manolitooo closed 7 months ago

manolitooo commented 7 months ago

Hi,

By having a look into this closed issue, I'm wondering if I'm doing something incorrectly. But after diving in the NSwag code, it does not seem so. I have found out the following within Client.Class.liquid:

{% if GeneratePrepareRequestAndProcessResponseAsAsyncMethods == false -%}
    partial void PrepareRequest({{ HttpClientType }} client, System.Net.Http.HttpRequestMessage request, string url);
    partial void PrepareRequest({{ HttpClientType }} client, System.Net.Http.HttpRequestMessage request, System.Text.StringBuilder urlBuilder);
    partial void ProcessResponse({{ HttpClientType }} client, System.Net.Http.HttpResponseMessage response);
{% endif -%}

Isn't it missing an "else" block to define the partial methods for the async case? Currently, I am getting the following error:

image

And I can confirm the generated code is missing that block of code where it is expected to be written.

Thanks in advance.

manolitooo commented 7 months ago

Working as expected. Sorry :).

maurictg commented 5 months ago

@manolitooo Do you know why the partial methods aren't created?

manolitooo commented 5 months ago

@manolitooo Do you know why the partial methods aren't created?

It's invalid c#.

CharlesCreteST commented 3 months ago

How is this supposed to be used if the generated code has errors?

smourier commented 3 weeks ago

FWIW, I had the same issue and I resolved it by defining a (common to clients) base class

in settings

  "codeGenerators": {
    "openApiToCSharpClient": {
      "clientBaseClass": "BaseClient", 

And then for example (you can use Task or ValueTask, your mileage may vary depending on your other settings):

public abstract class BaseClient
{
    protected virtual ValueTask PrepareRequestAsync(HttpClient client, HttpRequestMessage request, string url, CancellationToken cancellationToken)
    {
        return ValueTask.CompletedTask;
    }

    protected virtual ValueTask PrepareRequestAsync(HttpClient client, HttpRequestMessage request, StringBuilder url, CancellationToken cancellationToken)
    {
        return ValueTask.CompletedTask;
    }

    protected virtual ValueTask ProcessResponseAsync(HttpClient client, HttpResponseMessage response, CancellationToken cancellationToken)
    {
        return ValueTask.CompletedTask;
    }
}

Now you can override this in the class you need to do it.