swagger-api / swagger-api/swagger-codegen

[Java] Generated methods for multipart/form-data requestBody missing body parameter

Open
#8,346 10 comments 9 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

Generating Java code for a YAML file that contains a multipart/form requestBody results in a method that lacks any parameters to pass values in. As such there's no way to upload data. The yaml file I included isn't what I'm using, but it exhibits the same problem. The multiform data part came from https://swagger.io/docs/specification/describing-request-body/multipart-requests/

I can add a parameter section that duplicates the requestBody section and the code generated will take in said parameters, but parameters are not used to add form data for post, put and paste operations.

For my real YAML file, https://editor.swagger.io/ generates code that takes a "body" object parameter in the post methods. The included yaml file won't generate code for some reason, though it's listed as valid. The body object isn't correct though as the values need to go in the localVarFormParams variable and that's not happening.

Swagger-codegen version

3.0.0-SNAPSHOT (nightly)

Swagger declaration file content or url
openapi: "3.0.1"
info:
  version: 1.0.0
  title: Test
paths:
  /upload:
    post:
      summary: Upload a file with info
      operationId: uploadImage
      requestBody:
        content: 
          multipart/form-data: # Media type
            schema: # Request payload
              type: object
              properties: # Request parts
                id: # Part 1 (string value)
                  type: string
                  format: uuid
                address: # Part2 (object)
                  type: object
                  properties:
                    street:
                      type: string
                    city:
                      type: string
                profileImage: # Part 3 (an image)
                  type: string
                  format: binary
      responses:
        '200':
            description: Response
Command line used for generation

java -jar swagger-codegen-cli.jar generate -l java -i test.yml -o code

this generates the following methods in DefaultApi.java which don't take any parameters.

/**
 * Upload a file with info
 * 
 * @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
     */
    public void uploadImage() throws ApiException {
        uploadImageWithHttpInfo();
    }

    /**
     * Upload a file with info
     * 
     * @return ApiResponse<Void>
     * @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
        
     */
    public ApiResponse<Void> uploadImageWithHttpInfo() throws ApiException {
        com.squareup.okhttp.Call call = uploadImageValidateBeforeCall(null, null);
        return apiClient.execute(call);
    }

Steps to reproduce

Simply take the test.yaml file and generate java code.

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

Reproduce the issue with the included test.yaml using the swagger-codegen CLI and inspect the generated DefaultApi.java methods. Trace how the multipart requestBody is converted into parameters and localVarFormParams, then verify that generated uploadImage methods accept the form values and pass them to the request.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.