如何在Swashbuckle.AspNetCore中定义’File’模式类型

huangapple go评论42阅读模式
英文:

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&lt;IActionResult&gt; ExportToExcel(string listName, string language, [FromBody] List&lt;int&gt; personIds,
	DateTime startDate, DateTime endDate, List&lt;int&gt; notSelectedTimeTypeIds
)
{
	var stream = await AbsencePlanningProvider.Instance.ExportToExcel(User.GetSub(), language, listName, personIds,
		startDate, endDate, notSelectedTimeTypeIds);
	stream.Position = 0;
	return File(stream, &quot;application/octet-stream&quot;);
}

...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 == &quot;ExportToExcel&quot; || operation.OperationId == &quot;ExportToPdf&quot; ||
			operation.OperationId == &quot;GetReport&quot; || operation.OperationId == &quot;DownloadFile&quot;)
		{
			foreach (var item in operation.Responses[&quot;200&quot;].Content)
				item.Value.Schema = new OpenApiSchema { Type = &quot;file&quot; };
		}
	}
}

huangapple
  • 本文由 发表于 2023年6月1日 17:10:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76380336.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定