OpenAPITools / OpenAPITools/openapi-generator

[BUG] multipart/form-data with <useAbstractionForFiles>true</useAbstractionForFiles> not working

Open
#13,340 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Hey OpenAPI-Team,

thanks for your great Work supporting the Community to build better APIs!

I have Problem with my OAS3 Client-Server-Application.

Description

OpenAPI Client Generator (java/resttemplate) is generating non working Client if the following configOption is set:

 <useAbstractionForFiles>true</useAbstractionForFiles>
openapi-generator version

6.0.1

OpenAPI declaration file content or url
      requestBody:
        content:
          multipart/form-data:
            schema:
              required:
                - environmentId
                - folderPath
                - file
              type: object
              properties:
                environmentId:
                  type: string
                folderPath:
                  type: string
                fileName:
                  type: string
                file:
                  type: string
                  format: binary
Generation Details

I use generator spring for the backend with spring-boot library.
The API works as expected if i use swagger generated curl.

curl -X 'POST' \
  'http://127.0.0.1:8080/upload' \
  -H 'accept: application/json' \
  -H 'Content-Type: multipart/form-data' \
  -F 'environmentId=dev2' \
  -F 'folderPath=/test-vkad2506 ' \
  -F 'fileName=newFileName.png' \
  -F 'file=@oldFileName.png;type=image/png'

If i generate Client with generator java, and resttemplate library.

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>6.0.1</version>
    <executions>
        <execution>
            <id>contract-client-${api-contract1.name}</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>${api-contract1.folder}/${api-contract1.path}</inputSpec>
                <generatorName>java</generatorName>
                <generateApiTests>false</generateApiTests>
                <generateModelTests>false</generateModelTests>
                <library>resttemplate</library>
                <configOptions>
                    <sourceFolder>${api-contract1.name}</sourceFolder>
                    <dateLibrary>java8</dateLibrary>
                    <apiPackage>${generated.package.root}.${api-contract1.name}.${generated.package.api}</apiPackage>
                    <modelPackage>${generated.package.root}.${api-contract1.name}.${generated.package.model}</modelPackage>
                    <invokerPackage>${generated.package.root}.${api-contract1.name}.${generated.package.invoker}</invokerPackage>
                </configOptions>
            </configuration>
        </execution>                   
    </executions>
</plugin>

The generator creates a working API-Client with File Object _file in method signature:

public ResponseV1 upload(String environmentId, String folderPath, File _file, String fileName)

But as i do want to use abstraction for File, to use Resources in API-Client i added configOption:

<configOptions>
   ...
    <useAbstractionForFiles>true</useAbstractionForFiles>
   ...
</configOptions>

This changes the API-Clients method Signature from File to Resource.

public ResponseV1 upload(String environmentId, String folderPath, org.springframework.core.io.Resource _file, String fileName)

But if i call the Client, the Server is throwing the following Error:

org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'file' is not present

Can you help here please?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the generated Java RestTemplate client method shown for the multipart upload and compare its File and Resource variants using the supplied OpenAPI declaration. Reproduce the request against the Spring Boot endpoint and inspect why the Resource call omits the multipart part named file; done when the generated Resource client is accepted by the server.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring, spring-boot
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.