Skip to content

fix(core): write an element with no value with zero length for every VR - #638

Merged
wayfarer3130 merged 1 commit into
1.0-betafrom
fix/1.0-beta-write-empty-value
Nov 24, 2026
Merged

wayfarer3130 merged 1 commit into
1.0-betafrom
fix/1.0-beta-write-empty-value

Conversation

@wayfarer3130

@wayfarer3130 wayfarer3130 commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Problem

DicomDict.write() throws TypeError: Cannot read properties of undefined (reading 'length') for a string element with no Value. A DICOM JSON element with no Value is the normal form of an empty element, for example an empty Type 2 attribute in DICOMweb metadata. NAT-U-RT-7 in docs/specs/naturalized-dataset.md already says that the writer writes such an element with zero length.

The cause: ValueRepresentation.write writes nothing for an undefined value. Then the base writeBytes checks the value with === null only, and reads checkValue.length of undefined.

The same defect occurs in other forms:

Before this fix, Value: null, Value: "" and Value: [] already wrote correctly in explicit VR.

Changes

  • Tag.write changes the value before the VR writer runs. Thus the rule applies to every VR:
    • An absent, null or "" value becomes [].
    • A list of only undefined entries becomes [].
    • An undefined entry beside another entry becomes null. The existing null-entry rule then applies (an empty string value, an empty PN name, or 0 with a warning for a binary VR).
  • SequenceOfItems.writeBytes writes a null item as an empty item.
  • docs/specs/naturalized-dataset.md: NAT-U-RT-7 lists the forms of an element with no value, and the new NAT-I-NULL-2 gives the implementation.

Tests

  • The new test/core/absentValues.test.js writes each of 34 VRs in 6 forms: no Value key, undefined, null, "", [] and [undefined]. It writes in explicit VR little endian and in implicit VR little endian. The test reads the value length of each element from the written bytes, and expects 0 and no warning. Two more cases check an undefined entry beside a value, and a null sequence item.
  • test/issues/issue417-undefined-values.test.js: the skipped "KNOWN GAP" test now runs and passes. The test that expected the throw Not a number: undefined now expects a write that succeeds.
  • pnpm test: 1239 tests pass, and 11 tests are skipped. 3 tests of test/data.test.js fail (test_multiframe_1, test_oneslice_seg, test_deflated). The same 3 tests also fail on 1.0-beta without this change, because the local test data is missing.

🤖 Generated with Claude Code

DicomDict.write() threw "Cannot read properties of undefined (reading
'length')" for a string element with no Value, which is the DICOM JSON
form of an empty element (NAT-U-RT-7). Tag.write now gives each VR writer
an empty list for an absent, null, "" or all-undefined value, and turns
another undefined entry into a null entry. A null sequence item is
written as an empty item.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@wayfarer3130
wayfarer3130 marked this pull request as ready for review November 11, 2026 14:53
@wayfarer3130
wayfarer3130 requested a review from pieper November 11, 2026 14:53
@wayfarer3130
wayfarer3130 merged commit bc31dc9 into 1.0-beta Nov 24, 2026
4 checks passed
@github-actions

Copy link
Copy Markdown

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

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