RicoSuter / NSwag

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

C# Generator incorrectly appends optional GET parameter name to request URL #4940

Open AnotherMuggle opened 4 months ago

AnotherMuggle commented 4 months ago

You can see the bug marked "Bug here!" in the example generated C# code.

Example YAML

paths:
  /path1/path2/{requiredId}/{optionalId}:
    get:
      parameters:
        - in: path
          name: "requiredId"
          description: this is a required parameter
          required: true
          schema:
            type: string
        - in: path
          name: "optionalId"
          description: this is an optional parameter
          required: false
          schema:
            type: string

Example generated C#

// Operation Path: "path1/path2/{requiredId}/{optionalId}"
urlBuilder_.Append("path1/path2/");
urlBuilder_.Append(System.Uri.EscapeDataString(ConvertToString(requiredId, System.Globalization.CultureInfo.InvariantCulture)));
urlBuilder_.Append('/');
if (optionalId != null)
{
    urlBuilder_.Append(System.Uri.EscapeDataString(ConvertToString(optionalId, System.Globalization.CultureInfo.InvariantCulture)));
}
else
    if (urlBuilder_.Length > 0) urlBuilder_.Length--;
urlBuilder_.Append("{optionalId}"); // Bug here!

PrepareRequest(client_, request_, urlBuilder_);

var url_ = urlBuilder_.ToString();
request_.RequestUri = new System.Uri(url_, System.UriKind.RelativeOrAbsolute);
cpopescu89 commented 3 months ago

I have the same issue when generating the C# client from a json file. The parameters that are not marked as required are appended to the request incorrectly, instead of being replaced by the actual values. It doesn't matter the request type (GET/POST/PUT) I am using NSwag.ApiDescription.Client v.14.1.0

jplet397 commented 1 month ago

I have the same issue. I have an asp.net 8 solution with minimal API defined like this:

            .MapGet("/count/{test}", Test)
            .WithName("Test")
            .WithOpenApi();

However when used in the generated class, I get this result: https://localhost:61971/api/v1/count/08072946773{test}