Swagger-Codegen-Maven-Plugin用@requestParam生成多部分form-data请求API,而不是@requestpart
我正在使用请求对象[filemetadata]上传文件元数据信息,并将其作为文件上传多部分form-data请求的一部分发送。但是Swagger-Codegen-Plugin用@requestParam作为表单数据请求的第一部分生成代码。因此,我必须在WebMvcconFigurer中注册一个“ json到对象”转换器,以将请求零件转换为FileMetAdata,否则将通过以下错误:
o.s.w.s.m.s.DefaultHandlerExceptionResolver:208 - Resolved [org.springframework.web.method.annotation.MethodArgumentConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type 'com.upload.file.generated.model.FileMetadata'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.upload.file.generated.model.FileMetadata': no matching editors or conversion strategy found]
我有以下问题:
- 是否有任何选项可以生成两个请求参数,并注明了两个请求参数。 @requestpart?
- 如何为FileMetadata请求参数生成@Valid注释?
配置详细信息下面:
打开API规格:
openapi: 3.0.3
...
...
paths:
/api/v1/upload:
post:
tags:
- File upload
summary: Upload a file
operationId: uploadFile
requestBody:
description: File that needs to be uploaded
required: true
content:
multipart/form-data: # Media type
schema:
required:
- metadata
- file
type: object
properties:
metadata:
$ref: '#/components/schemas/FileMetadata'
file:
type: string
format: binary
encoding:
metadata:
contentType: application/json
file:
contentType: application/octet-stream
responses:
201:
description: OK
400:
description: Invalid input
401:
description: Unauthorized
403:
description: Forbidden
404:
description: Not Found
生成的API代码:
@Operation(summary = "Upload a file", description = "", tags={ "File upload" })
@ApiResponses(value = {... })
@RequestMapping(value = "/api/v1/upload",
produces = { "application/json" },
consumes = { "multipart/form-data" },
method = RequestMethod.POST)
ResponseEntity<Void> uploadFile(@Parameter(in = ParameterIn.DEFAULT, description = "", required=true,schema=@Schema()) @RequestParam(value="metadata", required=true) FileMetadata metadata, @Parameter(description = "file detail") @Valid @RequestPart("file") MultipartFile file);
pom.xml:
<plugin>
<groupId>io.swagger.codegen.v3</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>3.0.33</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/openapi.yaml</inputSpec>
<language>spring</language>
<output>${project.basedir}</output>
<modelPackage>com.file.upload.generated.model</modelPackage>
<apiPackage>com.file.upload.generated.api</apiPackage>
<generateModels>true</generateModels>
<generateModelDocumentation>false</generateModelDocumentation>
<generateApis>true</generateApis>
<generateApiDocumentation>false</generateApiDocumentation>
<generateApiTests>false</generateApiTests>
<generateSupportingFiles>false</generateSupportingFiles>
<configOptions>
<performBeanValidation>true</performBeanValidation>
<useBeanValidation>true</useBeanValidation>
<hideGenerationTimestamp>true</hideGenerationTimestamp>
<dateLibrary>java11-localdatetime</dateLibrary>
</configOptions>
<importMappings>
<importMapping>Date=java.time.LocalDate</importMapping>
</importMappings>
<typeMappings>
<typeMapping>Date=LocalDate</typeMapping>
</typeMappings>
</configuration>
</execution>
</executions>
</plugin>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实现这两个目标的最干净的方法是在项目中提供自定义的胡须模板。您可以在版本3.0.33 的文件夹。
对于您的情况,相应的模板将为 formParams.mustache ,我建议在其中更改该行以:
to:
要使用这些自定义模板,只需将模板目录添加到插件配置中:
对于更广泛的指南,请使用更广泛的指南使用小胡子模板,我可以推荐 OpenAPI-Generator的指南。
The cleanest way to achieve both of your goals would be to provide custom mustache templates in your project. You can find them in the Swagger Codegen Generators Repository -> or more precisely, you can find the Spring Templates in this folder for Version 3.0.33 of the generator.
For your case, the corresponding template would be formParams.mustache, where i would suggest to alter the line from:
To:
To use those custom templates just add the template directory to your plugin configuration:
For a more extensive Guide on using mustache templates i can recommend the guide from the openapi-generator on this topic.