Skip to content
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

Route/path parameters generated incorrectly in v14 #4683

Closed
bzwieratinnovadis opened this issue Jan 4, 2024 · 2 comments
Closed

Route/path parameters generated incorrectly in v14 #4683

bzwieratinnovadis opened this issue Jan 4, 2024 · 2 comments

Comments

@bzwieratinnovadis
Copy link

bzwieratinnovadis commented Jan 4, 2024

@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
Copy link
Contributor

krzyhan commented Jan 4, 2024

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
Copy link
Author

bzwieratinnovadis commented Jan 5, 2024

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants