RicoSuter / NSwag

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

Route/path parameters generated incorrectly in v14 #4683

Closed bzwieratinnovadis closed 10 months ago

bzwieratinnovadis commented 10 months ago

@RicoSuter I'm having an issue with route parameters with the stable release (v14.0.0) in a .NET 8 project. It seems to be related to issue #4587. I'm using NSwag.MSBuild to generate a CSharp client.

I have the following controller action:

[HttpGet("confirm/{batch}")]
public Task<MessageBoxConfirmBatchModel> ConfirmAsync(Guid batch) => _batchService.ConfirmAsync(batch);

OpenAPI definition:

"/api/message-box-batch/confirm/{batch}": {
  "get": {
    "tags": [
      "MessageBoxBatch"
    ],
    "parameters": [
      {
        "name": "OrganizationReference",
        "in": "header",
        "required": true,
        "schema": {
          "type": "string",
          "format": "uuid"
        }
      },
      {
        "name": "batch",
        "in": "path",
        "required": true,
        "schema": {
          "type": "string",
          "format": "uuid"
        }
      }
    ],
    "responses": {
      "200": {
        "description": "Success",
        "content": {
          "text/plain": {
            "schema": {
              "$ref": "#/components/schemas/MessageBoxConfirmBatchModel"
            }
          },
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/MessageBoxConfirmBatchModel"
            }
          },
          "text/json": {
            "schema": {
              "$ref": "#/components/schemas/MessageBoxConfirmBatchModel"
            }
          }
        }
      },
      "204": {
        "description": "No Content"
      },
      "400": {
        "description": "Bad Request",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/ValidationProblemDetails"
            }
          }
        }
      },
      "500": {
        "description": "Internal Server Error",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/ProblemDetails"
            }
          }
        }
      }
    }
  }
}

The resulting client:

var urlBuilder_ = new System.Text.StringBuilder();
urlBuilder_.Append("api/message-box-batch/confirm/{batch}");
urlBuilder_.Append(System.Uri.EscapeDataString(ConvertToString(batch, System.Globalization.CultureInfo.InvariantCulture)));

The request URI ends up being: api/message-box-batch/confirm/{batch}462995b7-433b-4382-b4d7-5f84599cf732

When the client is called the API returns a 400 Bad Request response due to the malformed URI.

In v13.20.0 the client was generated correctly:

var urlBuilder_ = new System.Text.StringBuilder();
urlBuilder_.Append("api/message-box-batch/confirm/{batch}");
urlBuilder_.Replace("{batch}", System.Uri.EscapeDataString(ConvertToString(batch, System.Globalization.CultureInfo.InvariantCulture)));

Is there some workaround in terms of configuration? Or should I wait for the next minor/patch version?

krzyhan commented 10 months ago

Not sure if related, but I checked with version built in #4680 and it works correctly (i used NSwag.Studio instead of MSBuild)

 "/api/Values/confirm/{batch}": {
      "get": {
        "tags": [
          "Values"
        ],
        "operationId": "Values_Confirm",
        "parameters": [
          {
            "name": "batch",
            "in": "path",
            "required": true,
            "schema": {
              "type": "integer",
              "format": "int32"
            },
            "x-position": 1
          }
        ],
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "type": "string"
                }
              }
            }
          }
        }
      }
    },
var urlBuilder_ = new System.Text.StringBuilder();
if (!string.IsNullOrEmpty(BaseUrl)) urlBuilder_.Append(BaseUrl);
// Operation Path: "api/Values/confirm/{batch}"
urlBuilder_.Append("api/Values/confirm/");
urlBuilder_.Append(System.Uri.EscapeDataString(ConvertToString(batch, System.Globalization.CultureInfo.InvariantCulture)));
bzwieratinnovadis commented 10 months ago

So I've been messing around with NSwagStudio to reproduce the issue and figured out why the code was generated incorrectly. Apparently there was a template being used that causes the issue, because without it everything works fine and path/route parameters are handled correctly. Hopefully this will help someone.

Closing the issue. Thanks everyone!