Repository: FHIR/sushi (ts)

Issue:
Warn when users assign references to resources w/ `|version`
Reference assignments should not use `|` to indicate a version (see [zulip thread](https://chat.fhir.org/#narrow/stream/179177-conformance/topic/syntax.20of.20Reference.2Ereference.20with.20version/near/373027152)). It's valid for constraining reference types, but not assigning references to instances.

Pull request description:
Warn and ignore version when replacing reference
Fixes #1298 and completes task [CIMPL-1142](https://standardhealthrecord.atlassian.net/browse/CIMPL-1142).

When assigning a reference, versions should not be included. Ignore the version, warn the user to not do that, and try to fish up the referenced item.

A side effect of this is that it is no longer possible to fish up a locally defined entity by name if its name has a `|` character in it. However, it can still be fished up by id, and also, putting `|` in a name is not recommended by the FHIR spec. Please don't do it.

Patch:
diff --git a/src/fhirtypes/common.ts b/src/fhirtypes/common.ts
index 0309756..5d6dec9 100644
--- a/src/fhirtypes/common.ts
+++ b/src/fhirtypes/common.ts
@@ -940,6 +940,16 @@ export function replaceReferences<T extends AssignmentRule | CaretValueRule>(
           rule.sourceInfo
         );
       }
+    } else {
+      // if we still haven't found anything, there's one more possibility:
+      // the reference includes a version, which it doesn't need.
+      const firstPipe = value.reference.indexOf('|');
+      if (firstPipe > -1) {
+        logger.warn('Reference assignments should not include a version.', rule.sourceInfo);
+        clone = cloneDeep(rule);
+        (clone.value as FshReference).reference = value.reference.slice(0, firstPipe);
+        clone = replaceReferences(clone, tank, fisher);
+      }
     }
   } else if (value instanceof FshCode) {
     const codeSystemMeta = fishForMetadataBestVersion(


Test patch:
diff --git a/test/export/InstanceExporter.test.ts b/test/export/InstanceExporter.test.ts
index b5b61c4..3430f99 100644
--- a/test/export/InstanceExporter.test.ts
+++ b/test/export/InstanceExporter.test.ts
@@ -5902,6 +5902,29 @@ describe('InstanceExporter', () => {
       });
     });
 
+    it('should log a warning and ignore the version when assigning a reference that contains a version', () => {
+      const orgInstance = new Instance('TestOrganization');
+      orgInstance.instanceOf = 'Organization';
+      const assignedIdRule = new AssignmentRule('id');
+      assignedIdRule.value = 'org-id';
+      orgInstance.rules.push(assignedIdRule);
+      const assignedRefRule = new AssignmentRule('managingOrganization')
+        .withFile('Reference.fsh')
+        .withLocation([5, 3, 5, 33]);
+      assignedRefRule.value = new FshReference('TestOrganization|2.3.4');
+      patientInstance.rules.push(assignedRefRule);
+      doc.instances.set(patientInstance.name, patientInstance);
+      doc.instances.set(orgInstance.name, orgInstance);
+      const exported = exportInstance(patientInstance);
+      expect(exported.managingOrganization).toEqual({
+        reference: 'Organization/org-id'
+      });
+      expect(loggerSpy.getAllMessages('warn')).toHaveLength(1);
+      expect(loggerSpy.getLastMessage('warn')).toMatch(
+        /Reference assignments should not include a version\..*File: Reference\.fsh.*Line: 5\D*/s
+      );
+    });
+
     it('should log an error when an invalid reference is assigned', () => {
       const observationInstance = new Instance('TestObservation');
       observationInstance.instanceOf = 'Observation';