英文:
How to define 'File' Schema Type in Swashbuckle.AspNetCore
问题
在将Swashbuckle.AspNetCore从v4.0.1更新到v.6.5.0后,以下代码不再能够编译:
public class FileSchemaType : IOperationFilter
{
public void Apply(Operation operation, OperationFilterContext context)
{
// TODO 用合适的解决方案替代这个临时方法(https://github.com/domaindrivendev/Swashbuckle/issues/1144)
if (operation.OperationId == "ExportToExcel" || operation.OperationId == "ExportToPdf" ||
operation.OperationId == "GetReport" || operation.OperationId == "DownloadFile")
{
operation.Responses["200"].Schema = new Schema { Type = "file" };
}
}
}
我不得不将上述代码重写为以下形式:
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
// TODO 用合适的解决方案替代这个临时方法(https://github.com/domaindrivendev/Swashbuckle/issues/1144)
if (operation.OperationId == "ExportToExcel" || operation.OperationId == "ExportToPdf" ||
operation.OperationId == "GetReport" || operation.OperationId == "DownloadFile")
{
// 在新版本中,operation.Responses["200"].Schema属性不再存在
}
}
根据发布说明,在v5.0.0-rc3版本中,Swashbuckle.AspNetCore进行了一系列重大更改,包括迁移到Swagger/OpenAPI v3。
英文:
After updating Swashbuckle.AspNetCore from v4.0.1 to v.6.5.0, the following code can't be compiled anymore:
public class FileSchemaType : IOperationFilter
{
public void Apply(Operation operation, OperationFilterContext context)
{
// TODO Replace this workaround (https://github.com/domaindrivendev/Swashbuckle/issues/1144) with a proper solution using e. g. attributes
if (operation.OperationId == "ExportToExcel" || operation.OperationId == "ExportToPdf" ||
operation.OperationId == "GetReport" || operation.OperationId == "DownloadFile")
{
operation.Responses["200"].Schema = new Schema {Type = "file"};
}
}
}
I had to rewrite the mentioned code to this:
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
// TODO Replace this workaround (https://github.com/domaindrivendev/Swashbuckle/issues/1144) with a proper solution using e. g. attributes
if (operation.OperationId == "ExportToExcel" || operation.OperationId == "ExportToPdf" ||
operation.OperationId == "GetReport" || operation.OperationId == "DownloadFile")
{
operation.Responses["200"].Schema = new Schema { Type = "file" };
}
}
However, the property Schema doesn't exist anymore on operation.Responses["200"]
According to the release notes in v5.0.0-rc3 a number of significant changes, including a transition to Swagger/OpenAPI v3, were made in Swashbuckle.AspNetCore.
答案1
得分: 0
使用 [Produces(typeof(HttpResponseMessage))] 并返回 File 实例的部分...
[Produces(typeof(HttpResponseMessage))]
public async Task<IActionResult> ExportToExcel(string listName, string language, [FromBody] List<int> personIds,
DateTime startDate, DateTime endDate, List<int> notSelectedTimeTypeIds)
{
var stream = await AbsencePlanningProvider.Instance.ExportToExcel(User.GetSub(), language, listName, personIds,
startDate, endDate, notSelectedTimeTypeIds);
stream.Position = 0;
return File(stream, "application/octet-stream");
}
...连同这个操作过滤器一起解决了问题:
public class FileSchemaType : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
// 这些操作必须具有属性 [Produces(typeof(HttpResponseMessage))]
// 以及此操作过滤器
if (operation.OperationId == "ExportToExcel" || operation.OperationId == "ExportToPdf" ||
operation.OperationId == "GetReport" || operation.OperationId == "DownloadFile")
{
foreach (var item in operation.Responses["200"].Content)
item.Value.Schema = new OpenApiSchema { Type = "file" };
}
}
}
英文:
Using [Produces(typeof(HttpResponseMessage))] and returning a File instance...
[Produces(typeof(HttpResponseMessage))]
public async Task<IActionResult> ExportToExcel(string listName, string language, [FromBody] List<int> personIds,
DateTime startDate, DateTime endDate, List<int> notSelectedTimeTypeIds
)
{
var stream = await AbsencePlanningProvider.Instance.ExportToExcel(User.GetSub(), language, listName, personIds,
startDate, endDate, notSelectedTimeTypeIds);
stream.Position = 0;
return File(stream, "application/octet-stream");
}
...together with this operation filter solved the problem:
public class FileSchemaType : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
// The listed operations must have the attribute [Produces(typeof(HttpResponseMessage))]
// in addition to this operation filter
if (operation.OperationId == "ExportToExcel" || operation.OperationId == "ExportToPdf" ||
operation.OperationId == "GetReport" || operation.OperationId == "DownloadFile")
{
foreach (var item in operation.Responses["200"].Content)
item.Value.Schema = new OpenApiSchema { Type = "file" };
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论