Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix FormData clone boundary inconsistency issue #2305

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions dio/lib/src/form_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@ String get _nextRandomId =>

/// A class to create readable "multipart/form-data" streams.
/// It can be used to submit forms and file uploads to http server.
///
/// Note: When initBoundary is specified, it will override the boundaryName configuration.
/// This is because the actual boundary is composed of boundaryName plus a random string.
class FormData {
FormData({
String? initBoundary,
this.boundaryName = _boundaryName,
this.camelCaseContentDisposition = false,
}) {
_init();
_init(initBoundary: initBoundary);
}

/// Create [FormData] from a [Map].
Expand All @@ -45,11 +49,16 @@ class FormData {
final bool camelCaseContentDisposition;

void _init({
String? initBoundary,
Map<String, dynamic>? fromMap,
ListFormat listFormat = ListFormat.multi,
}) {
// Get an unique boundary for the instance.
_boundary = '$boundaryName-$_nextRandomId';
if(initBoundary != null) {
_boundary = initBoundary;
}else{
// Get an unique boundary for the instance.
_boundary = '$boundaryName-$_nextRandomId';
}
if (fromMap != null) {
// Use [encodeMap] to recursively add fields and files.
// TODO(Alex): Write a proper/elegant implementation.
Expand Down Expand Up @@ -209,7 +218,7 @@ class FormData {

// Convenience method to clone finalized FormData when retrying requests.
FormData clone() {
final clone = FormData();
final clone = FormData(initBoundary: _boundary);
clone.fields.addAll(fields);
for (final file in files) {
clone.files.add(MapEntry(file.key, file.value.clone()));
Expand Down
1 change: 1 addition & 0 deletions dio/test/formdata_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ void main() async {
expect(fm1 != fm, true);
expect(fm1.files[0].value.filename, fm.files[0].value.filename);
expect(fm1.fields, fm.fields);
expect(fm1.boundary, fm.boundary);
},
testOn: 'vm',
);
Expand Down
Loading