Swagger unexpected API 示例请求正文中 JsonPatchDocument 的 PATCH 操作文档
Swagger unexpected API PATCH action documentation of JsonPatchDocument in example request body
我正在制作 Core 3.1 web API 并使用 JsonPatch 创建 PATCH 操作。我有一个名为 Patch
的动作,它有一个 JsonPatchDocument
参数。这是操作的签名:
[HttpPatch("{id}")]
public ActionResult<FileRecordDto> Patch(int id, [FromBody] JsonPatchDocument<FileRecordQueryParams> patchDoc)
据我了解,该参数需要接收以下结构中的 JSON 数据,我已成功通过操作测试:
[
{
"op": "operationName",
"path": "/propertyName",
"value": "newPropertyValue"
}
]
但是,Swagger 生成的操作文档具有不同的结构:
我不熟悉这个结构,甚至 "value"
属性 也没有,JsonPatchDocument
对象有。我见过的每个用 replace
操作打补丁的例子都有第一个结构。
为什么 Swagger 在 PATCH 端点的请求正文中为 JsonPatchDocument
对象生成替代结构?我该如何解决这个问题?
为 Swagger 安装的 NuGet 包:
Swashbuckle.AspNetCore
不适用于此类型 JsonPatchDocument<UpdateModel>
,它不代表预期的补丁请求文件。
您需要自定义一个文档过滤器来修改生成的规范。
public class JsonPatchDocumentFilter : IDocumentFilter
{
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
{
var schemas = swaggerDoc.Components.Schemas.ToList();
foreach (var item in schemas)
{
if (item.Key.StartsWith("Operation") || item.Key.StartsWith("JsonPatchDocument"))
swaggerDoc.Components.Schemas.Remove(item.Key);
}
swaggerDoc.Components.Schemas.Add("Operation", new OpenApiSchema
{
Type = "object",
Properties = new Dictionary<string, OpenApiSchema>
{
{"op", new OpenApiSchema{ Type = "string" } },
{"value", new OpenApiSchema{ Type = "string"} },
{"path", new OpenApiSchema{ Type = "string" } }
}
});
swaggerDoc.Components.Schemas.Add("JsonPatchDocument", new OpenApiSchema
{
Type = "array",
Items = new OpenApiSchema
{
Reference = new OpenApiReference { Type = ReferenceType.Schema, Id = "Operation" }
},
Description = "Array of operations to perform"
});
foreach (var path in swaggerDoc.Paths.SelectMany(p => p.Value.Operations)
.Where(p => p.Key == Microsoft.OpenApi.Models.OperationType.Patch))
{
foreach (var item in path.Value.RequestBody.Content.Where(c => c.Key != "application/json-patch+json"))
path.Value.RequestBody.Content.Remove(item.Key);
var response = path.Value.RequestBody.Content.Single(c => c.Key == "application/json-patch+json");
response.Value.Schema = new OpenApiSchema
{
Reference = new OpenApiReference { Type = ReferenceType.Schema, Id = "JsonPatchDocument" }
};
}
}
}
注册过滤器:
services.AddSwaggerGen(c => c.DocumentFilter<JsonPatchDocumentFilter>());
结果:
我正在制作 Core 3.1 web API 并使用 JsonPatch 创建 PATCH 操作。我有一个名为 Patch
的动作,它有一个 JsonPatchDocument
参数。这是操作的签名:
[HttpPatch("{id}")]
public ActionResult<FileRecordDto> Patch(int id, [FromBody] JsonPatchDocument<FileRecordQueryParams> patchDoc)
据我了解,该参数需要接收以下结构中的 JSON 数据,我已成功通过操作测试:
[
{
"op": "operationName",
"path": "/propertyName",
"value": "newPropertyValue"
}
]
但是,Swagger 生成的操作文档具有不同的结构:
我不熟悉这个结构,甚至 "value"
属性 也没有,JsonPatchDocument
对象有。我见过的每个用 replace
操作打补丁的例子都有第一个结构。
为什么 Swagger 在 PATCH 端点的请求正文中为 JsonPatchDocument
对象生成替代结构?我该如何解决这个问题?
为 Swagger 安装的 NuGet 包:
Swashbuckle.AspNetCore
不适用于此类型 JsonPatchDocument<UpdateModel>
,它不代表预期的补丁请求文件。
您需要自定义一个文档过滤器来修改生成的规范。
public class JsonPatchDocumentFilter : IDocumentFilter
{
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
{
var schemas = swaggerDoc.Components.Schemas.ToList();
foreach (var item in schemas)
{
if (item.Key.StartsWith("Operation") || item.Key.StartsWith("JsonPatchDocument"))
swaggerDoc.Components.Schemas.Remove(item.Key);
}
swaggerDoc.Components.Schemas.Add("Operation", new OpenApiSchema
{
Type = "object",
Properties = new Dictionary<string, OpenApiSchema>
{
{"op", new OpenApiSchema{ Type = "string" } },
{"value", new OpenApiSchema{ Type = "string"} },
{"path", new OpenApiSchema{ Type = "string" } }
}
});
swaggerDoc.Components.Schemas.Add("JsonPatchDocument", new OpenApiSchema
{
Type = "array",
Items = new OpenApiSchema
{
Reference = new OpenApiReference { Type = ReferenceType.Schema, Id = "Operation" }
},
Description = "Array of operations to perform"
});
foreach (var path in swaggerDoc.Paths.SelectMany(p => p.Value.Operations)
.Where(p => p.Key == Microsoft.OpenApi.Models.OperationType.Patch))
{
foreach (var item in path.Value.RequestBody.Content.Where(c => c.Key != "application/json-patch+json"))
path.Value.RequestBody.Content.Remove(item.Key);
var response = path.Value.RequestBody.Content.Single(c => c.Key == "application/json-patch+json");
response.Value.Schema = new OpenApiSchema
{
Reference = new OpenApiReference { Type = ReferenceType.Schema, Id = "JsonPatchDocument" }
};
}
}
}
注册过滤器:
services.AddSwaggerGen(c => c.DocumentFilter<JsonPatchDocumentFilter>());
结果: