Skip to content

Commit

Permalink
[#48] fixed multipart/form-data body serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
ganigeorgiev committed Sep 9, 2023
1 parent 140205d commit 74f03fd
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.15.1

- Fixed `multipart/form-data` body serialization when `null` values are submitted ([#48](https://github.com/pocketbase/dart-sdk/issues/48)).


## 0.15.0

- Added `pb.backups.upload(file)` action (_available with PocketBase v0.18.0_).
Expand Down
23 changes: 20 additions & 3 deletions lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,29 @@ class PocketBase {

body.forEach((key, value) {
final entries = <String>[];
// @todo consider adding a note in the docs that for `json` fields
// the value may need to be `jsonEncode()`-ed
// (and more specifically for null and <String>[])
if (value is Iterable) {
for (final v in value) {
entries.add(v.toString());
try {
final casted = value.cast<String>();
if (casted.isEmpty) {
// empty list -> resolve as empty entry
entries.add("");
} else {
// strings lists -> add each item as new entry
for (final v in casted) {
entries.add(v.toString());
}
}
} catch (_) {
// non-strings lists -> json encode
entries.add(jsonEncode(value));
}
} else if (value is Map) {
entries.add(jsonEncode(value));
} else {
entries.add(value.toString());
entries.add(value?.toString() ?? "");
}

request.fields[key] = entries;
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: pocketbase
description: Multi-platform Dart SDK for PocketBase, an open source realtime backend in 1 file.
repository: https://github.com/pocketbase/dart-sdk

version: 0.15.0
version: 0.15.1

environment:
sdk: '>=3.0.0 <4.0.0'
Expand Down
54 changes: 51 additions & 3 deletions test/client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,48 @@ void main() {
final mock = MockClient((request) async {
expect(request.method, "POST");
expect(request.url.toString(), "/base/test?a=1&a=2&c=3");
expect(request.body, contains("--dart-http-boundar"));
expect(request.body, contains("--dart-http-boundary"));
expect(
request.body,
contains('content-disposition: form-data; name="test_body"'),
contains('content-disposition: form-data; name="a"\r\n\r\n123\r\n'),
);
expect(
request.body,
contains('content-disposition: form-data; name="b1"\r\n\r\n1\r\n'),
);
expect(
request.body,
contains('content-disposition: form-data; name="b1"\r\n\r\n2\r\n'),
);
expect(
request.body,
contains(
'content-disposition: form-data; name="b2"\r\ncontent-type: text/plain; charset=utf-8\r\ncontent-transfer-encoding: binary\r\n\r\n\r\n',
),
);
expect(
request.body,
contains(
'content-disposition: form-data; name="c1"\r\n\r\n[1,2]\r\n',
),
);
expect(
request.body,
contains(
'content-disposition: form-data; name="c2"\r\ncontent-type: text/plain; charset=utf-8\r\ncontent-transfer-encoding: binary\r\n\r\n\r\n',
),
);
expect(
request.body,
contains(
'content-disposition: form-data; name="d"\r\ncontent-type: text/plain; charset=utf-8\r\ncontent-transfer-encoding: binary\r\n\r\n\r\n',
),
);
expect(
request.body,
contains(
'content-disposition: form-data; name="e"\r\n\r\n{"test":123}\r\n',
),
);
expect(
request.body,
Expand All @@ -164,6 +202,8 @@ void main() {
contains("multipart/form-data; boundary=dart-http-boundary-"),
);

//final bodyExpectations = <String>[]

return http.Response("", 200);
});

Expand All @@ -181,7 +221,15 @@ void main() {
"b": null,
"c": 3,
},
body: {"test_body": 123},
body: {
"a": 123,
"b1": ["1", "2"],
"b2": <String>[],
"c1": [1, 2],
"c2": <dynamic>[],
"d": null,
"e": <String, dynamic>{"test": 123},
},
files: [http.MultipartFile.fromString("test_file", "123")],
headers: {"test_header": "123"},
);
Expand Down

0 comments on commit 74f03fd

Please sign in to comment.