Can Swashbuckle be configured to include DTOs that are not a parameter or action result?

1.4k Views Asked by At

I have some additional DTO classes that I need to be able to use from TypeScript, but have no use in a controller action. They'll be sent/received using a different mechanism (SignalR). I still want to use Swagger to describe the types, because I can then use Swagger CodeGen to produce TypeScript definitions.

Can I get Swashbuckle to include arbitrary DTOs, which are not a parameter or result of an action, in the Swagger docs?

1

There are 1 best solutions below

0
On BEST ANSWER

Yes you can, use an IDocumentFilter

    private class ApplyDocumentVendorExtensions : IDocumentFilter
    {
        public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
        {
            schemaRegistry.GetOrRegister(typeof(ExtraType));
            schemaRegistry.GetOrRegister(typeof(BigClass));                
        }
    }

That class needs to be added in the SwaggerConfig section EnableSwagger like this: c.DocumentFilter<ApplyDocumentVendorExtensions>();


I have a few examples here: https://github.com/heldersepu/SwashbuckleTest/blob/master/Swagger_Test/App_Start/SwaggerConfig.cs