Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[spring] @RequestPart should use "{{basename}}" instead of "file" and observe "required" attribute #9250

Closed
6d77 opened this issue Mar 8, 2019 · 5 comments · Fixed by #9490

Comments

@6d77
Copy link

6d77 commented Mar 8, 2019

Description

When generating a spring-boot server for a POST processing multipart form data to upload one or several files, then the generated @RequestPart annotation always uses the fixed string "file" instead of the name of the part. Additionally, the required attribute is not considerd when building the @RequestPart annotation, so that file upload parameters are always required even if not defined to be so in the Swagger yaml file.

Swagger-codegen version

swagger-codegen-cli-2.4.2.jar

Swagger declaration file content or url

If you post the code inline, please wrap it with

  /kpack/jsonld:
    post:
      operationId: "uploadJsonld"
      consumes:
        - multipart/form-data
      produces:
        - application/json
      parameters:
        - name: jsonld
          in: formData
          type: file
          description: "JSON-LD graph file"
          required: true
Command line used for generation
java -jar ./swagger-codegen-cli.jar generate \
	-i api.yaml \
	-l spring 
Steps to reproduce

Create code with command given above. Check generated Api and Controller.

Related issues/PRs
Suggest a fix/enhancement

In https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/resources/JavaSpring/formParams.mustache change the fixed "file" and replace it by "{{basename}}". Additionally fill the "required" attribute of this annotation.

@6d77 6d77 changed the title @RequestPart should use "{{basename}}" instead of "file" [spring] @RequestPart should use "{{basename}}" instead of "file" Mar 8, 2019
@6d77 6d77 changed the title [spring] @RequestPart should use "{{basename}}" instead of "file" [spring] @RequestPart should use "{{basename}}" instead of "file" and observe "required" attribute Mar 8, 2019
@MGoyal92
Copy link
Contributor

Changing formParams.mustache to the below seems to fix the issue:

{{#isFormParam}}{{#notFile}}@ApiParam(value = "{{{description}}}"{{#required}}, required=true{{/required}}{{#allowableValues}}, allowableValues="{{#values}}{{{.}}}{{^-last}}, {{/-last}}{{#-last}}{{/-last}}{{/values}}"{{/allowableValues}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}}) @RequestParam(value="{{baseName}}"{{#required}}, required=true{{/required}}{{^required}}, required=false{{/required}}) {{{dataType}}} {{paramName}}{{/notFile}}{{#isFile}}@ApiParam(value = "{{{description}}}") {{#useBeanValidation}}@Valid{{/useBeanValidation}} @RequestPart(value="{{baseName}}"{{#required}}, required=true{{/required}}{{^required}}, required=false{{/required}}) MultipartFile {{baseName}}{{/isFile}}{{/isFormParam}}

@frantuma
Copy link
Member

thanks for reporting this! fixed in #9490

@mhaarika
Copy link

mhaarika commented Apr 27, 2021

Which version of code gen is this fixed

@MGoyal92
Copy link
Contributor

MGoyal92 commented May 5, 2021

Which version of code gen is this fixed

I'm not sure about the version, but it was merged 1.5 years back. You can find the details under #9490
"frantuma merged commit b028473 into swagger-api:master on Nov 21, 2019"

@mladenlukic
Copy link

This is still happening with version 3.0.35

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants