【问题标题】:Duplicate local variable contentType when using swagger codegen使用swagger codegen时重复的局部变量contentType
【发布时间】:2019-08-27 08:29:35
【问题描述】:

我在使用 swagger-codegen 时遇到了问题。我已经自动下载了 swagger 表单 swaggerHub,然后,我使用 swagger-codegen 生成客户端。但是,对于 POST 请求,它需要 Content-Type 作为参数。所以我在编译时收到了一条消息:

方法 validateAddress 中已经定义了变量 contentType

Pom.xml 文件:

<!-- download swagger -->
<plugin>
    <groupId>io.swagger</groupId>
    <artifactId>swaggerhub-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>aec</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>download</goal>
            </goals>
            <configuration>
                <api>Address</api>
                <owner>test</owner>
                <version>2.13.0</version>
                <format>yaml</format>
                <token>test</token>
                <outputFile>${address-service-swagger.file}</outputFile>
            </configuration>
        </execution>
    </executions>
</plugin>
<!-- generate -->

<plugin>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <executions>
         <execution>
            <id>address-service-client</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <ignoreFileOverride>${project.basedir}/.swagger-codegen-ignore</ignoreFileOverride>
                <inputSpec>${address-service-swagger.file}</inputSpec>
                <language>java</language>
                <modelPackage>com.shipment.client.address.pojo</modelPackage>
                <apiPackage>com.shipment.client.address.api</apiPackage>
                <invokerPackage>com.shipment.client.address.invoker</invokerPackage>
                <configOptions>
                  <generateApis>false</generateApis>
                    <dateLibrary>java8</dateLibrary>
                    <sourceFolder>src/gen/java</sourceFolder>
                </configOptions>
                <library>resttemplate</library>
            </configuration>
        </execution>
   </executions>
 </plugin>

招摇文件:

/addresses/validate:
    post:
      tags:
      - Addresses
      operationId: validateAddress
      description: Validate address
      parameters:
      - name: Authorization
        in: header
        required: true
        type: string
      - name: Content-Type
        in: header
        description: ...
        required: true
        type: string
      - name: addressRequest
        in: body
        description: The address request to validation
        required: true
        schema:
          $ref: "#/definitions/AddressRequest"
...

生成的 API 类:

    public void validateAddress(String authorization, String contentType, AddressRequest addressRequest) throws RestClientException {
        Object postBody = addressRequest;

        // verify the required parameter 'authorization' is set
        if (authorization == null) {
            throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'authorization' when calling validateAddress");
        }

        // verify the required parameter 'contentType' is set
        if (contentType == null) {
            throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'contentType' when calling validateAddress");
        }

        // verify the required parameter 'addressRequest' is set
        if (addressRequest == null) {
            throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'addressRequest' when calling validateAddress");
        }

        String path = UriComponentsBuilder.fromPath("/addresses/validate").build().toUriString();

        final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
        final HttpHeaders headerParams = new HttpHeaders();
        final MultiValueMap<String, Object> formParams = new LinkedMultiValueMap<String, Object>();

        if (authorization != null)
        headerParams.add("Authorization", apiClient.parameterToString(authorization));
        if (contentType != null)
        headerParams.add("Content-Type", apiClient.parameterToString(contentType));

        final String[] accepts = { };
        final List<MediaType> accept = apiClient.selectHeaderAccept(accepts);
        final String[] contentTypes = { };
        final MediaType contentType = apiClient.selectHeaderContentType(contentTypes);

        String[] authNames = new String[] {  };

        ParameterizedTypeReference<Void> returnType = new ParameterizedTypeReference<Void>() {};
        apiClient.invokeAPI(path, HttpMethod.POST, queryParams, postBody, headerParams, formParams, accept, contentType, authNames, returnType);
    }

如何解决这个问题?谢谢!

【问题讨论】:

    标签: spring-boot swagger swagger-codegen


    【解决方案1】:

    发生这种情况是因为局部变量名称与生成代码中的参数名称冲突。将&lt;localVarPrefix&gt; 添加到&lt;configOptions&gt; 可以避免这种冲突。

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

    【讨论】:

      猜你喜欢
      • 2020-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-13
      • 2015-08-09
      • 1970-01-01
      • 1970-01-01
      • 2013-08-16
      相关资源
      最近更新 更多