'NSwag autogenerated client does not show default values
I have an api with Swagger enabled. In one of the endpoints, I have a request with some values set as default.
[DefaultValue("test value")]
public string? Description { get; set; }
This looks fine in the Swagger page for the api. The default value is used in the Swagger example.
We use NSwag to generate an api client which is consumed in the gateway. When I generate such a client, the request object looks like this:
[System.Text.Json.Serialization.JsonPropertyName("description")]
public string? Description { get; set; } = "test value";
Assigning the value have no effect on the Swagger definition in the gateway, and the value is therefore displayed as description : "string"
How do I properly transfer the DefaultValue annotation to the autogenerated client, in such a way that I can reuse the request object and display the example with the test value string set?
The swagger.json for the api looks like this for the description field:
"properties": {
"description": {
"type": "string",
"default": "test value",
"nullable": true
},
Solution 1:[1]
I didn’t find a solution using NSwag by itself.
I therefore used the openapi package from Microsoft, in combination with Roslyn, to create a small application which reads the swagger Json definition, parses the source code of the generated client, and sets the default value attributes.
This works, and provides quite a bit of control, but I would still have preferred if it were a feature in NSwag.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | MortenGR |
