We are using NSwag to generate swagger pages for our API. We have a fictitious asp.net core 2.2 controller and the generated swagger pages looks like this:

Is it possible to change the contents of the Example Value (highlighted in yellow)? We would like to display an actual sample value for the testParameter instead of just "string". Is there a data annotation we can use, or some other overriding mechanism available to override this behaviour?
The version of NSwag we are using is NSwag.AspNetCore 13.0.2.
The class that defines the model is:
public class TestQuery : IRequest<TestResponse>
{
[DefaultValue("test1")]
[JsonProperty("testParameter")]
public string TestParameter { get; set; }
}
We are using MediatR hence the IRequest.
Here's the controller class:
[Authorize, Route("api/route/test")]
[ApiController]
public class TestController : ControllerBase
{
private readonly IMediator _mediator;
/// <summary>
/// TestController constructor
/// </summary>
/// <param name="mediator">Dependency Injected mediator reference</param>
public TestController(IMediator mediator)
{
_mediator = mediator;
}
/// <summary>
/// Async method which retrieves a test response for a given testParameter string
/// </summary>
/// <remarks>
///
/// Sample testParameters: "test1", "test2", "test3"
///
/// Sample Request:
/// {
/// "testParameter": "test1"
/// }
///
/// </remarks>
/// <param name="GetTestResponseQuery">Get test response request model</param>
/// <returns>A test response</returns>
[HttpPost]
public async Task<ApiResponse<TestResponse>> GetTestResponse([FromBody]TestQuery testQuery)
{
if (!ModelState.IsValid)
{
throw new Exception($"State for model TestQuery is not valid for service {testQuery.TestParameter}");
}
else
{
var result = await _mediator.Send(testQuery).ConfigureAwait(false);
return new ApiResponse<TestResponse>(result);
}
}
}
Thank you kindly, Yves Rochon
On your TestResponse class, add the example tag in xml comments of every property, that will be used in the swagger example. eg
public class TestResponse
{
/// <summary>
/// Property description
/// </summary>
/// <example>Sample value</example>
public string Property { get; set; }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With