[BUG] [Dart] MultipartRequest array of int not generating correctly
Bug Report Checklist
- [X] Have you provided a full/minimal spec to reproduce the issue?
- [X] Have you validated the input using an OpenAPI validator (example)?
- [X] Have you tested with the latest master to confirm the issue still exists?
- [X] Have you searched for related issues/PRs?
- [X] What's the actual output vs expected output?
- [ ] [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
I am doing multipartRequest to dotnet backend.
The error i get is:
ApiException 400: {"errors":{"Interests":["The value '[6, 5]' is not valid."]}}
this is whats generated:
/// Parameters:
///
/// * [String] name (required):
///
/// * [String] introduction (required):
///
/// * [String] description (required):
///
/// * [List<int>] interests (required):
///
/// * [MultipartFile] file:
Future<Response> apiPageCreatePostWithHttpInfo(String name, String introduction, String description, List<int> interests, { MultipartFile? file, }) async {
// ignore: prefer_const_declarations
final path = r'/api/page/create';
// ignore: prefer_final_locals
Object? postBody;
final queryParams = <QueryParam>[];
final headerParams = <String, String>{};
final formParams = <String, String>{};
const contentTypes = <String>['multipart/form-data'];
bool hasFields = false;
final mp = MultipartRequest('POST', Uri.parse(path));
if (name != null) {
hasFields = true;
mp.fields[r'Name'] = parameterToString(name);
}
if (introduction != null) {
hasFields = true;
mp.fields[r'Introduction'] = parameterToString(introduction);
}
if (description != null) {
hasFields = true;
mp.fields[r'Description'] = parameterToString(description);
}
if (interests != null) {
hasFields = true;
mp.fields[r'Interests'] = parameterToString(interests); //HERE IS THE PROBLEM
}
if (file != null) {
hasFields = true;
mp.fields[r'File'] = file.field;
mp.files.add(file);
}
if (hasFields) {
postBody = mp;
}
return apiClient.invokeAPI(
path,
'POST',
queryParams,
postBody,
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
);
}
openapi-generator version
6.6.0
OpenAPI declaration file content or url
"/api/page/create": {
"post": {
"tags": ["Page"],
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"required": [
"Description",
"Interests",
"Introduction",
"Name"
],
"type": "object",
"properties": {
"Name": {
"type": "string"
},
"Introduction": {
"type": "string"
},
"Description": {
"type": "string"
},
"Interests": {
"type": "array",
"items": {
"type": "integer",
"format": "int32"
}
},
"File": {
"type": "string",
"format": "binary"
}
}
},
"encoding": {
"Name": {
"style": "form"
},
"Introduction": {
"style": "form"
},
"Description": {
"style": "form"
},
"Interests": {
"style": "form"
},
"File": {
"style": "form"
}
}
}
}
},
"responses": {
"200": {
"description": "Success",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/PageData"
}
}
}
},
"400": {
"description": "Bad Request",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetails"
}
}
}
},
"401": {
"description": "Unauthorized",
"content": {
"application/json": {
"schema": {
"type": "string"
}
}
}
}
},
}
},
Generation Details
openapi-generator-cli generate -g dart -i swagger.json -o ./lib/api/rest --additional-properties=useEnumExtensions=true,ensureUniqueParams=false
Steps to reproduce
- build
- try to call it
Suggest a fix
this could probably work?
if (interests != null) {
hasFields = true;
final interestsString = interests.map((e) => e.toString()).join(',');
mp.fields[r'Interests'] = parameterToString(interestsString);
}
@yissachar @joernahrens could you please take a look at this? i stumbled upon this again with array of files.
@yissachar @joernahrens This became an issue for me with array of files. My specs looked like
properties:
caption:
type: string
media-files:
description: The list of image/video binary data to upload as part of the new post. May be empty
type: array
maxItems: 10
items:
type: string
format: binary
That was generating this code:
Future<Response> postsUsernameNewPostWithHttpInfo(String username, String caption, List<MultipartFile> mediaFiles,) async {
...
if (mediaFiles != null) {
hasFields = true;
mp.fields[r'media-files'] = mediaFiles.field; // The getter 'field' isn't defined for the type 'List<MultipartFile>'
mp.files.add(mediaFiles); // The argument type 'List<MultipartFile>' can't be assigned to the parameter type 'MultipartFile'.
}
...
I had to change it to have it support it:
required:
- caption
- num-media-files
properties:
caption:
type: string
num-media-files:
type: integer
minimum: 0
maximum: 10
# Why field names and not just an array of media files?
# Because the dart openapi generator doesn't recognize arrays of files properly
0:
description: The first image/video binary data to upload as part of the new post. Only accessed if num-media-files >=1.
type: string
format: binary
1:
... // copy-paste for 1 through 9
any updates for this? @ahmednfwela
I have (maybe) the same problem with this part of the mustache file.
For me i just have to submit a multipart file, the openapi spec look like this:
...
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"type": "object",
"properties": {
"file": {
"type": "string",
"format": "binary"
}
},
"required": [
"file"
]
}
}
}
},
...
And the generated code is:
Future<Response> addMediaWithHttpInfo(MultipartFile file, { String? container, String? storage, }) async {
// ignore: prefer_const_declarations
final path = r'/v2/media';
// ignore: prefer_final_locals
Object? postBody;
final queryParams = <QueryParam>[];
final headerParams = <String, String>{};
final formParams = <String, String>{};
if (container != null) {
queryParams.addAll(_queryParams('', 'container', container));
}
if (storage != null) {
queryParams.addAll(_queryParams('', 'storage', storage));
}
const contentTypes = <String>['multipart/form-data'];
bool hasFields = false;
final mp = MultipartRequest('POST', Uri.parse(path));
if (file != null) {
hasFields = true;
mp.fields[r'file'] = file.field;
mp.files.add(file);
}
if (hasFields) {
postBody = mp;
}
return apiClient.invokeAPI(
path,
'POST',
queryParams,
postBody,
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
);
}
I have a parameter MultipartFile where i set the field name "file", but the line mp.fields[r'file'] = file.field; makes another multipart field with the same name file, but only with the content "file". Now the backend has two fields of file