-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Generated client code does not replace/set route parameters with 14.0.0-preview010 #4587
Comments
OOPS! I'll work on that tomorrow. @RicoSuter, what's the best way to add a unit test for this? |
NJsonSchema uses Verify whici is IMO a nice way to test as snapshots are produced and VCS controlled and you can see differences in pull request when output changes. I think this hasn't been taken into use (yet) on NSwag's side though. |
I've added corrections to my draft PR: #4585. Please, checkout if that solves the issue. |
It did solve the issue. I'm inspecting a quite large 8 MB generated client from a 2.5 MB swagger file and haven't found any other issue yet. |
Can you confirm that the latest preview works fine for you? |
Can confirm with latest RC3 the issue no longer reproduces |
@RicoSuter @vas6ili I'm having a similar issue with route parameters with the stable release (v14.0.0) in a .NET 8 project. 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: 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? |
Thanks for the concrete example, @bzwieratinnovadis. Which tooling and version are you using? I just tried that example on NSwagStudio v14.0.0.0 and got this generated code: var urlBuilder_ = new System.Text.StringBuilder();
if (!string.IsNullOrEmpty(BaseUrl)) urlBuilder_.Append(BaseUrl);
// Operation Path: "api/message-box-batch/confirm/{batch}"
urlBuilder_.Append("api/message-box-batch/confirm/");
urlBuilder_.Append(System.Uri.EscapeDataString(ConvertToString(batch, System.Globalization.CultureInfo.InvariantCulture))); |
So I've been messing around with NSwagStudio to reproduce the issue and figured out why the code was being generated incorrecly. Apparently there was a template being used that causes the issue because without it everything is fine and path/route parameters are handled correctly. I closed related #4683 |
Hi @bzwieratinnovadis, I am still getting this error in NSwag 14.1. Can you elaborate what you mean with "a template being used"? I do nothing special, just generate a C# client from a valid openapi spec. But the generated code is obviously broken, the generated url contains names of the placeholders instead of just the values.
URL: Is there any fix or workaround available? I am surprised this issue is still open after almost one year. |
@lfderm, can you provide a sample swagger/openapi spec that has this issue, And the full set of options used? You can use NSwag Studio for that and post the nswag file. |
Hi @paulomorgado, i reproduced that when i was manually generating a schema. Adding an
Thank you for all your amazing work. |
I can confirm that making the route parameter mandatory fixes the issue. Still, this seems like a bug to me. The generated code for an optional parameter (here "offset") does not make sense:
There is already the if/else clause for the parameter. The last method call is absolutely wrong and should be removed from code generation. Then, I think optional route parameters would work fine. |
@dkone, a proper OpenAPI document would go a long way to expedite the fix. Better yet, adding unit tests exposing this. Although this can be "fixed", "path parameters are always required" (in https://swagger.io/docs/specification/v3_0/describing-parameters/#path-parameters). If you want them optional, you should specify them as query parameters. |
For a path template like
/odata/QueueRetention({key})
generated client code does not replace{key}
url route parameter. Usednet80/dotnet-nswag.exe
from https://github.com/RicoSuter/NSwag/releases/tag/v14.0.0-preview010 withnet8
(which is the reason I'm upgrading to 14)Generated client code snippet
API definition snippet
test-swag.json
NswagClient.zip
Probably related to #4579 @paulomorgado
The text was updated successfully, but these errors were encountered: