Szymson
Szymson

Reputation: 1122

Multiple 400 response example on Swagger with Swashbuckle.AspNetCore

I use Swashbuckle.AspNetCore library to generate Swagger documentation. Is it possible to add multiple error messages to one status code? For example I have field which has validations so I need this in my documentation:

Code   Description
 400   Invalid format
       Field is required
       Should be greater than 10

In previous version of library we had [SwaggerResponse] where I could put validation errors into description, but the attribute is not available in newest version. We have [ProducesResponseType] attribute which doesn't have description parameter.

Upvotes: 0

Views: 2183

Answers (1)

CodeHat
CodeHat

Reputation: 483

You can use Swashbuckle.AspNetCore.Filters to specify multiple examples.

  1. See this section for how to enable filters: Installation
    I specifically only added c.ExampleFilters() and services.AddSwaggerExamplesFromAssemblies(Assembly.GetEntryAssembly()) to my Startup.cs.
  2. See this section for how to create multiple examples: Multiple Example Setup
  3. See this section for usage details: Using Multiple Examples
    The document mentions the use of SwaggerResponse and SwaggerResponseExample annotation but I was able to achieve desired result with only using SwaggerResponseExample.

Upvotes: 2

Related Questions