OpenAPITools / OpenAPITools/openapi-generator

[BUG] [JAVA] Optional multipart/form-data 'file' parameter

Open
#6,677 2 comments 9 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

Not able to set multipart/form-data 'Multipart' file parameter as optional

openapi-generator version

OpenAPI version used: 4.3.1

OpenAPI declaration file content or url

openAPI YAML snippet:

  /v1/example/{exampleId}:
    post:
      parameters:
      - name: exampleId
        in: path
        required: true
        schema:
          type: string
          format: uuid
          example: '7dcf6a62-3039-40fc-b93d-d4b0d89885e6'
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                file:
                  type: string
                  description: Source file to be uploaded
                  format: binary
                jsonObject:
                  type: string
                  description: JSON object
                fileTags1:
                  type: string
              required:
              - fileTags1
      responses:
        200:
          description: OK
      deprecated: false

openAPI generated Java snippet:

    /**
     * POST /v1/example/{exampleId}
     *
     * @param exampleId  (required)
     * @param file Source file to be uploaded (optional)
     * @param jsonObject JSON object (optional)
     * @param fileTags1  (required)
     * @return OK (status code 200)
     */
    @ApiOperation(value = "", nickname = "v1ExampleExampleIdPost", notes = "", authorizations = {
        @Authorization(value = "bearerAuth")
    }, tags={  })
    @ApiResponses(value = { 
        @ApiResponse(code = 200, message = "OK") })
    @RequestMapping(value = "/v1/example/{exampleId}",
        consumes = { "multipart/form-data" },
        method = RequestMethod.POST)
    default ResponseEntity<Void> v1ExampleExampleIdPost(
      @ApiParam(value = "",required=true) @PathVariable("exampleId") UUID exampleId,
      @ApiParam(value = "Source file to be uploaded") @Valid @RequestPart(value = "file") MultipartFile file,
      @ApiParam(value = "JSON object") @RequestPart(value="jsonObject", required=false)  String jsonObject,
      @ApiParam(value = "") @RequestPart(value="fileTags1", required=true)  String fileTags1) {
        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);

    }

Please suggest any way to make this file optional or is it possible to update the RequestPart tag
with "required=false"

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 supplied OpenAPI YAML and inspect the generated Java method signature for multipart/form-data parameters. Trace the Java generator's handling of optional request-body properties; done when the optional file produces @RequestPart with required=false while the required fileTags1 parameter remains required.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.