我正在使用 openapi-generator 创建一个 multipart/form-data。在理想情况下,我应该能够上传文件,并在选项中指定文件应该发生的情况。
我希望选项成为一个对象。由于某种原因,这似乎不起作用。 openapi-generator 生成 API 接口等,但它不会生成 options 对象的模型。
我可以单独指定选项,但我更喜欢将选项作为一个对象,并为其提供必要的模型。我相信这提供了一种更结构化的方式来处理选项。
我的 yaml 文件如下所示(我指定了哪些有效,哪些无效):
/fileuploadwithoptions:
post:
summary: Upload a file and processes it according to the options specified.
requestBody:
content:
multipart/form-data:
schema:
required:
- file
type: object
properties:
file:
type: string
format: binary
option1: <-- this works
type: string
description: A descriptions for option 1.
options: <-- this does not work
#type: application/json
type: object
description: The options.
properties:
option1:
type: string
description: A descriptions for option 1.
option2:
type: string
description: A descriptions for option 2.
encoding:
file:
contentType: application/octet-stream
这会生成以下 API:
/**
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech) (6.0.1).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
package com.teradact.tokenizerplusserver.api;
import com.teradact.tokenizerplusserver.model.FileuploadwithoptionsPostRequestOptions;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.multipart.MultipartFile;
import javax.validation.Valid;
import javax.validation.constraints.*;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.annotation.Generated;
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2022-10-11T08:11:04.909499+02:00[Europe/Brussels]")
@Validated
@Tag(name = "fileuploadwithoptions", description = "the fileuploadwithoptions API")
public interface FileuploadwithoptionsApi {
default Optional<NativeWebRequest> getRequest() {
return Optional.empty();
}
/**
* POST /fileuploadwithoptions : Upload a file and processes it according to the options specified.
*
* @param file (required)
* @param option1 A descriptions for option 1. (optional)
* @param options (optional)
* @return The file. (status code 200)
* or bad input parameter (status code 400)
*/
@Operation(
operationId = "fileuploadwithoptionsPost",
summary = "Upload a file and processes it according to the options specified.",
responses = {
@ApiResponse(responseCode = "200", description = "The processed file.", content = {
@Content(mediaType = "application/octet-stream", schema = @Schema(implementation = org.springframework.core.io.Resource.class))
}),
@ApiResponse(responseCode = "400", description = "bad input parameter")
}
)
@RequestMapping(
method = RequestMethod.POST,
value = "/fileuploadwithoptions",
produces = { "application/octet-stream" },
consumes = { "multipart/form-data" }
)
default ResponseEntity<org.springframework.core.io.Resource> fileuploadwithoptionsPost(
@Parameter(name = "file", description = "", required = true) @RequestPart(value = "file", required = true) MultipartFile file,
@Parameter(name = "option1", description = "A descriptions for option 1.") @Valid @RequestParam(value = "option1", required = false) String option1,
@Parameter(name = "options", description = "") @Valid @RequestParam(value = "options", required = false) FileuploadwithoptionsPostRequestOptions options
) {
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
}
}
但这会出现以下错误:
“无法解析符号'FileuploadwithoptionsPostRequestOptions”,因为根本没有创建该对象的模型。
提前感谢您指出我错误的地方!
接口的名称为“FileuploadwithoptionsApi”,YAML 文件的名称为 /fileuploadwithoptions,响应实体的名称为 fileuploadwithoptionsPost,但 RequestParam 的对象为“FileuploadwithoptionsPostRequestOptions”,是否因此无法解析符号。
@Parameter(name = "options", description = "") @Valid @RequestParam(value = "options", required = false) FileuploadwithoptionsPostRequestOptions options)
对象“FileuploadwithoptionsPostRequestOptions”是否已实例化,或者是否具有与 @Request Param 中存在的对象匹配的类似类或接口。
好的,您已经导入了对象所需的类,请尝试使用@RequestPart,就像您用于文件一样。试试看是否有效。