swagger-api / swagger-api/swagger-codegen

[JavaSpring/spring-cloud]@RequestParms should be used instead of @ RequestPart on file upload

Open
#8,419 0 comments 0 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

In SpringMVC, when do a form post with file upload, @RequestParms should be used instead of @RequestPart , see SpringMVC doc https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestPart.html

But now the generated file used RequestParms which would lead the OpenFeign initial with the wrong Encoder that file can't be transport as a binary

Swagger-codegen version

swagger-codegen-cli-2.3.1.jar

Swagger declaration file content or url

I reproduce with the petstore example.

https://petstore.swagger.io/v2/swagger.yaml

The form upload content is

/pet/{petId}/uploadImage:
    post:
      tags:
      - "pet"
      summary: "uploads an image"
      description: ""
      operationId: "uploadFile"
      consumes:
      - "multipart/form-data"
      produces:
      - "application/json"
      parameters:
      - name: "petId"
        in: "path"
        description: "ID of pet to update"
        required: true
        type: "integer"
        format: "int64"
      - name: "additionalMetadata"
        in: "formData"
        description: "Additional data to pass to server"
        required: false
        type: "string"
      - name: "file"
        in: "formData"
        description: "file to upload"
        required: false
        type: "file"
      responses:
        200:
          description: "successful operation"
          schema:
            $ref: "#/definitions/ApiResponse"
      security:
      - petstore_auth:
        - "write:pets"
        - "read:pets"
Command line used for generation
# download the stable release jar
wget http://central.maven.org/maven2/io/swagger/swagger-codegen-cli/2.3.1/swagger-codegen-cli-2.3.1.jar -O swagger-codegen-cli.jar
# run codegen with petstore
java -jar swagger-codegen-cli.jar generate -i http://petstore.swagger.io/v2/swagger.yaml -o gen -l spring --library spring-cloud
Steps to reproduce

view the generated gen/src/main/java/io/swagger/api/PetApi.java

see

@RequestMapping(value = "/pet/{petId}/uploadImage",
    produces = "application/json",
    consumes = "multipart/form-data",
    method = RequestMethod.POST)
ResponseEntity<ModelApiResponse> uploadFile(@ApiParam(value = "ID of pet to update",required=true) @PathVariable("petId") Long petId,@ApiParam(value = "Additional data to pass to server" ) @RequestParam(value="additionalMetadata", required=false)  String additionalMetadata,@ApiParam(value = "file detail") @RequestParam("file") MultipartFile file);

the @RequestParam("file") lead an encoder bug to the Feign, uploadFile won't work now.

Related issues/PRs

noop.

Suggest a fix/enhancement

In formParams.mustache , replace with

- ...{{#isFile}}@ApiParam(value = "file detail") @RequestParam("{{baseName}}") MultipartFile ...
+ ...{{#isFile}}@ApiParam(value = "file detail") @RequestPart("{{baseName}}") MultipartFile ...

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 modules/swagger-codegen/src/main/resources/JavaSpring/libraries/spring-cloud/formParams.mustache and reproduce generation with the petstore Swagger file and spring-cloud library. Inspect the generated PetApi.java; done means file form parameters generate the annotation needed for binary upload rather than the current annotation that causes the Feign encoder issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend, tooling
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.