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

Quarkus OpenAPI生成器能否支持带索引数组的multipart/form-data请求

Quarkus OpenAPI Generator 能否支持索引式数组参数的multipart请求?

我通过Quarkus OpenAPI Generator从openapi.yaml生成了如下multipart POST接口,其中properties为数组类型:

@POST
@Path("/document")
@Consumes({"multipart/form-data"})
@Produces({"application/json"})
@GeneratedMethod ("addDocument")
public javax.ws.rs.core.Response addDocument(
    @javax.ws.rs.BeanParam AddDocumentMultipartForm multipartForm
);

public static class AddDocumentMultipartForm {
    @FormParam("fileContent")
    @org.jboss.resteasy.reactive.PartFilename("fileContentFile")
    @org.jboss.resteasy.reactive.PartType(MediaType.APPLICATION_OCTET_STREAM)
    public FileUpload fileContent;
    @FormParam("properties")
    @org.jboss.resteasy.reactive.PartType(MediaType.TEXT_PLAIN)
    public java.util.List<Long> properties;
}

当前接口可以正常处理重复name字段的数组请求:

[
    {
        "name": "properties",
        "value": 127
    },
    {
        "name": "properties",
        "value": 128
    },
    {
        "name": "fileContent",
        "type": "application/pdf",
        "filename": "sample.pdf"
    }
]

但我需要支持带索引的数组参数格式的multipart请求:

[
    {
        "name": "properties[0]",
        "value": 127
    },
    {
        "name": "properties[1]",
        "value": 128
    },
    {
        "name": "fileContent",
        "type": "application/pdf",
        "filename": "sample.pdf"
    }
]

请问能否通过Quarkus OpenAPI Generator实现这一需求?


可以实现,关键是调整OpenAPI定义文件的参数配置,而非直接修改生成的代码:

  • 修改OpenAPI YAML中的数组参数定义
    在openapi.yaml里,为properties数组参数指定style: form和explode: false,这会让生成的接口支持索引式参数格式。示例定义如下:

    paths:
      /document:
        post:
          consumes:
            - multipart/form-data
          produces:
            - application/json
          operationId: addDocument
          requestBody:
            content:
              multipart/form-data:
                schema:
                  type: object
                  properties:
                    fileContent:
                      type: string
                      format: binary
                    properties:
                      type: array
                      items:
                        type: integer
                        format: int64
                      style: form
                      explode: false
            required: true
    
  • 重新生成代码
    基于修改后的OpenAPI YAML文件重新运行Quarkus OpenAPI Generator,生成的AddDocumentMultipartForm类会自动适配索引式参数格式,Resteasy Reactive底层会负责解析properties[0]、properties[1]这类参数并映射到List<Long>中。

补充说明:Quarkus OpenAPI Generator严格遵循OpenAPI规范的参数样式定义,style: form+explode: false对应索引式数组参数格式;默认的explode: true则对应重复字段名的格式,通过调整这两个参数即可切换支持的请求格式。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 10:17:48