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

Merged
merged 8 commits into from
Nov 11, 2024

Conversation

helloliuyiming
Copy link
Contributor

@helloliuyiming helloliuyiming commented Oct 3, 2024

Resolves #2303

Fix boundary inconsistency in FormData.clone method

Description:

This PR addresses an issue where cloning FormData objects resulted in different boundaries each time. The problem was caused by allocating a new boundary for each clone without any boundary reuse logic.

Changes made:

  • Added an initBoundary parameter to the FormData constructor.
  • Implemented boundary reuse logic when cloning FormData objects.
  • Updated documentation to explain how initBoundary affects boundaryName.

Technical details:

  • The FormData constructor now accepts an optional initBoundary parameter.
  • When initBoundary is provided, it overrides the boundaryName configuration.
  • The cloning process now reuses the existing boundary when appropriate.

Impact:
This fix ensures consistent behavior when cloning FormData objects and improves efficiency by reusing boundaries where possible. It should resolve issues related to unexpected boundary changes during FormData manipulation.

Testing:

  • Added unit tests to verify boundary consistency across cloned FormData objects.
  • Manually tested with various use cases to ensure backwards compatibility.

Note to reviewers:
Please pay special attention to the boundary reuse logic and any potential side effects on existing FormData usage.

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

Copy link
Member

@AlexV525 AlexV525 left a comment

Choose a reason for hiding this comment

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

Is it possible to directly update the _boundary after the construction, so we don't need an extra argument for the constructor which won't increase the complexity for usages?

@helloliuyiming
Copy link
Contributor Author

Is it possible to directly update the _boundary after the construction, so we don't need an extra argument for the constructor which won't increase the complexity for usages?

I apologize for the delayed response as I've been quite busy lately.

Regarding your suggested solution - I want to make sure I understand it correctly. Would this be what you had in mind?

  FormData({
    String? initBoundary,
    this.boundaryName = _boundaryName,
    this.camelCaseContentDisposition = false,
  }) {
    _init();
    if(initBoundary != null) {
      _boundary = initBoundary;
    }
  }

@AlexV525
Copy link
Member

No. What I mean is:

// Convenience method to clone finalized FormData when retrying requests.
FormData clone() {
  final clone = FormData();
  clone._boundary = _boundary; // <-- Replace the boundary.
  clone.fields.addAll(fields);
  for (final file in files) {
    clone.files.add(MapEntry(file.key, file.value.clone()));
  }
  return clone;
}

@helloliuyiming helloliuyiming requested a review from a team as a code owner November 10, 2024 10:08
@helloliuyiming
Copy link
Contributor Author

Ah, I see. Your solution is indeed much better than what I proposed. Thank you for the guidance. I have submitted a new one implementing your suggested approach. Please review again, thanks.

@AlexV525
Copy link
Member

Please add the CHANGELOG entry and update the PR checklist status.

@helloliuyiming
Copy link
Contributor Author

done!

Copy link
Member

@AlexV525 AlexV525 left a comment

Choose a reason for hiding this comment

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

Thanks for the update. One last nit

dio/CHANGELOG.md Outdated Show resolved Hide resolved
Copy link
Contributor

Code Coverage Report: Only Changed Files listed

Package Base Coverage New Coverage Difference
Overall Coverage 🟢 84.7% 🟢 84.71% 🟢 0.01%

Minimum allowed coverage is 0%, this run produced 84.71%

@AlexV525 AlexV525 merged commit 5406162 into cfug:main Nov 11, 2024
3 checks passed
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.

Improve FormData Clone Method to Maintain Boundary Consistency and Resolve Signature Verification Issues
2 participants