java 变量contentType已在openapi生成的源代码方法中定义

mwecs4sa  于 2023-03-28  发布在  Java
关注(0)|答案(1)|浏览(78)

我有下面打开的API模式文件

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: {}

我试图生成java客户端使用下面的pom配置

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

当我试图生成客户端使用mvn清洁安装我得到下面的错误

DefaultApi.java:[91,25] variable contentType is already defined in method controllerUserLoginPostWithHttpInfo(java.
lang.String,java.lang.Object)

在www.example.com中DefaultApi.java,我看到生成的类包含两个同名的变量

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);
    }

我也尝试了配置选项,但它不做任何更改

<configOptions>
    ...
    <localVarPrefix>localVar</localVarPrefix>
</configOptions>
kq0g1dla

kq0g1dla1#

您可以尝试将OpenAPI模式文件中的contentType变量名称更改为其他名称,例如myContentType。这应该会生成没有任何编译错误的代码。

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: my-content-type
          in: header
          schema:
            type: string
          example: application/json
      responses:
        '200':
          description: Successful response
          content:
            application/json: {}

看起来你已经尝试在生成的代码中更改局部变量的前缀了。据我所知,设置localVar应该可以为所有局部变量添加前缀。

相关问题