Open balzmo opened 5 years ago
this is still a bug in version 4.2.2 of the generator and multiple files (specified as an array of binary strings) don't produce a list of multiPartFiles. Swagger generator does a better job generating a list of Resource objects, respecting the "required" indications and using the names provided in the yaml for the parts
Hello,
I had the exact same problem, but had the particularity to use a forked version of openapi-generator. We switched to the official version 4.3.1 and it looks ok to me :
requestBody:
required: true
content:
multipart/form-data:
schema:
type: object
properties:
myObject:
$ref: '#/components/schemas/MyObject'
file:
type: string
format: binary
required:
- myObject
- file
The corresponding java generation :
@RequestMapping(value = "/clients/{client_id}/accounts/{account_id}/myEndpoint",
produces = { "application/json" },
consumes = { "multipart/form-data" },
method = RequestMethod.POST)
ResponseEntity<DossierGestionReponse> myEndpoint(
@ApiParam(value = "Client id",required=true) @PathVariable("client_id") String clientId,
@ApiParam(value = "Account id",required=true) @PathVariable("account_id") String accountId,
@ApiParam(value = "", required=true, defaultValue="null") @RequestPart(value="myObject", required=true) MyObject myObject,
@ApiParam(value = "") @Valid @RequestPart(value = "file") MultipartFile file);
We also struggled to test this with postman. we need to specify explicitely the content type of "myObject" like this (Otherwise we get an error 415) : And do the same in our tests :
// Given
MockPart fileMock = new MockPart("file", "file", "test data".getBytes(StandardCharsets.UTF_8));
MockPart myObjectMock = new MockPart("myObject", "myObject", objectMapper.writeValueAsString(myObject).getBytes(StandardCharsets.UTF_8));
myObjectMock.getHeaders().setContentType(MediaType.APPLICATION_JSON); // Set the content type explicitely in form-data for json objects
//WHEN
MvcResult mvcResult = this.mockMvc.perform(
multipart("/clients/" + DEFAULT_CLIENT_ID + "/accounts/" + DEFAULT_ACCOUNT_ID + "/myEndpoint")
.part(fileMock)
.part(myObjectMock))
.andDo(print())
.andExpect(status().isOk())
.andReturn();
Hope this helps
Confirmed this is still an issue as of swagger generator 3.0.29 with "spring" language and latest supported spec of "openapi: 3.0.3"
the following:
requestBody:
content:
multipart/form-data:
schema:
type: object
properties:
someValue:
type: string
required: true
file:
description: 'single file binary for upload.'
type: string
format: binary
is generated as: 1 @requestParam and 1 @requestPart. when it should have been: 2 @requestPart (making that change makes it work for the swagger-UI) Also: unrelated but "required" is still ignored.
details:
@Parameter(in = ParameterIn.DEFAULT, description = "",schema=@Schema())
@RequestParam(value="someValue", required=false)
String someValue,
@Parameter(description = "file detail") @Valid @RequestPart("file") MultipartFile file) {
Is there a plan to fix this. The only workaround as of now is to specify string instead of the complex object type to avoid type conversion
It looks like, this bug is back:
required: true
content:
multipart/form-data:
schema:
type: object
properties:
uploadtyp:
$ref: '#/components/schemas/UploadTyp'
files:
type: array
items:
type: string
format: binary
required:
- uploadtyp
- files
With openapi-generator-maven-plugin Version 5.4.0 it renders correctly both uploadtyp and files as RequestPart. But in Version 6.0.1 and 6.1.0 it renders uploadtyp as RequestParam and files as RequestPart.
I've independantly verified: on 6.1.0. The binary file is the only parameter that receives a @RequestPart. All other properties become @RequestParam. Now, it will "work", but it doesn't do what the spec promises ( multipart/form-data, you'd expect the other form-data to be in the other 'parts')
6.1.0:
requestBody:
content:
multipart/form-data:
schema:
type: object
properties:
test:
type: string
test2:
$ref: '#/components/schemas/AddOnOption'
assetFile:
type: string
format: binary
required:
- assetFile
generates:
@RequestParam(value = "test", required = false) String test,
@RequestParam(value = "test2", required = false) AddOnOption test2,
@RequestPart(value = "assetFile", required = true) MultipartFile assetFile
when expected is: @RequestPart(value = "test", required = false) String test, @RequestPart(value = "test2", required = false) AddOnOption test2, @RequestPart(value = "assetFile", required = true) MultipartFile assetFile
It looks like, this bug is back:
Yep, there's a regression issue for that: https://github.com/OpenAPITools/openapi-generator/issues/12498
Description
When generating multipart interfaces, the interface code is not working for Spring with Jersey:
Actual result:
This code line leads to the following error on invocation:
Expected result:
openapi-generator version
3.3.4
OpenAPI declaration file content or url
Example interface extract, full example see attached:
Command line used for generation
Maven code:
Steps to reproduce
Execute code generation via Maven on example project attached. oagen-fileupload-spring-demo.zip
Send the following request:
Note: The example pom.xml contains a workaround for replacing the concerned code line with the correct code.
Suggest a fix