繁体   English   中英

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

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

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

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

一个rest 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) {
...
}

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

我希望 getContract 从 memory 中的 Stream/bytes 返回一些 StreamResource 或其他一些文件表示形式。这是否可能通过 swagger-codegen-maven-plugin 或其他一些选项?

最后,我从

<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

尝试这样的事情:

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,也许你也可以在你的情况下使用它

暂无
暂无

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

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