Skip to content

feat(sources): format fhir reads each FHIR file by its extension - #44

Open
Gordex2014 wants to merge 4 commits into
HeliosSoftware:mainfrom
Gordex2014:feat/fhir-mixed-file-format
Open

Gordex2014 wants to merge 4 commits into
HeliosSoftware:mainfrom
Gordex2014:feat/fhir-mixed-file-format

Conversation

@Gordex2014

Copy link
Copy Markdown
Contributor

A FHIR bulk export can arrive as a zip, or as loose files, that mixes NDJSON files (one per resource type) with Bundle files. A file source has one format, and a zip loads only the entries that fit it, so no single source could take the whole drop. This PR adds format: fhir for folder and S3 sources: each file is read by its extension.

What changes

A new format value, fhir.

  • It is written in a source file as format: fhir and returned by the API as "format": "fhir".
  • Each loose file and each zip entry gets the reader its extension names: .ndjson and .jsonl are read as NDJSON, .json as a Bundle (or a single resource, as fhir_bundle already allows).
  • Zip entries with any other extension are skipped, as in every format. An entry with no extension at all (named ndjson, say) is skipped too.

A bulk export manifest is recognised.

  • A manifest is a .json object with no string resourceType, a string transactionTime and an array output: the FHIR Bulk Data manifest shape.
  • In a zip it is skipped: no load, no alert.
  • As a loose file it becomes a load with no records and no alert.
  • Any other .json without resourceType (a Bundle that lost it, for one) is still unreadable and alerted.

A loose file with another extension is unreadable.

  • It is a file that matches the pattern but has no reader, like any file today that cannot be read.
  • It is recorded unreadable with the reason not an NDJSON (.ndjson, .jsonl) or JSON (.json) file, raises unreadable_file, and is archived.
  • A folder source moves it to processed/. A read-only bucket leaves it in place.
  • It is not left in the folder on purpose: there it would be listed again on every pass without anyone seeing it.

The definition is checked.

  • format: fhir needs the passthrough transform (fhir_passthrough.py, the file name the engine already uses for bulk auto). Any other transform is refused.
  • bulk_from: source_objects is refused: HFS would get the Bundle bytes in an NDJSON manifest, without the rewritten references.
  • Both checks run before the early return a build without a secret backend takes, so they apply in every build. They apply to the API's create and replace as well (400 invalid_definition).
  • On fhir_server, database and mllp sources the format is forced, as it already is for any declared value.

Bulk delivery.

  • auto resolves to transformed for fhir. The passthrough runs, and the resources go out as NDJSON per type with their ids and rewritten references.
  • They also carry the source tag when delivery.stamp_source is on.
  • No code change was needed: auto was already transformed outside ndjson/fhir_bundle. A test pins it.

Dedup takes the reader into account, in fhir sources only.

  • With the reader chosen by name, renaming a file is how one refused for its extension gets sent again.
  • So an earlier file with the same SHA counts as the original only when both names fall in the same group: zip, NDJSON (.ndjson and .jsonl together), Bundle (.json) or other.
  • This applies in the check before reading and in the one inside the admission transaction.
  • There is no schema change, and sha256 keeps its meaning. Other formats run the same query as before.

Smaller changes.

  • The passthrough's nested-Bundle message now names both ways to read a Bundle.
  • The skip log names a format by its YAML value.

Docs.

  • docs/sources.md:
    • a new "FHIR files" section;
    • the format list, which now also names hl7v2;
    • the zip paragraph, which gains the missing .txt for HL7 and is reworded so it claims the manifest skip only where it is true;
    • the auto bullet.
  • docs/running.md, docs/transforms.md, docs/development.md and docs/api.md (the dedup exception).
  • A new decision, 48, and a new line in ROADMAP.md.
  • A new example source, sources/fhir-export.yaml.example.

In plain words

Drop a FHIR export zip in one folder and it all goes in. The NDJSON files are read as NDJSON and the Bundles as Bundles, and anything that is not FHIR JSON is skipped or reported.

id: fhir-export
name: FHIR export drops
source:
  type: folder
  path: inbox/fhir-export
  pattern: "*.zip"
format: fhir
transform: transforms/fhir_passthrough.py

Side effects checked

  • csv, ndjson, fhir_bundle and hl7v2 sources are unchanged. They keep the same reader, zip filter and dedup query. fhir_bundle with auto still takes source_objects.
  • Rolling back: an engine before this one cannot parse a source file that says format: fhir, so that file has to be changed or removed first. This is recorded in decision 48.
  • Known limits, unchanged from fhir_bundle:
    • A reference to a Bundle entry that has no id stays urn:uuid:….
    • The same bytes under two names read the same way are still one load.
  • Out of scope: fhir_bundle with source_objects hands HFS the Bundle bytes in an NDJSON manifest, as it does today. This PR leaves that alone.

How to verify

  • Config (src/config.rs):
    • the extension → reader table;
    • folder and S3 sources refused without the passthrough;
    • source_objects refused;
    • accepted with the passthrough;
    • a database source given format: fhir is accepted as ndjson;
    • auto → transformed;
    • the format names match their serde names.
  • Mixed zip (tests/zip_drop.rs), holding NDJSON, JSONL, a Bundle with a urn:uuid reference, a manifest, a README and an entry with no extension:
    • three loads, five resources sent by id, the reference sent as Patient/p3;
    • no load for the manifest, the README or the entry with no extension;
    • no alert, and the zip archived.
    • A .json entry that is not a manifest and has no resourceType is unreadable, with one alert.
  • Loose files (tests/zip_drop.rs):
    • .ndjson and .json are sent;
    • manifest.json is an empty load with no alert;
    • notes.txt is unreadable, with the reason and one alert, and moved to processed/.
  • Renaming (tests/zip_drop.rs, loose and in a zip):
    • x.txt is unreadable;
    • the same bytes as x.ndjson are read and sent;
    • as y.jsonl they are a duplicate.
  • The check inside the transaction (tests/events.rs): Db::admit_file with the name predicate returns the later file of the same reader group, not the earlier one of another group. It runs on both stores.
  • API (tests/api.rs): 400 invalid_definition naming the passthrough, then 201 with "format": "fhir".
  • Checks run:
    • On Linux (Debian bookworm, Rust 1.99) at af87306: the whole CI test job passed. That was fmt, both clippy runs, the Python tests, cargo test and the --no-default-features tests.
    • On macOS, everything else also passes. The two tests/worker.rs cases that fail on macOS also fail on main (the /private path prefix and __CF_USER_TEXT_ENCODING), and they pass on Linux.
    • tests/events.rs, tests/zip_drop.rs and tests/loads.rs also passed against PostgreSQL 17, using HIE_TEST_DATABASE_URL.
    • The commit after af87306 changes two lines of docs only.
  • Image: docker build and docker run --rm <image> check, as in the image job, which does not run for a pull request from a fork.

Review notes

  • The plan had two rounds of review, and so did the code.

  • The plan's last corrections came after its second review round:

    • dedup by reader group;
    • the narrower manifest shape;
    • the loose manifest.

    The code review then covered all three.

  • Considered and not taken:

    • Tying fhir to a built-in copy of the passthrough instead of checking its file name. The file name is how the engine already identifies the passthrough. The check is meant to catch a configuration mistake; it is not a security boundary.
    • An end-to-end $bulk-submit test for a mixed zip. The transformed path is already tested, and the config test pins fhir to it.

A file source with format: fhir reads each loose file and zip entry by
its extension: .ndjson and .jsonl as NDJSON, .json as a Bundle or a
single resource. A bulk export manifest is skipped in a zip and is an
empty load as a loose file; a loose file with another extension is
unreadable, alerted and archived.

The definition needs the passthrough transform and refuses
bulk_from: source_objects; auto resolves to transformed. Content dedup
counts an earlier file only when both names are read the same way, so a
file renamed to an extension that reads it is taken again.
Sources, delivery, transforms and development docs, a decision entry,
the roadmap line and an example source for a zipped FHIR export.
…-ups

A fhir zip keeps only the entries a reader takes, so one named just
ndjson or json is skipped rather than read and refused. Tests for
zip-entry dedup by reader, for admit_file with a name predicate, and
for Format::as_str against its serde name. Docs: when NDJSON saved as
.json fails, the manifest shape, the default pattern, the example link
and the fhir exception to content dedup in the API docs.
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