【问题标题】:Swagger UI doesn't pass content-type for a part within a multipart requestSwagger UI 不会为多部分请求中的部分传递内容类型
【发布时间】:2019-12-06 16:36:03
【问题描述】:

我正在使用以下代码配置 ASP.NET Core 3.1 控制器方法来上传包含 2 个部分的多部分请求,一个名为“元数据”以包含 JSON 模型,另一部分名为“文件”以包含实际文件二进制。 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; }
}

【问题讨论】:

    标签: asp.net-core swagger-ui


    【解决方案1】:

    这是 Swagger UI 的一个问题 - 目前(截至 2019 年 12 月)它不会将 Content-Type 标头添加到 multipart/* 请求中的 JSON 数据中。该问题在此处跟踪:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-11
      • 2019-06-23
      • 2014-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多