【问题标题】:when using swagger codegen getting 'List<byte[]>' instead of simply 'byte[]'当使用 swagger codegen 获取 'List<byte[]>' 而不是简单的 'byte[]'
【发布时间】:2019-05-29 08:52:22
【问题描述】:

一个 byte[] 在 swagger 文件中被建模为一个 byte[] 的数组。使用 swagger codegen 时,我们得到的是 List&lt;byte[]&gt; 而不是简单的 byte[]

Swagger.json

"document": {
    "type": "array",
    "items": 
    {
        "type": "string",
        "format": "byte"
    }
}

pom.xml

<plugin>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <version>2.3.1</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>${project.basedir}/src/main/resources/swagger.json</inputSpec>
                <language>java</language>
                <configOptions>
                   <sourceFolder>src/gen/java/main</sourceFolder>
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>

【问题讨论】:

    标签: swagger maven-plugin swagger-codegen


    【解决方案1】:

    问题在于生成 swagger.json 文件,即 maven 插件 swagger-maven-plugin。 byte[] 的正确 swagger.json 文件应如下所示:

    "document": {
            "type": "string",
            "format": "byte"
     }
    

    为了实现这一点,我们必须完全按照以下链接所示添加自定义 ModelConvertors: https://github.com/kongchen/swagger-maven-plugin/issues/422

    还在项目 pom 文件中添加 ModelConvertors 标记,其中包含自定义模型转换器位置的路径。

    注意:swagger-codegen-maven-plugin 没有变化。

    【讨论】:

    【解决方案2】:

    如果您将 swagger-codegen-maven-plugin 用于 OpenAPI v.2 json 文件,则返回类型为 byte[] 的响应如下所示:

    "responses": {
      "200": {
        "description": "byte[] return example for swagger: 2.0",
        "schema": {
          "type": "string",
          "format": "byte"
        }
      }
    }
    

    OpenAPI v.3 的响应如下所示:

    "responses": {
      "200": {
        "description": "byte[] return example for openapi: 3.0",
        "content": {
          "*/*": {
            "schema": {
              "type": "string",
              "format": "byte"
            }
          }
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-16
      • 2015-08-06
      • 2010-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多