简体   繁体   English

在 Spring 中使用多部分请求自动生成 OpenAPI

[英]Automatic OpenAPI generation in Spring with multipart requests

I have a problem with the OpenAPI generator in a spring boot application generating the definition of a REST endpoint that accepts a multipart request.我在 spring 引导应用程序中的 OpenAPI 生成器存在问题,该应用程序生成接受多部分请求的 REST 端点的定义。

When I am explicitly defining the parts like shown below.当我明确定义如下所示的部分时。 It generates an OpenAPI definition that is working/valid when used in for example in Azure API Management.它会生成一个在 Azure API 管理中使用时有效/有效的 OpenAPI 定义。

@PostMapping(consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
public List<String> upload(@RequestPart MultipartFile file, @RequestPart UploadMetaData data) {

  . . .

}
paths:
  /api/v1/upload:
    post:
      operationId: upload
      requestBody:
        content:
          multipart/form-data:
            schema:
              required:
              - data
              - file
              type: object
              properties:
                gatewayId:
                  type: string
                file:
                  type: string
                  format: binary
                data:
                  $ref: '#/components/schemas/UploadMetaData'
      responses:
        "200":
          description: OK
          content:
            '*/*':
              schema:
                type: array
                items:
                  type: string

But when I am trying to use MultipartRequest instead of the annotated - File , the controller itself is still working as expected accepting an arbitrary list of multipart segments when I call it directly, but the OpenAPI definition that is generated is not reflecting the actual interface because it is trying to use the request parameter as an URL path parameter, so when I import this spec into Azure API Management it is not working.但是,当我尝试使用MultipartRequest而不是带注释的 - File时, controller 本身仍在按预期工作,当我直接调用它时接受任意多部分段列表,但是生成的 OpenAPI 定义没有反映实际接口,因为它试图将request参数用作 URL 路径参数,因此当我将此规范导入 Azure API 管理时,它不起作用。

@PostMapping(consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
public List<String> upload(MultipartRequest request) {

  . . .

}
paths:
  /api/v1/upload:
  post:
    operationId: upload
    parameters:
    - name: request
      in: query
      required: true
      schema:
        $ref: '#/components/schemas/MultipartRequest'
    requestBody:
      content:
        multipart/form-data:
          schema:
            type: string
    responses:
      "200":
        description: OK
        content:
          '*/*':
            schema:
              type: array
              items:
                type: string

Other than giving my API a name, I didn't configure anything special for the generator:除了给我的 API 一个名字之外,我没有为生成器配置任何特殊的东西:

@OpenAPIDefinition(info = @Info(title = "My REST API", version = "1.0"))
public class MySpringApplication {

. . . 

}
<dependency>
  <groupId>org.springdoc</groupId>
  <artifactId>springdoc-openapi-ui</artifactId>
  <version>1.5.12</version>
</dependency>

Is this a bug or am I missing some annotation or other configuration here?这是一个错误还是我在这里遗漏了一些注释或其他配置?

The MultipartRequest , is not supported out of the box. MultipartRequest不支持开箱即用。 You can use the following code to enable the support.您可以使用以下代码启用支持。

static{
    SpringDocUtils.getConfig().addFileType(MultipartRequest.class);
}

This support will be added for the future release.将在未来的版本中添加此支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM