Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Merge requests
  • !11449

[JavaSpring] Multipart causing HttpMediaTypeNotSupportedException when parameter is different than File or String

  • Review changes

  • Download
  • Email patches
  • Plain diff
Merged Administrator requested to merge github/fork/redamessoudi/fix_multipart_requestparam_java_gen into master Jan 29, 2022
  • Overview 0
  • Commits 12
  • Pipelines 0
  • Changes 65

Created by: redamessoudi

PR checklist

  • Read the contribution guidelines.
  • Provided a full/minimal spec to reproduce the issue
  • Validated the input using an OpenAPI validator
  • Tested with the latest master to confirm the issue still exists
  • Searched for related issues/PRs
  • Built the project and updated samples

Description

Having an endpoint :

  • Consuming multipart/form-data
  • File as parameter
  • Other parameters different than String (e.g Boolean, Long, Enums, ...)

The generated API crashes HttpMediaTypeNotSupportedException with error Unsupported Media Type and status 415 because of the usage of @RequestPart for parameters different than File or String.

{
  "status": 415,
  "error": "Unsupported Media Type",
  "message": "Content type 'application/octet-stream' not supported"
}

Spring docs explained it here as :

Note that @RequestParam annotation can also be used to associate the part of a "multipart/form-data" request with a method argument supporting the same method argument types.

The main difference is that when the method argument is not a String, @RequestParam relies on type conversion via a registered Converter or PropertyEditor while @RequestPart relies on HttpMessageConverters taking into consideration the 'Content-Type' header of the request part. @RequestParam is likely to be used with name-value form fields while @RequestPart is likely to be used with parts containing more complex content (e.g. JSON, XML).

openapi-generator version

5.X.X and master

OpenAPI example

openapi: '3.0.1'
info:
  version: 1.0.0
  title: MultipartFile test
paths:
  /multipart-mixed:
    post:
      tags:
        - multipart
      description: Mixed MultipartFile test
      operationId: multipartMixed
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              required:
                - status
                - file
              properties:
                status:
                  $ref: '#/components/schemas/MultipartMixedStatus'
                marker:
                  description: "additional object"
                  type: object
                  properties:
                    name:
                      type: string
                file:
                  description: "a file"
                  type: string
                  format: binary
      responses:
        '204':
          description: Successful operation
components:
  schemas:
    MultipartMixedStatus:
      description: "additional field as Enum"
      type: string
      enum:
        - ALLOWED
        - IN_PROGRESS
        - REJECTED
      example: REJECTED

Steps to reproduce

Generate server as a target using JavaSpring

Generated code

default ResponseEntity<Void> multipartMixed(
@ApiParam(value = "", required = true, allowableValues = "ALLOWED, IN_PROGRESS, REJECTED") @Valid 
@RequestPart(value = "status", required = true) MultipartMixedStatus status,
@ApiParam(value = "a file", required = true) 
@RequestPart(value = "file", required = true) MultipartFile file,
@ApiParam(value = "") @Valid 
@RequestPart(value = "marker", required = false) MultipartMixedMarker marker
    ) {
        return getDelegate().multipartMixed(status, file, marker);
    }

Expected code

default ResponseEntity<Void> multipartMixed(
@ApiParam(value = "", required = true, allowableValues = "ALLOWED, IN_PROGRESS, REJECTED") @Valid 
@RequestParam(value = "status", required = true) MultipartMixedStatus status,
@ApiParam(value = "a file", required = true) 
@RequestPart(value = "file", required = true) MultipartFile file,
@ApiParam(value = "") @Valid 
@RequestParam(value = "marker", required = false) MultipartMixedMarker marker
    ) {
        return getDelegate().multipartMixed(status, file, marker);
    }

Solution

In JavaSpring module in formParams.mustache, I switched the parameter type to @RequestParam (instead of @RequestPart) as long as it is not a File.

Assignee
Assign to
Reviewers
Request review from
Time tracking
Source branch: github/fork/redamessoudi/fix_multipart_requestparam_java_gen