OpenAPI生成Java客户端时contentType变量重复定义错误
问题:OpenAPI生成Java客户端时出现变量重名编译错误
OpenAPI 3.0.1 Schema文件
openapi: 3.0.1 info: title: tt version: 1.0.0 servers: - url: https://1.1.1.1 paths: /controller/user/login: post: tags: - default summary: Login requestBody: content: application/json: schema: type: object example: email: user password: user from: user parameters: - name: Content-Type in: header schema: type: string example: application/json responses: '200': description: Successful response content: application/json: {}
Maven插件配置
<plugin> <groupId>org.openapitools</groupId> <artifactId>openapi-generator-maven-plugin</artifactId> <version>4.3.1</version> <executions> <execution> <id>id1</id> <goals> <goal>generate</goal> </goals> <configuration> <inputSpec>${project.basedir}/src/main/resources/open-api/schema_1.yaml</inputSpec> <generatorName>java</generatorName> <library>google-api-client</library> <skipValidateSpec>true</skipValidateSpec> <output>${codegen.rest.dir}</output> <modelPackage>${basepackage}.pkg1</modelPackage> <apiPackage>${basepackage}.pkg1.interfaces</apiPackage> <invokerPackage>${basepackage}.pkg1.client</invokerPackage> <addCompileSourceRoot>false</addCompileSourceRoot> <generateSupportingFiles>true</generateSupportingFiles> <generateApis>true</generateApis> <generateApiDocumentation>false</generateApiDocumentation> <generateApiTests>false</generateApiTests> <generateModelTests>false</generateModelTests> <generateModelDocumentation>false</generateModelDocumentation> <configOptions> <java8>true</java8> <library>resttemplate</library> <dateLibrary>java8</dateLibrary> <sourceFolder>.</sourceFolder> <delegatePattern>false</delegatePattern> </configOptions> </configuration> </execution> </executions> </plugin>
编译错误信息
DefaultApi.java:[91,25] variable contentType is already defined in method controllerUserLoginPostWithHttpInfo(java.lang.String,java.lang.Object)
生成代码中的问题
生成的DefaultApi.java中,方法参数contentType与方法内局部变量contentType重名:
public ResponseEntity<Void> controllerUserLoginPostWithHttpInfo(String contentType, Object body) throws RestClientException { Object postBody = body; String path = apiClient.expandPath("/controller/user/login", Collections.<String, Object>emptyMap()); final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>(); final HttpHeaders headerParams = new HttpHeaders(); final MultiValueMap<String, String> cookieParams = new LinkedMultiValueMap<String, String>(); final MultiValueMap formParams = new LinkedMultiValueMap(); if (contentType != null) headerParams.add("Content-Type", apiClient.parameterToString(contentType)); final String[] accepts = { "application/json" }; final List<MediaType> accept = apiClient.selectHeaderAccept(accepts); final String[] contentTypes = { "application/json" }; final MediaType contentType = apiClient.selectHeaderContentType(contentTypes); String[] authNames = new String[] { }; ParameterizedTypeReference<Void> returnType = new ParameterizedTypeReference<Void>() {}; return apiClient.invokeAPI(path, HttpMethod.POST, queryParams, postBody, headerParams, cookieParams, formParams, accept, contentType, authNames, returnType); }
已尝试添加<localVarPrefix>localVar</localVarPrefix>配置项,但问题未解决。
解决方案
方案1:移除Schema中手动定义的Content-Type header参数
你的OpenAPI Schema已经通过requestBody指定了application/json的请求内容类型,OpenAPI Generator会自动处理Content-Type请求头,无需手动在parameters里添加该参数。移除后生成的代码不会出现重名问题:
# 修改后的paths部分 paths: /controller/user/login: post: tags: - default summary: Login requestBody: content: application/json: schema: type: object example: email: user password: user from: user responses: '200': description: Successful response content: application/json: {}
方案2:修改自定义Content-Type参数的名称
如果业务必须保留自定义的Content-Type头(比如支持非标准内容类型),可以修改参数名称避免冲突,例如改成X-Content-Type:
parameters: - name: X-Content-Type in: header schema: type: string example: application/json
方案3:升级OpenAPI Generator版本
你使用的4.3.1版本较老旧,该变量重名bug在后续版本(如5.x及以上)已修复。升级插件到最新稳定版即可解决问题:
<version>6.6.0</version> <!-- 替换为最新稳定版 -->
方案4:自定义代码模板(进阶)
若上述方案均不适用,可自定义Java代码模板,修改局部变量contentType的命名。找到OpenAPI Generator的Java模板文件(api.mustache),将局部变量名改为selectedContentType或其他不冲突名称,再通过插件配置指定自定义模板路径。
内容的提问来源于stack exchange,提问作者Peter
相关产品推荐
相关产品推荐

