Skip to content

Fixes#1175

Merged
rrousselGit merged 2 commits intomasterfrom
deep-copy-fix
Feb 27, 2025
Merged

Fixes#1175
rrousselGit merged 2 commits intomasterfrom
deep-copy-fix

Conversation

@rrousselGit
Copy link
Copy Markdown
Owner

@rrousselGit rrousselGit commented Feb 27, 2025

fixes #1164
fixes #1169

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced the deep-copy functionality to address multiple issues, resulting in more reliable handling of nested and cross-library objects.
  • Tests
    • Added a new test case to verify deep-copy behavior across modules, ensuring consistent and predictable operation.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Feb 27, 2025

Walkthrough

The pull request updates the Freezed package to enhance deep-copy functionality. The changelog now broadly addresses various deep-copy bugs instead of a single scenario. The generator logic has been altered to always create a CopyWith object when the annotation is provided, and the deep clone parsing now processes all types regardless of library source. Additionally, the template for the CopyWith class has been refined to handle cases with and without cloneable properties, and a new test has been added to validate cross-library deep copy functionality.

Changes

File(s) Change Summary
packages/freezed/CHANGELOG.md Updated changelog: replaced specific deep-copy fix detail with a general note addressing various deep-copy bugs.
packages/freezed/lib/src/freezed_generator.dart, packages/freezed/lib/src/templates/copy_with.dart Modified deep-copy logic: in the generator, simplified the condition to create a CopyWith object; in the template, refined the interface and override logic based on cloneable properties.
packages/freezed/lib/src/models.dart Removed a conditional check based on library source URI, enabling processing of all types in deep cloning.
packages/freezed/test/deep_copy_test.dart Added a new "cross-library deep copy" test with an additional import to validate deep-copy behavior across libraries.

Sequence Diagram(s)

sequenceDiagram
    participant U as User
    participant FG as FreezedGenerator
    participant CW as CopyWith
    participant DP as DeepCloneableProperty

    U->>FG: Request object generation with deep copy
    FG->>CW: Generate CopyWith logic (if annotation present)
    CW->>DP: Retrieve cloneable properties
    DP-->>CW: Return all types (no library check)
    FG-->>U: Return generated deep copy functionality
Loading

Poem

Hop, hop in fields of code so bright,
Bugs in deep copy now take their flight.
I nibble through logic, swift and keen,
Each clone, a wonder, fresh and clean.
With a joyful bounce and a rabbit cheer,
I celebrate these fixes, hopping near and dear!


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 71cca2e and c835a79.

📒 Files selected for processing (5)
  • packages/freezed/CHANGELOG.md (1 hunks)
  • packages/freezed/lib/src/freezed_generator.dart (1 hunks)
  • packages/freezed/lib/src/models.dart (0 hunks)
  • packages/freezed/lib/src/templates/copy_with.dart (3 hunks)
  • packages/freezed/test/deep_copy_test.dart (1 hunks)
💤 Files with no reviewable changes (1)
  • packages/freezed/lib/src/models.dart
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: freezed (packages/freezed, master, get)
🔇 Additional comments (7)
packages/freezed/CHANGELOG.md (1)

3-3: Updated the changelog to cover broader deep-copy fixes.

The change generalizes the description from a specific deep-copy bug to "various deep-copy bugs," which better reflects the comprehensive fixes implemented across the codebase.

packages/freezed/lib/src/freezed_generator.dart (1)

86-88: Improved CopyWith generation logic.

The change modifies the condition for creating a CopyWith object. Previously, it required both a non-null copyWith annotation AND non-empty cloneable properties. Now it will always create a CopyWith object when the annotation is provided, regardless of whether cloneable properties exist. This enables cross-library deep-copy support.

packages/freezed/test/deep_copy_test.dart (2)

8-8: Added import for cross-library deep copy test.

Added import for deep_copy2.dart to support the new cross-library deep copy test.


14-17: Added test for cross-library deep copy functionality.

This test validates that deep copy works across library boundaries by testing that Country().copyWith.president?.assistant?.call() properly returns null. This is an important test case that verifies the changes made to the deep-copy mechanism.

packages/freezed/lib/src/templates/copy_with.dart (3)

60-80: Enhanced _deepCopyInterface to handle empty cloneable properties.

The method now properly handles cases where cloneableProperties is empty by:

  1. Using conditional logic to generate different class bodies
  2. Adding a constructor for cross-library deep-copy support when no cloneable properties exist
  3. Using abstract mixin for cases with cloneable properties

This improvement enables deep-copy support across library boundaries even when a class doesn't have cloneable properties.


100-100: Added early return for empty cloneable properties.

The method now returns an empty string when there are no cloneable properties, which prevents generating unnecessary code and aligns with the changes in the _deepCopyInterface method.


384-386: Refined override logic based on parent's cloneable properties.

The _maybeOverride method now checks if the parent has any cloneable properties before adding the @override annotation. This prevents unnecessary overrides when the parent doesn't have any properties to clone.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

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.

Missing $*CopyWith Class For Union - 3.0.0 Deep copy from a separate library/file in freezed 3.0.0 doesn't generate

1 participant