Open API Maven插件:如何生成文件上传代码

时间:2020-11-10 17:45:22

标签: java feign openapi-generator

我无法为文件上传生成开放式api生成的Feign客户端。

这是我试图用来上传文件数组的开放api requestBodies 组件的片段

requestBodies:
    FilesRequestBody:
      description: Array of files
      content:
        multipart/form-data:
          schema:
            type: object
            properties:
              files:
                type: array
                items:
                  type: file

我的pom文件包含以下插件设置

                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>4.2.3</version>
                <executions>
                    <execution>
                        <id>generate-feign-client</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>${project.build.directory}/contract/api.yaml</inputSpec>
                            <language>java</language>
                            <output>${project.build.directory}/generated-sources</output>
                            <library>feign</library>
                            <typeMappings>
                                <mapping>file=File</mapping>
                            </typeMappings>
                        </configuration>
                    </execution>

请注意 typeMappings 配置-这不起作用。

理想情况下,我想要一个生成的Feign客户程序,它看起来像..

import feign.Param;
import feign.RequestLine;

import java.io.File;

public interface ImagesApi {

    @RequestLine("POST /api/images")
    @Headers({
            "Content-Type: multipart/form-data",
            "Accept: application/json"
    })
    List<Image> uploadImages(@Param("config") List<File> files);

}

但是,这是生成的,显然不会编译

    @RequestLine("POST /api/images")
    @Headers({
        "Content-Type: multipart/form-data",
        "Accept: application/json",
    })
    List<Image> uploadImages(UNKNOWN_BASE_TYPE UNKNOWN_BASE_TYPE);

关于如何使Maven插件使用java.io.File之类的Java类型的任何想法?

1 个答案:

答案 0 :(得分:0)

似乎openapi生成器中存在错误。它无法处理

     $ref '#/components/requestBodies/FilesRequestBody'

因此,要使其正常运行,请不要使用组件引用。内联请求正文

  /api/images:
    post:
      tags:
        - image
      summary: uploadImage
      operationId: uploadImage
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                file_list:
                  type: array
                  items:
                    type: string
                    format: binary
      responses:
        "201":
          $ref: '#/components/responses/ImagesResponse'
        default:
          $ref: '#/components/responses/ErrorResponse'