Autorest C# client generation not respecting input/output types

581 Views Asked by At

I'm trying to generate a C# API client for my .NET 6 Web API project using Autorest, but my generated client doesn't seem to care about my request and response data types.

My controller:

[ApiController]
[Route("[controller]")]
public class CalculateController : ControllerBase
{
    [HttpPost(Name = "Add")]
    [ProducesResponseType(typeof(AddResponseData), (int)HttpStatusCode.OK)]
    public AddResponseData Add([FromBody] AddRequestData data)
    {
        return new()
        {
            Sum = data.Value1 + data.Value2
        };
    }
}

Models:

public class AddRequestData
{
    public int Value1 { get; set; }

    public int Value2 { get; set; }
}

public class AddResponseData
{
    public int Sum { get; set; }
}

I generate the client using a PowerShell script:

$solutionName = "AutorestTest"
$apiProject = "AutorestTest.Api"
$clientProject = "AutorestTest.Client"

# Build solution
dotnet build $solutionName.sln

# Generate swagger.json
Set-Location $apiProject
dotnet swagger tofile --output "../swagger.json" "bin/Debug/net6.0/$apiProject.dll" v1
Set-Location ..

# Generate client files
autorest `
    --input-file="swagger.json" `
    --output-folder="$clientProject/Generated" `
    --clear-output-folder `
    --namespace="$clientProject" `
    --csharp

I expect to be able to use the generated client like this:

var client = new AutorestTestApiClient(new ("https://localhost:7164"), new());
// or just `AddResponseData result = ...`
Result<AddResponseData> result = await client.AddAsync(new AddRequestData()
{
    Value1 = 1,
    Value2 = 2
});
Debug.Assert(result.Value.Sum == 3);

But the generated AddAsync method looks like this:

public virtual async Task<Response> AddAsync(RequestContent content, ContentType contentType, RequestContext context = null)
{
    // ...
}

It uses these generic RequestContent and Response types, instead of my own data types.

How do I make autorest generate clients that use AddRequestData and AddResponseData models in the generated methods?

Here's my full sample project: GitHub Rene-Sackers/AutorestTest

0

There are 0 best solutions below