To add examples of objects returned from your ASP.NET Core Web API to Swagger, you can use the Swashbuckle.AspNetCore.Examples
NuGet package. This package provides a set of classes that you can use to define examples for your API methods and models.
Here are the steps to use Swashbuckle.AspNetCore.Examples
to add examples to your API documentation:
Swashbuckle.AspNetCore.Examples
NuGet package.public class MyModelExample { public int Id { get; set; } = 1; public string Name { get; set; } = "John Doe"; }
IExamplesProvider
interface. This provider should return the example object you created in the previous step. For example:public class MyModelExampleProvider : IExamplesProvider { public object GetExamples() { return new MyModelExample(); } }
SwaggerResponseExample
attribute to your API method, passing in the HTTP status code and the type of your model. For example:[HttpGet] [SwaggerResponse((int)HttpStatusCode.OK, Type = typeof(MyModel))] [SwaggerResponseExample((int)HttpStatusCode.OK, typeof(MyModelExampleProvider))] public IActionResult Get() { // ... }
With these steps, you should be able to add examples to your Web API methods and models in Swagger using the Swashbuckle.AspNetCore.Examples
package.
"Swagger Example Responses with XML Comments"
/// <summary> /// Gets the list of products. /// </summary> /// <remarks> /// Sample response: /// /// [ /// { /// "id": 1, /// "name": "Product 1" /// }, /// { /// "id": 2, /// "name": "Product 2" /// } /// ] /// </remarks> [HttpGet] [ProducesResponseType(typeof(List<Product>), 200)] public IActionResult GetProducts() { // Implementation }
[ProducesResponseType]
attribute to provide example responses for Swagger."Swagger Example Responses with SwaggerResponseAttribute"
[HttpGet] [SwaggerResponse(200, "List of products", typeof(List<Product>))] public IActionResult GetProducts() { // Implementation }
[SwaggerResponse]
attribute to specify example responses directly in the action method."Swagger Example Responses with SwaggerObjectResult"
[HttpGet] public IActionResult GetProducts() { var products = new List<Product> { new Product { Id = 1, Name = "Product 1" }, new Product { Id = 2, Name = "Product 2" } }; return new SwaggerObjectResult(products); }
SwaggerObjectResult
with the desired object to provide an example in Swagger."Swagger Example Responses with ProducesResponseType and ResponseWrapper"
[HttpGet] [ProducesResponseType(typeof(ResponseWrapper<List<Product>>), 200)] public IActionResult GetProducts() { var products = new List<Product> { new Product { Id = 1, Name = "Product 1" }, new Product { Id = 2, Name = "Product 2" } }; return Ok(new ResponseWrapper<List<Product>>(products)); }
ResponseWrapper
class to encapsulate the response and provide examples with [ProducesResponseType]
."Swagger Example Responses with Swashbuckle.Examples"
[HttpGet] [SwaggerResponseExample(200, typeof(List<Product>), typeof(ProductsResponseExample))] public IActionResult GetProducts() { // Implementation }
"Swagger Example Responses with OperationFilter"
public class ExamplesOperationFilter : IOperationFilter { public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription) { // Add example responses to the operation } }
GlobalConfiguration.Configuration .EnableSwagger(c => { // Other Swagger configurations c.OperationFilter<ExamplesOperationFilter>(); }) .EnableSwaggerUi();
IOperationFilter
to add example responses and apply it during Swagger configuration."Swagger Example Responses with Swashbuckle.AspNetCore.Filters"
[HttpGet] [SwaggerResponseExample(200, typeof(List<Product>), typeof(ProductsResponseExample))] public IActionResult GetProducts() { // Implementation }
"Swagger Example Responses with XML Comments and IExamplesProvider"
/// <summary> /// Gets the list of products. /// </summary> /// <remarks> /// Sample response: /// /// [ /// { /// "id": 1, /// "name": "Product 1" /// }, /// { /// "id": 2, /// "name": "Product 2" /// } /// ] /// </remarks> [HttpGet] [ProducesResponseType(typeof(List<Product>), 200)] public IActionResult GetProducts() { // Implementation } public class ProductsResponseExample : IExamplesProvider<List<Product>> { public List<Product> GetExamples() { return new List<Product> { new Product { Id = 1, Name = "Product 1" }, new Product { Id = 2, Name = "Product 2" } }; } }
IExamplesProvider
to provide example responses for Swagger."Swagger Example Responses with Swashbuckle.AspNetCore.SwaggerGen"
services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" }); c.OperationFilter<ExamplesOperationFilter>(); });
public class ExamplesOperationFilter : IOperationFilter { public void Apply(OpenApiOperation operation, OperationFilterContext context) { // Add example responses to the operation } }
"Swagger Example Responses with Swashbuckle.AspNetCore.Annotations"
[HttpGet] [SwaggerResponse(200, "List of products", typeof(List<Product>))] public IActionResult GetProducts() { // Implementation }
angular-material-6 jwe web-mediarecorder snackbar symlink-traversal android-mapview builder angular-ngmodel android-collapsingtoolbarlayout mms