swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

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

Open Morac2 opened this issue 8 years ago • 10 comments

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.

Morac2 avatar Jun 22 '18 16:06 Morac2

If I replace multipart/form-data with any other type in the yaml file, the code will add a body parameter, but that won't work since it needs to be form data.

Morac2 avatar Jun 22 '18 16:06 Morac2

I checked and using "application/x-www-form-urlencoded" also doesn't work.

Morac2 avatar Jun 22 '18 16:06 Morac2

Hello, Did you find a solution or workaround for this? Thanks;

rizoom avatar Jan 11 '19 18:01 rizoom

I have the same problem.

marvin-w avatar Apr 24 '19 21:04 marvin-w

+1

Splash34 avatar Feb 07 '20 12:02 Splash34

Faced the same issue. Any chance it will be fixed?

evilchewits avatar Dec 29 '20 23:12 evilchewits

This is a serious issue that has been open for over a year - any timeline on a fix?

Cloudmersive avatar Oct 13 '21 01:10 Cloudmersive

It is now open for over 3 years ... and I also have this issue

downdrown avatar Jan 12 '22 12:01 downdrown

I am facing the same issue, my GAV details:

<groupId>io.swagger.codegen.v3</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>3.0.32</version>

arvindkrishnakumar-okta avatar Feb 10 '22 17:02 arvindkrishnakumar-okta

seems to be fixed in SwaggerHub On-Premise 2.1.0 v3.0.34

delp-gsharma avatar Feb 11 '23 06:02 delp-gsharma