openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[JAVA][SPRING] Bug generating multipart file array for RestAssured library

Open ajware-uscis opened this issue 4 years ago • 1 comments

Description

When generating the Api class for RestAssured and creating methods for composing and adding data to the RequestSpecBuilder, the generated method for adding a list of files (List<java.io.File>) to the reqSpec creates code that is unable to compile.

The generated code tries to pass the list directly to reqSpec.addMultiPart(java.io.File file) which only accepts a single file.

openapi-generator version

4.3.1

OpenAPI declaration file content or url
openapi: 3.0.1
info:
  title: foo-api
  description: RESTful API exposing services used for foo
  version: 1.0.0
paths:
  /foo/upload:
    post:
      operationId: uploadFooFiles
      requestBody:
        required: true
        content:
          multipart/form-data:
            schema:
              properties:
                files:
                  type: array
                  items:
                    type: string
                    format: binary
      responses:
        200:
          description: Success
          content:
            application/json:
              schema:
                type: object
                properties:
                  code:
                    type: integer
                  message:
                    type: string
Command line used for generation

Java version: 11.0.7-amzn Gradle version: 6.8.3

build.gradle:

plugins {
    id "org.openapi.generator" version "4.3.1"
    id "java"
}

dependencies {
    implementation "io.springfox:springfox-swagger2:2.9.0"
    implementation "org.openapitools:jackson-databind-nullable:0.2.1"
    implementation "io.rest-assured:rest-assured:3.1.0"
}

openApiGenerate {
    generatorName = "java"
    inputSpec = "$swaggerSourceFile"
    outputDir = "$swaggerOutputDir"
    configFile = "$swaggerConfigFile"
    configOptions = [
        dateLibrary: "java8"
    ]
    library = "rest-assured"
}

CLI: gradle :openApiGenerate or ./gradlew :openApiGenerate

Steps to reproduce
  • Create a Java Spring project with OAS generator
  • Add OAS spec attached above
  • Add build.gradle file with openApiGenerate task as pasted above
  • Call CLI command above: gradle :openApiGenerate
  • Inspect the bug in generate RestAssured Api class (method name: filesMultiPart)
Related issues/PRs
Suggest a fix/enhancement

Generated method filesMultiPart should iterate over the list of files, adding each individually:

public UploadFooFilesOper filesMultiPart(List<File> files) {
    for (final File file : files) {
        reqSpec.addMultiPart(file);
    }
    return this;
}

ajware-uscis avatar Jul 19 '21 16:07 ajware-uscis

  • This is still present issue with 6.6.0 version (using java11).

  • Present issue with 7.5.0 version (using java21).