Skip to content

feat(fhir): port the DocumentReference mapper (findings 8, 16) - #582

Merged
awatson1978 merged 2 commits into
release/1.0-beta-fhirfrom
fhir-pr3-documentreference
Oct 2, 2026
Merged

awatson1978 merged 2 commits into
release/1.0-beta-fhirfrom
fhir-pr3-documentreference

Conversation

@awatson1978

Copy link
Copy Markdown
Collaborator

This completes the mapper set: the DocumentReference direction for encapsulated documents, with review findings 8 and 16 fixed inside the port. documentReference.js maps a naturalized Encapsulated Document instance (Encapsulated PDF SOP class, or anything carrying a MIMETypeOfEncapsulatedDocument) to a FHIR R4B DocumentReference — masterIdentifier from the SOP Instance UID, type from ConceptNameCodeSequence with LOINC/DCM/SNOMED system mapping, the document itself as a base64 attachment, and a context.related link back to the originating study. The package index swaps the slice-two placeholder for the real import and re-export, so toFhir now routes encapsulated documents to a DocumentReference (and no ImagingStudy) exactly as v2 does.

The source file is a verbatim port apart from the two fixes. The test needed more adaptation than the earlier slices, declared here: v2 built its input datasets with dcmjs.encapsulated.encapsulatePdf, which belongs to the pdfs wave and is not on this branch yet, so buildPdfDataset hand-constructs the same naturalized shape. The Part 10 round-trip test serializes through @dcmjs-org/legacy's datasetToBuffer and reads back with DicomMessage.readFile — legacy is a test-only devDependency; the package's own sources still import nothing but @dcmjs-org/core. The image-fixture tests use packages/fixtures/dicom/sample-dicom.dcm (provenance still being traced, as noted on the earlier slices) through @dcmjs-org/parser.

The findings, each with regressions written against the ported-but-unfixed code and observed failing before the fix:

  • Finding 8 (HIGH). payloadBytes never unwrapped the { InlineBinary } shape that the event-stream naturalizer produces for binary elements, and new Uint8Array(<plain object>) is zero-length — so every naturalized encapsulated document silently mapped to {"size": 0, "data": ""}. The pdf-side extractor already handled both shapes, meaning the two extractors disagreed on the same input. The fix ports that exact unwrap block (base64 string, ArrayBuffer, or typed-array view) as a local copy — this package must not import the pdfs package, so the ~20 lines are duplicated knowingly, with a comment naming the original. The regression feeds a base64 InlineBinary and asserts the attachment round-trips byte-exact; two red tests covered the string and buffer/view shapes.
  • Finding 16 (MED). The Part 10 writer pads only odd-length OB values, so an even stored length has no pad byte — but the trailing-NUL trim ran anyway, corrupting any even-length payload that genuinely ends in 0x00 (ordinary content for STL, OBJ, CDA, octet-stream). EncapsulatedDocumentLength (0042,0015) carries the exact length, so when present it now wins over the heuristic; without it the historical heuristic still applies, since for a bare PDF payload that remains the best available guess. This is the read half of a pair: the write side that actually emits (0042,0015) lands with the pdfs wave.

The mapping in use:

import { toFhir } from "./packages/fhir/src/index.js";

const { patient, imagingStudy, documentReference } = toFhir(pdfDataset);
// imagingStudy === null for encapsulated documents
// documentReference.content[0].attachment =
//   { contentType: "application/pdf", title: "Discharge Summary",
//     size: 87, data: "JVBERi0xLjQK..." }   // byte-exact

Thirteen tests in the ported suite (the nine v2 tests — full mapping, subject/includeData options, ConceptNameCodeSequence typing, null for images, the odd-length Part 10 round trip — plus the four finding regressions), 51 in the package, 895 passing overall on this branch, and pnpm run build is green.

🤖 Generated with Claude Code

awatson1978 and others added 2 commits October 2, 2026 13:41
documentReference.js comes over verbatim, and the package index swaps
the slice-two placeholder for the real import and re-export, completing
the toFhir three-key surface. The ported test builds its Encapsulated
PDF datasets synthetically (v2 built them with dcmjs.encapsulated,
which belongs to the pdfs wave and is not on this branch yet), keeps
the Part 10 round trip through @dcmjs-org/legacy as a test-only
devDependency, and naturalizes the image fixture through
@dcmjs-org/parser like the toFhir suite.

Co-Authored-By: Claude Fable 5 <[email protected]>
…tLength

Review findings 8 and 16 on PR #512. The event-stream naturalizer wraps
binary elements as { InlineBinary } (base64 string, ArrayBuffer, or
view); payloadBytes never unwrapped that shape, and new Uint8Array of a
plain object is empty, so every naturalized encapsulated document
silently mapped to { size: 0, data: "" }. The unwrap block mirrors the
one in v2's extractEncapsulatedPdf, kept as a local copy because this
package must not depend on the pdfs package.

The even-length pad-trim heuristic also stripped real trailing zero
bytes, since an even stored length never carries a writer pad. When
EncapsulatedDocumentLength (0042,0015) is present it now wins over the
heuristic; the write side that emits it lands with the pdfs wave.

Co-Authored-By: Claude Fable 5 <[email protected]>
@awatson1978
awatson1978 merged commit bf71595 into release/1.0-beta-fhir Oct 2, 2026
10 checks passed
@awatson1978
awatson1978 deleted the fhir-pr3-documentreference branch October 2, 2026 18:56
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