示例请求正文中 JsonPatchDocument 的 Swagger 意外 API PATCH 操作文档

Luk*_*kas 3 c# asp.net-web-api swagger swashbuckle asp.net-core

我正在制作 Core 3.1 Web API 并使用JsonPatch创建 PATCH 操作。我有一个名为的动作Patch,它有一个JsonPatchDocument参数。这是动作的签名:

[HttpPatch("{id}")]
public ActionResult<FileRecordDto> Patch(int id, [FromBody] JsonPatchDocument<FileRecordQueryParams> patchDoc)
Run Code Online (Sandbox Code Playgroud)

据我了解,该参数需要以以下结构接收 JSON 数据,我已成功使用该操作对其进行了测试:

[
  {
    "op": "operationName",
    "path": "/propertyName",
    "value": "newPropertyValue"
  }
]
Run Code Online (Sandbox Code Playgroud)

但是,由Swagger生成的操作文档具有不同的结构: 在此处输入图片说明

我不熟悉这种结构,甚至"value"它也缺少JsonPatchDocument对象所具有的属性。replace我见过的每个修补操作的例子都有第一个结构。

为什么 Swagger 会JsonPatchDocument为 PATCH 端点的请求正文中的对象生成替代结构?我该如何解决?

为 Swagger 安装的 NuGet 包: Swashbuckle.AspNetCore v5.6.3

mj1*_*313 6

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" }
            };
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

注册过滤器:

services.AddSwaggerGen(c => c.DocumentFilter<JsonPatchDocumentFilter>());
Run Code Online (Sandbox Code Playgroud)

结果:

在此处输入图片说明