Swagger codegen 生成重复的变量

Swagger codegen generates duplicated variables

我正在尝试从包含

的 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

这意味着,acceptcontentType 将出现在生成的方法签名中。

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

<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

我得到

Error:(130,31) java: variable accept is already defined in method

并且生成的代码包含

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

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

在 OpenAPI 2.0 中,AcceptContent-Type headers 应该使用 consumesproduces 而不是参数来定义。在 OpenAPI 3.0 中,这些 headers 被定义为 request/response 媒体类型。

按如下方式更改您的操作定义:

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:
                ...

派对有点晚了,但我在使用第 3 方时遇到了同样的问题 API,这是搜索中出现的第一个问题。 openapi yaml 在我的案例中似乎有效,并且由于它是第 3 方,因此无论如何更改它都是不可行的。我不得不使用 localVariablePrefix(变量完全拼写出来)。

所以要修改你的pom.xml:

<configOptions>
    <dateLibrary>joda</dateLibrary>
    <localVariablePrefix>localVar</localVariablePrefix>
</configOptions>
...