Swashbuckle.AspNetCore.SwaggerGen 6+ 的版本。仅删除 text/plain 内容类型。在我的情况下适用于字符串结果。
internal class RemoveTextContentOperationFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
foreach (var (_, response) in operation.Responses)
{
if (response.Content.ContainsKey("text/plain"))
response.Content.Remove("text/plain");
}
}
}
为字符串操作生成的招摇:
"/api/news/{newsArticleId}/file-link": {
"get": {
"tags": [
"NewsApi"
],
"operationId": "NewsApi_GetUploadFileLink",
"parameters": [
{
"name": "newsArticleId",
"in": "path",
"required": true,
"schema": {
"type": "integer",
"format": "int32"
}
}
],
"responses": {
"500": {
"description": "Server Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ApiError"
}
},
"text/json": {
"schema": {
"$ref": "#/components/schemas/ApiError"
}
}
}
},
"401": {
"description": "Unauthorized",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ApiError"
}
},
"text/json": {
"schema": {
"$ref": "#/components/schemas/ApiError"
}
}
}
},
"200": {
"description": "Success",
"content": {
"application/json": {
"schema": {
"type": "string"
}
},
"text/json": {
"schema": {
"type": "string"
}
}
}
}
}
}
},