Swagger codegen to Java Spring 从二进制格式的 OpenAPI 组件生成不正确的文件响应实体

Swagger codegen to Java Spring generates incorrect file response entity from OpenAPI component of binary format

我正在使用 swagger-codegen-maven-plugin 从 OpenAPI 文件生成 Spring 接口 (OpenAPI 3.0.2)

<groupId>io.swagger.codegen.v3</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>3.0.14</version>

一个休息的回复API应该是PDF文件

components:
    schemas:
        contractFile:
            type: string
            format: binary

生成Java REST接口然后包含以下方法

default ResponseEntity<File> getContract(@ApiParam(value = "File to download",required=true) @PathVariable("uid") String uid) {
...
}

File class 表示文件系统的路径,但我在文件系统上没有文件,只有 java 内存中的字节,我不想将它们保存为文件到磁盘。

我希望 getContract return 内存中 Stream/bytes 的一些 StreamResource 或文件的其他表示形式。是否可以通过 swagger-codegen-maven-plugin 或其他选项实现?

尝试这样的事情:

responses:
     '200':
          description: A PDF file
          content:
            application/pdf:
              schema:
                type: string
                format: binary

也许你可以尝试这样的事情

/myendpoint:
    get:
      tags: [myendpint]
      summary: my cool endpoint
      operationId: getStuff
      x-custom-return-type: 'java.something.stream.Stream'

而不是使用 mustache 并在具有供应商扩展名的 mustache 文件中指定它

ResponseEntity<{{#responseWrapper}}{{
    .}}<{{/responseWrapper}}{{>returnTypes}}{{#vendorExtensions.x-custom-return-type}}

我将其用于接口 API,也许您也可以在您的案例中使用它

最后,我转自

<plugin>
  <groupId>io.swagger.codegen.v3</groupId>
  <artifactId>swagger-codegen-maven-plugin</artifactId>
  <version>3.0.14</version>
  ...
</plugin>

<plugin>
  <groupId>org.openapitools</groupId>
  <artifactId>openapi-generator-maven-plugin</artifactId>
  <version>4.2.3</version>
  ...
</plugin>

这个插件正确地生成了 Resource 而不是 ResponseEntity