Search code examples
openapiswashbuckle.aspnetcore.net-6.0

OpenAPI Request and Response Examples in .NET 6


Has anyone had success yet with getting complex Swagger Request and Response examples to render through the Swagger UI in .NET 6?

I'm on .NET 6 RC 1 trying to use Swashbuckle.AspNetCore 6.2.1 and Swashbuckle.AspNetCore.Filters 7.0.2. The instructions say "Don’t forget to enable the ExamplesOperationFilter", but I can't even find the definition of ExamplesOperationFilter. Where is it?

I have seen the XML <example> tag stuff, but that's too simple for my needs - I have nested arrays and lists and enums and all sorts of stuff I want to display.


Solution

  • Ok I got it going. Didn't need ExamplesOperationFilter.

    In Program.cs I needed:

    builder.Services.AddSwaggerExamplesFromAssemblyOf<MySampleRequest>();
    

    And in the controller I needed [SwaggerRequestExample(...)] for the request. For the response, [SwaggerResponse(Type=...)] seemed to be enough.