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.