You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

OpenApiRequestBody处理multipart/form-data请求渲染异常求助

Azure Functions中OpenApiRequestBody处理multipart/form-data的问题

我在Visual Studio中使用Azure Functions模板和Http trigger with OpenApi函数,想要通过OpenApiRequestBodyAttribute指定函数接收multipart/form-data类型请求,其中「server」参数是JSON对象,「file」参数是二进制数据。但Microsoft.OpenApi库文档不足,没找到对应示例。

现有代码

public class SFTPEndpoints
{
    private readonly ILogger<SFTPEndpoints> _logger;

    public SFTPEndpoints(ILogger<SFTPEndpoints> log)
    {
        _logger = log;
    }

    public record Server(string Url, string Username, string Password, int Port, string Path);
    public record ServerWithFileBytes(Server Server, byte[] File);

    [FunctionName("sftp-string-upload")]
    [OpenApiOperation(operationId: "sftp-string-upload", tags: new[] { "sftp" })]
    [OpenApiSecurity("function_key", SecuritySchemeType.ApiKey, Name = "code", In = OpenApiSecurityLocationType.Query)]
    [OpenApiRequestBody("multipart/form-data", typeof(ServerWithFileBytes), Description = "Upload a file to the SFTP Server.")]
    [OpenApiResponseWithoutBody(HttpStatusCode.OK, Summary = "The file was uploaded successfully.")]
    public async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req)
    {
        _logger.LogInformation("C# HTTP trigger function processed a request.");

        await Console.Out.WriteLineAsync(req.ToString());

        return new OkObjectResult(JsonConvert.SerializeObject(req));
    }
}

遇到的问题

  • 生成的Swagger文档(访问localhost/api/swagger/ui)中,点击「Try it out」后「server」属性的文本框为空,无法像手动编写的规范那样预填充Server记录的Schema。
  • 无法查看库生成的实际YAML/JSON规范,访问localhost/api/openapi/3.0始终返回「Invalid OpenApi Version」错误。已重写DefaultOpenApiConfigurationOptions类强制使用OpenApi3,否则请求体将完全空白,代码如下:
internal class OpenApiConfigurationOptions : DefaultOpenApiConfigurationOptions
{
    public override OpenApiVersionType OpenApiVersion { get; set; } = OpenApiVersionType.V3;
}

手动编写的有效规范

openapi: 3.0.3
info:
  version: 1.0.0
  title: FTP Server API
  description:
    API methods for interacting with SFTP and FTPS file servers.
servers:
  - url: example.com/api
paths:
  /sftp/upload/file:
    put:
      summary: Upload a file to the SFTP server.
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                ServerData:
                  $ref: "#/components/schemas/Server"
                File:
                  type: string
                  format: binary
      responses:
        200:
          description: File uploaded successfully.
components:
  schemas:
    Server:
      type: object
      properties:
        Url:
          type: string
          description: Can be a URL or an IP.
          example: ftp.example.com
        Username:
          type: string
        Password:
          type: string
          format: password
        Port:
          type: integer
        Path:
          type: string
    ServerWithFile:
      allOf:
        - $ref: "#/components/schemas/Server"
        - type: object
          properties:
            Filename:
              type: string
            File:
              type: string
              format: byte

恳请告知如何修正注解,或如何查看库生成的规范。

内容的提问来源于stack exchange,提问作者Caleb Keller

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 22:34:55