Skip to content

fix: DetachedDocument.toMap() must also preserve property insertion order - #6530

Merged
lvca merged 1 commit into
mainfrom
fix/6472-detachedoc-order
Aug 21, 2026
Merged

lvca merged 1 commit into
mainfrom
fix/6472-detachedoc-order

Conversation

@lvca

@lvca lvca commented Aug 21, 2026

Copy link
Copy Markdown
Member

Problem

PR #6500 (issue #6472) fixed keys()/values() index-misalignment for Document by:

  1. Making SQLMethodValues use document.toMap(false) instead of toMap().
  2. Switching MutableDocument.toMap(boolean) from a plain HashMap to a LinkedHashMap, so its iteration order matches getPropertyNames() (both derived from the same insertion-ordered map field).

The post-merge review caught that DetachedDocument has the exact same bug and was left unfixed:

// DetachedDocument.java
public synchronized Map<String, Object> toMap(final boolean includeMetadata) {
  final Map<String, Object> result = new HashMap<>(map);   // bucket order
  ...
}

public synchronized Set<String> getPropertyNames() {
  return map.keySet();                                      // insertion order (map is a LinkedHashMap)
}

Document.detach() is a common pattern for handing documents outside a transaction, so detached.keys()/detached.values() (or plain toMap(false)) could still disagree by index for a detached document with 3+ properties.

Fix

Switch DetachedDocument.toMap(boolean) to build a LinkedHashMap, matching MutableDocument and ImmutableDocument (which already used LinkedHashMap).

Tests

  • DocumentTest.detachedDocumentToMapPreservesPropertyInsertionOrder: asserts toMap(false) key/value order matches getPropertyNames() on a detached document with 5 properties.
  • SQLMethodValuesTest.keysAndValuesAreIndexAlignedOnDetachedDocument: asserts keys()[i]/values()[i] correspondence via the SQL methods, on a detached document.

Confirmed both new tests fail against the pre-fix code (property order came back scrambled, e.g. [name, country, city, age, email] instead of insertion order [name, age, city, country, email]) and pass with the fix.

Fixes the remaining part of #6472 not covered by #6500.

…rder

Follow-up to PR #6500 (issue #6472): that PR fixed the keys()/values()
index-misalignment for MutableDocument by switching its toMap(boolean) from
a plain HashMap to a LinkedHashMap, but DetachedDocument.toMap(boolean) had
the exact same bug and was left unfixed - it also copied into a plain
HashMap while its getPropertyNames() returns the same LinkedHashMap-backed
map's keySet(). Document.detach() is a common pattern for handing documents
outside a transaction, so keys()/values() on a detached document could
still disagree by index.

Switch DetachedDocument.toMap(boolean) to LinkedHashMap as well, matching
MutableDocument and ImmutableDocument. Confirmed the added regression tests
fail against the pre-fix code (order came back scrambled, e.g.
[name, country, city, age, email] instead of insertion order) and pass
with the fix.
@mergify

mergify Bot commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

@codacy-production

codacy-production Bot commented Aug 21, 2026 •

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Coverage 100.00% diff coverage · -6.55% coverage variation

Metric Results
Coverage variation ✅ -6.55% coverage variation
Diff coverage ✅ 100.00% diff coverage

View coverage diff in Codacy

Coverage variation details
Coverable lines Covered lines Coverage
Common ancestor commit (c751bca) 163958 130992 79.89%
Head commit (0f245e9) 196345 (+32387) 144014 (+13022) 73.35% (-6.55%)

Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: <coverage of head commit> - <coverage of common ancestor commit>

Diff coverage details
Coverable lines Covered lines Diff coverage
Pull request (#6530) 1 1 100.00%

Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: <covered lines added or modified>/<coverable lines added or modified> * 100%

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@claude

claude Bot commented Aug 21, 2026

Copy link
Copy Markdown

Review

Clean, minimal, well-targeted fix — matches the pattern already used in MutableDocument/ImmutableDocument.

Correctness

  • The LinkedHashMap-copy fix in DetachedDocument.toMap(boolean) is correct and, importantly, general: toMap() copies from this.map and getPropertyNames() returns map.keySet() directly (line 168), so the two are always order-consistent regardless of which constructor populated map — the full Document.detach() path (init(), which explicitly builds a LinkedHashMap) and the partial-detach path used by DocumentIndexer (which wraps a caller-supplied map as-is). Nice that this doesn't rely on the caller-supplied map in the partial-detach constructor being order-preserving itself.
  • Searched for the same anti-pattern elsewhere (new HashMap<>(map) under engine/src/main/java/com/arcadedb/database/) and found no other occurrence — this does look like the last spot from keys()/values() on a Document are not parallel arrays: values() includes internal fields (@rid/@type/@cat) that keys() omits #6472.

Tests

  • Both new tests are solid regression tests: they assert index-alignment between getPropertyNames()/keys() and toMap()/values() rather than just checking for LinkedHashMap, so they'd also catch a future regression introduced through a different code path.
  • Good that SQLMethodValuesTest exercises the actual SQL keys()/values() methods on a detached document (matching how keys()/values() on a Document are not parallel arrays: values() includes internal fields (@rid/@type/@cat) that keys() omits #6472 was originally reported) rather than just unit-testing toMap() directly.
  • Per repo convention, confirmed the PR description states both tests fail pre-fix and pass post-fix.

Minor / nit

  • Both new tests duplicate the 5-property "OrderPerson" fixture setup that already exists in toMapPreservesPropertyInsertionOrder in DocumentTest.java. Not blocking given the codebase's stated preference for avoiding premature abstraction over three similar lines, but if this fixture gets reused a fourth time a small shared builder might be worth it.
  • The inline comment referencing MutableDocument.toMap() and issue keys()/values() on a Document are not parallel arrays: values() includes internal fields (@rid/@type/@cat) that keys() omits #6472 is a good "why", consistent with CLAUDE.md's guidance to comment on non-obvious constraints rather than restate the code.

No security, performance, or dependency concerns — this only changes the concrete Map implementation used for a short-lived copy, so no algorithmic-complexity or memory regression.

Nice catch closing the gap left by the post-merge review on #6500.

@lvca lvca self-assigned this Aug 21, 2026
@lvca lvca added this to the 26.9.1 milestone Aug 21, 2026
@lvca
lvca merged commit 194877e into main Aug 21, 2026
27 of 28 checks passed
@lvca
lvca deleted the fix/6472-detachedoc-order branch August 21, 2026 03:24
@codecov

codecov Bot commented Aug 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 71.17%. Comparing base (c751bca) to head (0f245e9).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6530      +/-   ##
==========================================
- Coverage   71.19%   71.17%   -0.02%     
==========================================
  Files        1866     1866              
  Lines      163958   163958              
  Branches    34629    34629              
==========================================
- Hits       116724   116694      -30     
- Misses      33007    33030      +23     
- Partials    14227    14234       +7     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

1 participant