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
相关产品推荐
相关产品推荐

