Phương Thảo
Phương Thảo

Reputation: 27

Duplicate local variable contentType when using swagger codegen

I've an issue when using swagger-codegen. I've downloaded swagger form swaggerHub automatically, and then, I used swagger-codegen to generate client. However, With the POST request, it's required Content-Type as a parameter. So I've got a message when compile:

variable contentType is already defined in method validateAddress

Pom.xml file:

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

swagger file:

/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 class generated:

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

How to solve this problem? Thanks!

Upvotes: 2

Views: 1547

Answers (1)

Yury Buzenets
Yury Buzenets

Reputation: 11

It happens because a local variable name conflicts with a parameter name in the generated code. Adding <localVarPrefix> to <configOptions> allows you to avoid that conflict.

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

Upvotes: 1

Related Questions