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

[Spring]the `required` property of type `file` not work in spring server code

Open mumutu opened this issue 8 years ago • 3 comments

in the petstore.json, the file is marked as required false

...
"/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"
            ]
          }
        ]
      }
...

in swagger-ui this file property is optional, but in the generated spring server code


@ApiOperation(value = "uploads an image", notes = "", response = ModelApiResponse.class, authorizations = {
        @Authorization(value = "petstore_auth", scopes = {
            @AuthorizationScope(scope = "write:pets", description = "modify pets in your account"),
            @AuthorizationScope(scope = "read:pets", description = "read your pets")
            })
    }, tags={ "pet", })
    @ApiResponses(value = { 
        @ApiResponse(code = 200, message = "successful operation", response = ModelApiResponse.class) })
    @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" ) @RequestPart(value="additionalMetadata", required=false)  String additionalMetadata,
        @ApiParam(value = "file detail") @RequestPart("file") MultipartFile file);

obviously the file is required here..so I wonder if it is a bug

mumutu avatar Apr 16 '17 07:04 mumutu

The bug is in the formParams.mustache its missing the handling of required flag in the "isFile" condition.

Should be changed to:

{{#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 = "file detail") {{#useBeanValidation}}@Valid{{/useBeanValidation}} @RequestPart("file"{{#required}}, required=true{{/required}}{{^required}}, required=false{{/required}}) MultipartFile {{baseName}}{{/isFile}}{{/isFormParam}}

The change is in the last RequestPart after the "file"

@diyfr can you help fixing the template?

rajrangan avatar Dec 07 '18 16:12 rajrangan

I am also facing same issue after upgrading the springfox jar from 2.9.2 to 3.0.0

ajitdas91 avatar Mar 03 '21 14:03 ajitdas91

I am facing similar problem. Why all files are named file, when in MultipartFile upload there could be several files with different names.

I need to handle mutipart upload with following example:

Content-Type: multipart/form-data; boundary=c1kYTeGi3w4vvurkXxIKtHAJM-NfpbCA0FSvj
...

--c1kYTeGi3w4vvurkXxIKtHAJM-NfpbCA0FSvj
Content-Disposition: form-data; name="MetaData#1"; filename="H_442223285642_20231213_130949"
...

--c1kYTeGi3w4vvurkXxIKtHAJM-NfpbCA0FSvj
Content-Disposition: form-data; name="MetaData#2"; filename="H_442223285642_20231213_131636"
...

Is there a way to handle it?

jan-pesta avatar Apr 05 '24 07:04 jan-pesta