首页 > 解决方案 > 示例请求正文中 JsonPatchDocument 的 Swagger 意外 API PATCH 操作文档

问题描述

我正在制作一个 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我见过的每个修补操作的例子都有第一个结构。

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

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

标签: c#asp.net-coreasp.net-web-apiswaggerswashbuckle

解决方案


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>());

结果:

在此处输入图像描述


推荐阅读