能否将 Swashbuckle 配置为包括不是参数或操作结果的 DTO?
Can Swashbuckle be configured to include DTOs that are not a parameter or action result?
我有一些额外的 DTO 类 需要能够从 TypeScript 使用,但在控制器操作中没有用。他们将 sent/received 使用不同的机制 (SignalR)。我仍然想使用 Swagger 来描述类型,因为我可以使用 Swagger CodeGen 来生成 TypeScript 定义。
我能否让 Swashbuckle 在 Swagger 文档中包含任意 DTO,这些 DTO 不是参数或操作结果?
可以,使用 IDocumentFilter
private class ApplyDocumentVendorExtensions : IDocumentFilter
{
public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
{
schemaRegistry.GetOrRegister(typeof(ExtraType));
schemaRegistry.GetOrRegister(typeof(BigClass));
}
}
需要在 SwaggerConfig 部分 EnableSwagger 中添加 class,如下所示:c.DocumentFilter<ApplyDocumentVendorExtensions>();
我这里有几个例子:
https://github.com/heldersepu/SwashbuckleTest/blob/master/Swagger_Test/App_Start/SwaggerConfig.cs
我有一些额外的 DTO 类 需要能够从 TypeScript 使用,但在控制器操作中没有用。他们将 sent/received 使用不同的机制 (SignalR)。我仍然想使用 Swagger 来描述类型,因为我可以使用 Swagger CodeGen 来生成 TypeScript 定义。
我能否让 Swashbuckle 在 Swagger 文档中包含任意 DTO,这些 DTO 不是参数或操作结果?
可以,使用 IDocumentFilter
private class ApplyDocumentVendorExtensions : IDocumentFilter
{
public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
{
schemaRegistry.GetOrRegister(typeof(ExtraType));
schemaRegistry.GetOrRegister(typeof(BigClass));
}
}
需要在 SwaggerConfig 部分 EnableSwagger 中添加 class,如下所示:c.DocumentFilter<ApplyDocumentVendorExtensions>();
我这里有几个例子:
https://github.com/heldersepu/SwashbuckleTest/blob/master/Swagger_Test/App_Start/SwaggerConfig.cs