Swagger UI不会在多部分请求中传递部分的内容类型

时间:2019-12-05 22:07:55

标签: asp.net-core swagger-ui

我正在使用以下代码来配置ASP.NET Core 3.1控制器方法,以上载包含2个部分的多部分请求,一个部分名为“元数据”以包含JSON模型,另一部分名为“ file”以包含实际文件二进制Swagger UI似乎可以正确呈现,但是在调用该操作时,它从未为我的元数据部分发送“内容类型”。

调试我的控制器方法时,文件二进制文件的部分很好,并包含“ content-type”标头,但是当我检查元数据部分的标头时,“ content-type”标头丢失了。对于元数据部分,我收到的唯一标头是“ content-disposition”。

我确实确认swagger.json正确显示“ application / json”作为元数据部分的内容类型。

当前我正在使用Swashbuckle.AspNetCore v5.0.0-rc4

我在操作过滤器中缺少任何东西吗?

public class FileUploadOperationFilter : IOperationFilter
{
    public virtual void Apply(OpenApiOperation operation, OperationFilterContext context)
    {
        context?.MethodInfo?.GetCustomAttribute<FileUploadOperationAttribute>()?.Apply(operation, context);
    }
}

[AttributeUsage(AttributeTargets.Method)]
public class FileUploadOperationAttribute : Attribute
{
    public Type MetadataType { get; }

    public string MetadataPropertyName { get; set; } = "metadata";

    public string FilePropertyName { get; set; } = "file";

    public FileUploadOperationAttribute(Type metadataType)
    {
        MetadataType = metadataType;
    }

    public virtual void Apply(OpenApiOperation operation, OperationFilterContext context)
    {
        if (operation == null || context == null)
            return;

        var metadataSchema = context.SchemaGenerator.GenerateSchema(
            MetadataType,
            context.SchemaRepository);

        operation.RequestBody = new OpenApiRequestBody
        {
            Content = new Dictionary<string, OpenApiMediaType>
            {
                ["multipart/form-data"] = new OpenApiMediaType
                {
                    Schema = new OpenApiSchema
                    {
                        Type = "object",
                        Properties = new Dictionary<string, OpenApiSchema>
                        {
                            [MetadataPropertyName] = metadataSchema,
                            [FilePropertyName] = new OpenApiSchema
                            {
                                Type = "string",
                                Format = "binary",
                            },
                        },
                    },

                    Encoding = new Dictionary<string, OpenApiEncoding>
                    {
                        [MetadataPropertyName] = new OpenApiEncoding
                        {
                            ContentType = "application/json",
                        },
                    },
                }
            },
        };
    }

}

[HttpPut("{fileName}/versions/{version}/content")]
[FileUploadOperation(typeof(SubmitFileRequest))]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(typeof(GenericActionResponse), StatusCodes.Status404NotFound)]
public async Task<IActionResult> SubmitFileVersionContentAsync([FromRoute] string fileName, [FromRoute] string version, CancellationToken cancellationToken)
{
    // code omitted for brevity...
}

public class SubmitFileRequest
{
    [Required]
    public DateTime CreationTimeUtc { get; set; }

    [Required]
    public DateTime LastWriteTimeUtc { get; set; }

    [Required]
    public string InstallMethod { get; set; }
}

enter image description here

1 个答案:

答案 0 :(得分:2)

这是Swagger UI的问题–当前(截至2019年12月),它没有向Content-Type请求中的JSON数据添加multipart/*头。在此跟踪该问题:

https://github.com/swagger-api/swagger-ui/issues/5356