Body
Summary
When mapping KDS_Person from openEHR to FHIR with multiple=true, the engine may produce multiple Patient resources for a single logical patient if mapped fields come from different COMPOSITION/content[n] roots.
Current Behavior
OpenEhrToFhir groups created resources by a key derived from the first openEHR index of each mapped path.
If mappings resolve from different content[n] entries (e.g. ADMIN_ENTRY.person_data.v0, EVALUATION.gender.v1, ADMIN_ENTRY.versicherungsinformationen.v0), separate Patient instances are created.
Expected Behavior
All Person-related mappings for one composition should merge into one Patient (unless the model explicitly represents multiple patients).
Impact
- Partial duplicate Patient resources in output bundle.
- Users currently need multiple: false as workaround in Person mapping models.
Reproduction
- Use Person context/model with:
- ADMIN_ENTRY.person_data.v0 as start
- extensions mapping additional patient fields from other content[...] archetypes (gender, insurance).
- Run toFHIR on composition containing those archetypes in separate content[n].
- Observe bundle contains 2+ Patient entries (one partial, one fuller).
Root Cause (technical)
Resource creation key is index-based (derived from openEHR path index) when fhirConfig.multiple=true, so logically-related data from different content[n] does not merge.
Proposed Fix
Introduce a stable logical grouping strategy for toFHIR resource assembly, instead of using only openEHR index:
- group by primary target resource/root mapping context, and/or
- allow explicit grouping key in mapping config.
Keep index-based behavior only where truly needed for repeated resource instances.
Workaround
Set spec.fhirConfig.multiple: false in Person model (ADMIN_ENTRY.person_data.v0) to force single Patient.
Acceptance Criteria
- Given Person data split across multiple content[n], toFHIR returns exactly one Patient.
- Existing genuinely multi-instance models remain supported.
- Add regression test covering split-content Person mappings.
NOTE:
This might be fixed with the introduction of the composition layer as it is planned.