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 Multipart for List<int> and List<String> #439

Merged
merged 21 commits into from
Jun 3, 2023

Conversation

techouse
Copy link
Collaborator

@techouse techouse commented May 29, 2023

Adds the ability to do something like

@Post(path: 'multipart_list')
@multipart
Future<Response> postMultipartList({
  @Part('ints') required List<int> ints,
  @Part('doubles') required List<double> doubles,
  @Part('nums') required List<num> nums,
  @Part('strings') required List<String> strings,
});

final Response res = await postMultipartList(
  ints: <int>[1, 2, 3],
  doubles: <double>[1.23, -1.23, 0.0, 0.12324, 3 / 4],
  nums: <num>[1.23443534678, 0.00000000001, -34251, 0.0, 3 / 4],
  strings: <String>['lorem', 'ipsum', 'dolor', '''r237tw78re ei[04o2 ]de[qwlr;,mgrrt9ie0owp[ld;s,a.vfe[plre'q/sd;poeßšđčćž'''],
);

which will produce this payload (cURL form just for brevity)

curl -v -X POST \
	-f 'ints[0]: 1' \
	-f 'ints[1]: 2' \
	-f 'ints[2]: 3' \
	-f 'doubles[0]: 1.23' \
	-f 'doubles[1]: -1.23' \
	-f 'doubles[2]: 0.0' \
	-f 'doubles[3]: 0.12324' \
	-f 'doubles[4]: 0.75' \
	-f 'nums[0]: 1.23443534678' \
	-f 'nums[1]: 0.00000000001' \
	-f 'nums[2]: -34251' \
	-f 'nums[3]: 0.0' \
	-f 'nums[4]: 0.75' \
	-f 'strings[0]: lorem' \
	-f 'strings[1]: ipsum' \
	-f 'strings[2]: dolor' \
	-f "strings[3]: r237tw78re ei[04o2 ]de[qwlr;,mgrrt9ie0owp[ld;s,a.vfe[plre'q/sd;poeßšđčćž" \
	https://foo.dev/multipart_list"

The gist of this is to use bracket notation (field[0], field[1], field[2], ...) to overcome the limitation of MultipartRequest.fields which is a Map<String, String> and as such can not have repeated keys.

This limitation has been discussed before in dart-lang/http#277 and dart-lang/http#24.

The solution I've implemented is exactly the same as suggested here dart-lang/http#277 (comment)


Addresses #77, #316 and #438

@techouse techouse changed the title ✨ multipart List 🐛 fix multipart List data encoding May 29, 2023
@techouse techouse added the bug Something isn't working label May 29, 2023
@codecov
Copy link

codecov bot commented May 29, 2023

Codecov Report

Merging #439 (49e5506) into develop (d824ded) will increase coverage by 0.05%.
The diff coverage is 100.00%.

@@             Coverage Diff             @@
##           develop     #439      +/-   ##
===========================================
+ Coverage    93.75%   93.80%   +0.05%     
===========================================
  Files            8        8              
  Lines          448      452       +4     
===========================================
+ Hits           420      424       +4     
  Misses          28       28              
Impacted Files Coverage Δ
chopper/lib/src/request.dart 100.00% <100.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@techouse techouse added the enhancement New feature or request label May 30, 2023
@techouse techouse changed the title 🐛 fix multipart List data encoding 🐛 fix Multipart List and List<PartValueFile> May 30, 2023
@techouse techouse changed the title 🐛 fix Multipart List and List<PartValueFile> 🐛 fix Multipart List<int>, List<String> and List<PartValueFile> May 30, 2023
@techouse techouse marked this pull request as ready for review May 30, 2023 11:22
@techouse techouse requested a review from JEuler as a code owner May 30, 2023 11:22
@techouse techouse changed the title 🐛 fix Multipart List<int>, List<String> and List<PartValueFile> 🐛 fix Multipart for List<int> and List<String> May 30, 2023
@techouse
Copy link
Collaborator Author

techouse commented May 30, 2023

@JEuler I'd like to get your thoughts on this one, as the solution is quite opinionated, however, I see no other way of making it work. Quite frankly, I'm surprised it wasn't addressed earlier. 😅

@JEuler
Copy link
Collaborator

JEuler commented Jun 3, 2023

I've read your discussion on the issue and looked at the PR. As I understand, nothing can be done better, because Chopper utilizes the http package.

So, we can, of course, provide something like an option to offer the capability to join list of ints to one array... but I don't feel like we should because it is easy to do by using "stringification". So, LGTM.

@techouse
Copy link
Collaborator Author

techouse commented Jun 3, 2023

So, we can, of course, provide something like an option to offer the capability to join list of ints to one array... but I don't feel like we should because it is easy to do by using "stringification".

Well yes, in the case of ints it's easy, but as soon as you have strings or something more complex it explodes. 🙈

@techouse techouse merged commit 455cd34 into lejard-h:develop Jun 3, 2023
@techouse techouse deleted the fix/issue-438-multipart-list branch June 3, 2023 09:07
@techouse techouse mentioned this pull request Jun 3, 2023
techouse added a commit that referenced this pull request Jun 4, 2023
# chopper

## 6.1.4

- [FIX] #439
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants