Skip to content

fix(dictionary): keep nameMap current when a tag is registered - #625

Merged
wayfarer3130 merged 3 commits into
masterfrom
register-tag-namemap
Nov 24, 2026
Merged

wayfarer3130 merged 3 commits into
masterfrom
register-tag-namemap

Conversation

@pieper

@pieper pieper commented Oct 28, 2026

Copy link
Copy Markdown
Collaborator

The bug

registerTag() adds an entry to the dictionary but not to DicomMetaDictionary.nameMap, which is what denaturalizeDataset() looks a name up in. So a registered tag can be read but never written — the value is dropped, with nothing but a [warn] Unknown name in dataset to show for it. A silent write failure, which is why it was easy to miss.

registerTag("(4d43,1001)", { name: "MyPrivateMode", vr: "CS", vm: "1" });

DicomMetaDictionary.dictionary["(4d43,1001)"];        // the entry is there
DicomMetaDictionary.nameMap["MyPrivateMode"];          // undefined
DicomMetaDictionary.denaturalizeDataset({ MyPrivateMode: "HYBRID" });  // {}

Why nameMap could not just be regenerated

nameMap is built once at module load, and the loop that was meant to pick up dictionary additions walks Object.keys(DicomMetaDictionary.dictionary). The dictionary is a Proxy with no ownKeys trap over its cache, so that loop sees only entries assigned directly onto the Proxy — never anything registerTag() put in the module's cache.

That loop stays: assigning onto the dictionary is the documented custom-dictionary pattern from #215, and test_custom_dictionary covers it. (I removed it at first and that test caught me.) Registered entries are now contributed alongside it, from a map kept for the purpose, and a registrar callback keeps nameMap current for anything registered after load.

Two more fixes needed to make the feature usable

  • Normalize the tag to upper case. getAllStandardTagEntries() carries upper case and Tag.toCleanString() upper-cases when reading a file, so a tag registered in lower case denaturalized to key 4d431001 while a reader produces 4D431001 — the two never line up. Lookups still accept either case, as they already do for the standard dictionary.
  • Export registerTag and dictionary from index.js. They were reachable only by importing src/dictionary.fast.js directly, so this was not part of the public API at all.

What is deliberately unchanged

The shipped private dictionary stays out of nameMap. Those names are vendor-specific and collide, and writing one because a caller's field happened to match the name would be wrong. Entries from registerTag() default to version: "User" and are included; version: "PrivateTag" is still filtered out. There is a test for that, so the distinction does not get lost later.

This also leaves the [warn] Unknown name in dataset 20011003 warnings in the suite output alone — those are Philips private tags naturalized to numeric keys, a separate problem closer to #388.

Tests

New test/dictionary-register.test.js: reachability from the entry point, write-and-read-back of a private creator plus a block element, upper-case normalization through a real file, both tag spellings, the PrivateTag exclusion, and malformed input. Two of them fail with the registrar removed.

Full suite: 26 suites / 360 tests.

Disclosure

AI-assisted (Claude), per the process in #517. Motivated by needing to write a registered private block in a downstream project, but the fix is general.

registerTag() added an entry to the dictionary but not to
DicomMetaDictionary.nameMap, which is what denaturalizeDataset() looks a
name up in. So a registered tag could be read but never written: the
value was dropped with nothing but a "[warn] Unknown name in dataset",
which is why this was easy to miss.

nameMap could not simply be regenerated either. It is built once at
module load, and the loop that was meant to pick up dictionary additions
walks Object.keys(dictionary) - which sees only entries assigned directly
onto the Proxy, never anything registerTag() put in the module's cache.
That loop stays, because assigning onto the dictionary is the documented
custom dictionary pattern (#215) and a test covers it; registered entries
are now contributed alongside it, from a map kept for the purpose, and a
registrar callback keeps nameMap current for anything registered later.

Two further fixes needed to make the feature usable:

- Normalize the tag to upper case. getAllStandardTagEntries() carries
  upper case, and Tag.toCleanString() upper-cases when reading a file, so
  a tag registered in lower case denaturalized to a key no reader ever
  produces. Lookups still accept either case, as they do for the standard
  dictionary.
- Export registerTag and dictionary from index.js. They were reachable
  only by importing src/dictionary.fast.js directly, so the feature was
  not part of the public API at all.

The shipped private dictionary is still kept out of nameMap: those names
are vendor-specific and collide, and writing one because a caller's field
happened to match would be wrong. Entries from registerTag() default to
version "User" and are included.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@netlify

netlify Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for dcmjs2 ready!

Name Link
🔨 Latest commit a999528
🔍 Latest deploy log https://app.netlify.com/projects/dcmjs2/deploys/6b061618d23158d21c908e92
😎 Deploy Preview https://deploy-preview-625--dcmjs2.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

wayfarer3130 and others added 2 commits November 24, 2026 15:44
Resolve the conflict in src/index.js: keep both export sets in the data
namespace, dictionary and registerTag from this branch, and
findPrivateBlock, reservePrivateBlock and privateBlockKey from #626.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
registerTag() no longer replaces a name that nameMap already has for a
different tag (a standard keyword or an earlier registration). The
existing entry wins, as in 1.x, and a warning names both tags. One
helper, addRegisteredToNameMap(), applies the rule at load and on each
later registerTag() call.

The registerTag() doc comment now says that a private element registered
by position assumes block 0x10, and points to the private-block
functions of #626.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@wayfarer3130
wayfarer3130 merged commit 7f66c60 into master Nov 24, 2026
20 checks passed
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 0.53.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

wayfarer3130 added a commit that referenced this pull request Nov 25, 2026
Record master (d236313) as a parent with -s ours; the tree stays the 1.0-beta tree. Master fixes #657, #622, #510, #625 and #632 are not in 1.x and need ports.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
wayfarer3130 added a commit that referenced this pull request Nov 27, 2026
) (#674)

* fix: registerTag and anonymizer fixes from 0.53.3 (port of #510 and #625)

- registerTag() stores the tag in upper case and finds both cases, so a
  tag registered in lower case keeps its keyword when a file is read.
- The first registration of a name keeps the name; a warning names both
  tags.
- DicomMetaDictionary.entryForName() is the one lookup by name
  (standard keyword first, then a registerTag() name). It is used by
  denaturalizeDataset(), tagAsIntegerFromName(), entryFor() and
  cleanTags(), so cleanTags() empties a registered name in a custom list.
- dcmjs.data exports registerTag and dictionary again, as in 0.53.3.
- The anonymizer list has ResponsiblePersonRole, as in 0.53.3.
- MIGRATION.md states the nameMap difference from 0.53.3.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix: registerTag name rules and tag registry requirements (review of #674)

- The tag of the first argument wins over entry.tag.
- A name that is the keyword of a different tag throws. The keyword of
  the same tag is accepted with no warning.
- When two tags use one name, the second tag gets its hex tag as its
  name and keeps its VR and VM, so a read and a write keep both values.
- A new name for a registered tag removes the old name.
- entryForName() reads only own entries of nameMap.
- docs/specs/registries.md: REG-U-18 to REG-U-21 for the tag registry;
  registries-implementation.md: REG-I-23 to REG-I-26.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants