Skip to content

[JAX-RS] Codegen Creates form-data variables with duplicate names #3300

@jencodingatwork

Description

@jencodingatwork
Description

The JAX-RS Code generator creates variables with duplicate names when more than one file is included in the API call which results in a compilation error.

Swagger-codegen version

2.1.6

Swagger declaration file content or url
paths:
  /Plugin:
    post:
      tags:
        - Plugin
      summary: Upload Plugin
      description: This can only be done by the logged in user.
      operationId: createPlugin
      consumes:
      - "multipart/form-data"
      produces:
        - application/json
        - application/xml
      parameters:
        - in: formData
          name: json
          description: Uploaded Plugin
          required: true
          type: string
        - in: formData
          name: jarFile
          description: Jar File to upload
          required: true
          type: file
        - in: formData
          name: configFile
          description: config File to upload
          required: true
          type: file
      responses:
        200:
          description: successful operation
Command line used for generation

java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate -i C:/PluginAPI/plugin.yaml -l jaxrs -o C:/Temp/JAX_RStest java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate -i C:/PluginAPI/plugin.yaml -l jaxrs -o C:/Temp/JAX_RStest

Steps to reproduce
  1. Execute command
  2. Look at Plugin API code, where you should see the following method. Notice that there are two variables named inputStream, two variables named fileDetail, and all params are set to “file”.
@POST 
    @Produces({ "application/json", "application/xml"})
    @Consumes({"multipart/form-data"})
    @io.swagger.annotations.ApiOperation(value = "Upload Plugin", notes = "This can only be done by the logged in user.", response = void.class, tags={ "Plugin", })
    @io.swagger.annotations.ApiResponses(value = { 
        @io.swagger.annotations.ApiResponse(code = 200, message = "successful operation", response = void.class) })
    public Response createAnalytic(@ApiParam(value = "Uploaded Plugin", required=true)@FormParam("json")  String json,
            @FormDataParam("file") InputStream inputStream,
            @FormDataParam("file") FormDataContentDisposition fileDetail,
            @FormDataParam("file") InputStream inputStream,
            @FormDataParam("file") FormDataContentDisposition fileDetail,@Context SecurityContext securityContext)
    throws NotFoundException {
        return delegate.createAnalytic(json, inputStream, fileDetail, inputStream, fileDetail, securityContext);
    }
Related issues

I could not find any related issues.

Suggest a Fix

My fix to this was to modify three mustache files so that the hardcoded variable name also includes the paramName resulting in the following code:

@POST 
    @Produces({ "application/json", "application/xml"})
    @Consumes({"multipart/form-data"})
    @io.swagger.annotations.ApiOperation(value = "Upload Plugin", notes = "This can only be done by the logged in user.", response = void.class, tags={ "Plugin", })
    @io.swagger.annotations.ApiResponses(value = { 
        @io.swagger.annotations.ApiResponse(code = 200, message = "successful operation", response = void.class) })
    public Response createAnalytic(@ApiParam(value = "Uploaded Plugin", required=true)@FormParam("json")  String json,
            @FormDataParam("jarFile") InputStream jarFileinputStream,
            @FormDataParam("jarFile") FormDataContentDisposition jarFileDetail,
            @FormDataParam("configFile") InputStream configFileinputStream,
            @FormDataParam("configFile") FormDataContentDisposition configFileDetail,@Context SecurityContext securityContext)
    throws NotFoundException {
        return delegate.createAnalytic(json,jarFileinputStream, jarFileDetail,configFileinputStream, configFileDetail,securityContext);
    }

Line 79 of the api.mustache file should be modified to become:

return delegate.{{nickname}}({{#allParams}}{{#isFile}}{{paramName}}inputStream, {{paramName}}Detail{{/isFile}}{{^isFile}}{{paramName}}{{/isFile}},{{/allParams}}securityContext);

The formParams.mustache file should be updated to become:

{{#isFormParam}}{{#notFile}}@ApiParam(value = "{{{description}}}"{{#required}}, required=true{{/required}}{{#allowableValues}}, {{> allowableValues }}{{/allowableValues}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}}){{#vendorExtensions.x-multipart}}@FormDataParam("{{paramName}}")  {{{dataType}}} {{paramName}}{{/vendorExtensions.x-multipart}}{{^vendorExtensions.x-multipart}}@FormParam("{{paramName}}")  {{{dataType}}} {{paramName}}{{/vendorExtensions.x-multipart}}{{/notFile}}{{#isFile}}
            @FormDataParam("{{paramName}}") InputStream {{paramName}}inputStream,
            @FormDataParam("{{paramName}}") FormDataContentDisposition {{paramName}}Detail{{/isFile}}{{/isFormParam}}

The serviceFormParams.mustache file should be updated to become:

{{#isFormParam}}{{#notFile}}{{{dataType}}} {{paramName}}{{/notFile}}{{#isFile}}InputStream {{paramName}}inputStream, FormDataContentDisposition {{paramName}}fileDetail{{/isFile}}{{/isFormParam}}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions