java - Swagger codegen 生成重复的变量

标签 java http yaml swagger swagger-codegen

我正在尝试从包含的 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到最新版本),在克服了许多其他问题后,我总是以这样的方式结束。

最佳答案

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

因此要修改您的 pom.xml:

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

关于java - Swagger codegen 生成重复的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60506131/

相关文章:

YamlDotNet:如何自定义反序列化器以处理具有不同类型值的相同键

java - 什么是 sjavac,它适用于谁以及如何使用它?

java - udf内的PIG超时

java - 通过 SMTP 发送邮件时获取 "MessagingException: Could not convert socket to TLS"

c - 翻译 curl -v --data 命令

python - CondaEnvException : Pip failed. 尝试使用 .yml 文件在 conda 中创建环境时出现 pip 子进程错误

java - 使用 HTTPS 保护 Netbeans 6.7 tomcat 管理器 URL

android - Android Volley 的 onResponse 方法未执行

http - CORS 使用带有 ARR 和 URL 重写的 IIS 作为反向代理

javascript - 如何在 JavaScript 中将 JSON 转换为 YAML