Skip to content

Fix Query.Clone() shallow copy of Includes and Variables#775

Merged
ahmad-moussawi merged 2 commits intomainfrom
copilot/fix-query-clone-bug
Apr 10, 2026
Merged

Fix Query.Clone() shallow copy of Includes and Variables#775
ahmad-moussawi merged 2 commits intomainfrom
copilot/fix-query-clone-bug

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 10, 2026

Query.Clone() and XQuery.Clone() assigned Includes and Variables by reference, so mutations on the clone leaked back to the original.

var query = new Query("users")
    .Include("posts", new Query("posts"), "user_id", "id");

var clone = query.Clone();
clone.Includes[0].Name = "modified";   // mutates original!
clone.Include("comments", ...);        // adds to original's list!
clone.Define("x", 1);                  // adds to original's Variables!

Changes

  • Include: Added Clone() method that deep-copies all fields, including recursively cloning the embedded Query.
  • Query.Clone(): Replace reference assignments with Includes.Select(i => i.Clone()).ToList() and new Dictionary<string, object>(Variables).
  • XQuery.Clone(): Same fixes applied for consistency.
  • Tests: Added four regression tests covering independent list mutation, independent object property mutation, independent variable dictionary mutation, and full property preservation.
Original prompt

Create a pull request that fixes the bug described in PR #748 (“Fix Clone() in Query.”) for repository sqlkata/querybuilder.

Context:

  • The user provided PR metadata: Fix Clone() in Query. #748
  • Goal: “check this issue” (the bug around Query.Clone()), and implement a PR to fix it.

Requirements:

  1. Inspect the current implementation of Query.Clone() (and any related cloning/copying methods) and determine what is wrong.
  2. Implement a correct fix for Clone() so that cloning a Query produces an independent copy without shared mutable state (e.g., lists, dictionaries, clauses, bindings, variables, includes, components, etc.).
  3. Add or update unit tests to reproduce the bug and ensure it remains fixed.
  4. Ensure all existing tests pass.
  5. Keep the fix minimal and consistent with the project’s patterns.

Notes:

  • Base branch: main.
  • Use clear commit messages and PR title describing the fix.
  • If there are multiple query types (e.g., Query, XQuery, SqlServerQuery, etc.), ensure cloning behavior is correct across them or document why only Query needs changes.

The following is the prior conversation context from the user's chat exploration (may be truncated):

User: Can you check this issue, and implement a PR to fix it?

Assistant: I’ve queued up the changes to create a fix PR for Query.Clone(), but I’m currently paused waiting for your approval/confirmation in the tool dialog—once you accept, I’ll proceed with implementing the fix and adding regression tests.

This pull request was created from Copilot chat.

- Add Clone() method to Include class for deep copying
- Fix Query.Clone() to deep-copy Includes list and Variables dictionary
- Fix XQuery.Clone() with same deep-copy fixes
- Add unit tests for the clone bug (independent list, objects, dict, properties)

Fixes #747

Agent-Logs-Url: https://github.com/sqlkata/querybuilder/sessions/d82ad628-73f5-4c2f-b56e-f4021ce1519f

Co-authored-by: ahmad-moussawi <2517523+ahmad-moussawi@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix bug in Query.Clone() to ensure independent copies Fix Query.Clone() shallow copy of Includes and Variables Apr 10, 2026
Copilot AI requested a review from ahmad-moussawi April 10, 2026 08:59
@ahmad-moussawi ahmad-moussawi marked this pull request as ready for review April 10, 2026 11:04
@ahmad-moussawi ahmad-moussawi merged commit 5122d43 into main Apr 10, 2026
2 checks passed
@ahmad-moussawi ahmad-moussawi deleted the copilot/fix-query-clone-bug branch April 10, 2026 11:27
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.

2 participants