OpenAPITools / OpenAPITools/openapi-generator

multipart/form-data parts order not respected

Open
#14,761 0 comments 0 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

I'm using

<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>6.4.0</version>

With

<generatorName>java</generatorName>
<library>jersey3</library>

My openapi file contains

content:
  multipart/form-data:
    schema:
      type: object
      properties:
        info:
          $ref: '#/components/schemas/Info'
        file:
          type: string
          format: binary

The problem comes from the the api file generated

  public ApiResponse<Document> uploadWithHttpInfo(Info info, File _file) throws ApiException {
    Object localVarPostBody = null;
    
    // create path and map variables
    String localVarPath = "/upload";

    // query params
    List<Pair> localVarQueryParams = new ArrayList<Pair>();
    Map<String, String> localVarHeaderParams = new HashMap<String, String>();
    Map<String, String> localVarCookieParams = new HashMap<String, String>();
    Map<String, Object> localVarFormParams = new HashMap<String, Object>();

And more specifically the line

Map<String, Object> localVarFormParams = new HashMap<String, Object>();

Due to the Hashmap, when I'm using the client, the file part is send before the info part.

1 > Content-Type: multipart/form-data
1 > User-Agent: OpenAPI-Generator/5.1.0/java
--Boundary_1_506748835_1676884274746
Content-Type: application/octet-stream
Content-Disposition: form-data; filename="1420494472980263478.temp"; size=1931; name="file"
...
--Boundary_1_506748835_1676884274746
Content-Type: text/plain
Content-Disposition: form-data; name="info"
...
--Boundary_1_506748835_1676884274746--

Making the api responding : first part in multipart must be the info part

I fixed the problem with the maven replacer plugin to force using a LinkedHashMap by replacing
Map<String, Object> localVarFormParams = new HashMap<String, Object>();
with
Map<String, Object> localVarFormParams = new LinkedHashMap<String, Object>();

Now the order is respected and everything is working fine.

Could you include such a fix ?

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 Java generator using the jersey3 library and trace how the generated uploadWithHttpInfo method creates localVarFormParams. Compare the generated multipart request with the OpenAPI property order; done means the info part is emitted before the file part without a post-generation replacement.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.