Skip to content

fix: preserve private elements through denaturalize in a writable shape - #546

Merged
awatson1978 merged 1 commit into
release/1.0-beta-corefrom
core/finding-4-private-fallback-writable
Sep 30, 2026
Merged

awatson1978 merged 1 commit into
release/1.0-beta-corefrom
core/finding-4-private-fallback-writable

Conversation

@awatson1978

Copy link
Copy Markdown
Collaborator

Private elements now survive naturalizeDataset → denaturalizeDataset → write() — in a shape the writer can actually serialize.

Some background. When naturalizeDataset meets an element that has no dictionary entry — a private tag, typically — it keeps the raw 8-hex-digit tag as the key. On the way back, denaturalizeDataset looked that key up in nameMap, found nothing, logged "Unknown name in dataset", and dropped the element. So a plain read → naturalize → denaturalize → write round trip silently destroyed every unregistered private element in the file. That's the classic vendor-tag workflow: read a GE or Philips file, touch one standard attribute, write it back — and all the private calibration data is gone.

The review of PR #512 flagged this exact spot as finding 4. That branch had tried to fix the drop with a vr: "UN" fallback, but it left Value as an array of strings — UN carries bytes, so DicomDict.write() then threw First argument to DataView constructor must be an ArrayBuffer. Preserving the element is right (PS3.5 6.2.2); building an unwritable element is not. This PR implements the preservation correctly.

The mechanics: the VR is already known at naturalize time — the reader put it on the element. So naturalizeDataset now records it in the dataset's existing _vrMap side-table whenever there's no dictionary entry, and denaturalizeDataset uses that recorded VR to rebuild the element through the normal path. No guessing, no UN coercion: an LO comes back as an LO with its string value intact, and the writer serializes it exactly as it would any known element.

const dicomDict = DicomMessage.readFile(buffer); // contains (0009,0010) / (0009,1001)
const dataset = DicomMetaDictionary.naturalizeDataset(dicomDict.dict);
dataset._vrMap["00091001"]; // "LO" — recorded at naturalize time (new)

dicomDict.dict = DicomMetaDictionary.denaturalizeDataset(dataset);
// before: both private elements silently dropped ("Unknown name in dataset")
// after:  { "00091001": { vr: "LO", Value: ["private-top"] } } — and
const reread = DicomMessage.readFile(dicomDict.write()); // round-trips cleanly

When no VR was ever recorded — a hand-built dataset like denaturalizeDataset({ "00291010": ["ACME"] }) with no _vrMap — the element is still skipped with the existing warning, per the review's guidance: skip when no VR is known, rather than emit something the writer chokes on.

The regression tests are the two known-gap skips in test/issues/issue388-private-tags.test.js, now green and unskipped: the #388 case (private elements inside a SQ item survive denaturalize → write → re-read) and the #215 case (unregistered top-level private tags survive static denaturalize and write). A third new test pins the no-recorded-VR skip. This is review finding 4 from the PR #512 review map; the defective UN fallback itself never landed on this branch, so what lands here is the intended behavior done right. For the record, master (0.52) has the silent-drop behavior, not the unwritable-element defect — that was new code in the #512 development branch.

🤖 Generated with Claude Code

naturalizeDataset now records the original VR of dictionary-less
elements in _vrMap, and denaturalizeDataset rebuilds a writable element
from that VR instead of dropping the element with an Unknown-name
warning. A hex key with no recorded VR is still skipped, so no element
is ever emitted in a shape the writer cannot serialize.

Review finding 4 from the PR #512 review map; unskips the #388/#215
known-gap tests in test/issues/issue388-private-tags.test.js.

Co-Authored-By: Claude Fable 5 <[email protected]>
@awatson1978
awatson1978 merged commit 3a90958 into release/1.0-beta-core Sep 30, 2026
8 checks passed
@awatson1978
awatson1978 deleted the core/finding-4-private-fallback-writable branch September 30, 2026 11:34
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 1.0.0-beta.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant