Warm tip: This article is reproduced from serverfault.com, please click

java-Swagger codegen 生成重复的变量

(java - Swagger codegen generates duplicated variables)

发布于 2020-03-03 11:21:34

我正在尝试从包含的 yaml 生成客户端

  acceptParam:
    name: Accept
    type: string
    required: true
    in: header
    description: Accepted Content-type. Should be set to application/json
  contentTypeParam:
    name: Content-Type
    type: string
    required: true
    in: header
    description: Request Content-type. Should be set to application/json

这意味着,accept并且contentType将出现在生成的方法签名中。

最重要的是,我配置了这样的插件

<plugin>
    <groupId>io.swagger.codegen.v3</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <version>3.0.18</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <phase>generate-sources</phase>
            <configuration>
                <inputSpec>${project.basedir}/src/main/resources/swagger.yaml</inputSpec>
                <language>java</language>
                <configOptions>
                    <dateLibrary>joda</dateLibrary>
                    <localVarPrefix>localVar</localVarPrefix>
                </configOptions>
                <library>resttemplate</library>
                <output>${project.build.directory}/generated-sources</output>
                <modelPackage>com.example.client.model</modelPackage>
                <apiPackage>com.example.client.api</apiPackage>
                <generateApiTests>false</generateApiTests>
                <generateModelTests>false</generateModelTests>
            </configuration>
        </execution>
    </executions>
</plugin>

还是之后 mvn clean install

我越来越

错误:(130,31)java:变量accept已经在方法中定义

并且生成的代码包含

public Response authorize(Request body, String accept, ...) {
   ....
   final List<MediaType> accept = apiClient.selectHeaderAccept(accepts);    
   ....
}

我尝试了不同版本的插件(从 2.3.0 到最新版本),在克服了许多其他问题后,我总是以这样的方式结束。

Questioner
JohnDoe
Viewed
0
Helen 2020-03-04 15:38:10

在 OpenAPI 2.0 中,应该使用and而不是参数来定义AcceptContent-Type标头在 OpenAPI 3.0 中,这些标头被定义为请求/响应媒体类型。consumesproduces

更改你的操作定义如下:

swagger: '2.0'

paths:
  /foo:
    post:
      consumes:
        - application/json
      produces:
        - application/json
      ...

或者如果你使用 OpenAPI 3.0:

openapi: 3.0.0

paths:
  /foo:
    post:
      requestBody:
        content:
          application/json:     # <----
            schema:
              ...
      responses:
        '200':
          description: ok
          content:
            application/json:   # <----
              schema:
                ...