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 Error when cloning MultiPart File #1903

Merged
merged 24 commits into from
Jul 25, 2023
Merged

Fix Error when cloning MultiPart File #1903

merged 24 commits into from
Jul 25, 2023

Conversation

gabrielaraujoz
Copy link
Contributor

@gabrielaraujoz gabrielaraujoz commented Jul 24, 2023

New Pull Request Checklist

  • I have read the Documentation
  • I have searched for a similar pull request in the project and found none
  • I have updated this branch with the latest main branch to avoid conflicts (via merge from master or rebase)
  • I have added the required tests to prove the fix/feature I'm adding
  • I have updated the documentation (if necessary)
  • I have run the tests without failures
  • I have updated the CHANGELOG.md in the corresponding package

@gabrielaraujoz gabrielaraujoz requested a review from a team as a code owner July 24, 2023 11:57
@gabrielaraujoz
Copy link
Contributor Author

@kuhnroyal thanks for the tips, PR is ready for review

Copy link
Member

@kuhnroyal kuhnroyal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, needs some tweaks for a correct deprecation. And please try to keep the general formatting order of constructor above properties.

headers = caseInsensitiveKeyMap(headers),
contentType = contentType ?? MediaType('application', 'octet-stream');
contentType = contentType ?? MediaType('application', 'octet-stream'),
_stream = data.call();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need the internal _stream field anymore I believe.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This field is returned in finalize(), should I change it to a call to _data?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea I think this is fine, do you think this could be a problem?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, all good for me

/// Creates a new [MultipartFile] from a chunked [Stream] of bytes. The length
/// of the file in bytes must be known in advance. If it's not, read the data
/// from the stream and use [MultipartFile.fromBytes] instead.
///
/// [contentType] currently defaults to `application/octet-stream`, but in the
/// future may be inferred from [filename].
MultipartFile(
Stream<List<int>> stream,
Stream<List<int>> Function() data,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can not remove this, as it will break current users. We need to keep the stream parameter here but add a @Deprecated annotation on the whole constructor with a message that clone() will not work and that it will be removed in 6.0.0.
Then add a new factory MultipartFile.fromStream with the new data parameter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, will do. If it's a new factory, it can't be called by other factories, right? Should I remove the other factories and make dataa named parameter, so that the old constructor can still work? I'm sorry I'm a bit confused with this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I believe what makes this solution work is that we're storing the stream generating method in _data, not the stream itself

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your solution looks great, thanks for the help!

this.length, {
this.filename,
MediaType? contentType,
Map<String, List<String>>? headers,
}) : _stream = stream,
}) : _data = data,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_data: () => stream

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this as well, sorry. Shouldn't data be a method reference that will be stored in that internal parameter _data to be called when cloning? If I just make it a method that will deliver the old stream, the stream would have already been listened to when I try to clone it, resulting in the same error that was happening before. Could you please help me understand that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify, this is what I get when I make this change:
Screenshot 2023-07-24 at 14 36 50

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea this was odd, not sure what Dart is doing there. I wrapped it in another pair of braces (() => stream) to please the compiler.

dio/lib/src/multipart_file.dart Outdated Show resolved Hide resolved
@kuhnroyal
Copy link
Member

I pushed some changes, can you check if that works for you?

@gabrielaraujoz
Copy link
Contributor Author

It does! Thanks a lot @kuhnroyal, I was trying to make the new constructor with as a factory and that's why I could not set _data. It is really good as you've done it and it has passed both unit and my local tests with my stg app.

You're a wizard Harry(Peter)

@gabrielaraujoz gabrielaraujoz requested a review from kuhnroyal July 24, 2023 19:00
dio/CHANGELOG.md Outdated Show resolved Hide resolved
@AlexV525 AlexV525 merged commit 73489dd into cfug:main Jul 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants