Repository: FHIR/sushi (ts)

Issue:
{CodeSystem name}|{version}#{code} "{display}" not working correctly
See Zulip: https://chat.fhir.org/#narrow/stream/215610-shorthand/topic/Using.20Coding.20with.20.2Eversion/near/239067905

It appears that specifying the version in FSH concept syntax does not work when referencing a code from FSH-defined CodeSystems.  For example, this should work but does not:
```
CodeSystem: Test
* ^version = "1.0"
* #testcode "test-code" "A code for testing"


Instance: TestPatient
InstanceOf: Patient
* maritalStatus = Test|1.0#testcode
```

It results in:
> error Resolved value "Test|1.0" is not a valid URI.

Pull request description:
Use Version with FSH Code System
Fixes #816 

This PR supports parsing out the CodeSystem system from the version when fishing for CodeSystems while resolving references. Previously, the full `system|version` text was used, so fishing for the code system was unsuccessful. This instead uses just the `system` portion if the version is included, and then adds the version back after the URL is resolved.

Patch:
diff --git a/src/fhirtypes/common.ts b/src/fhirtypes/common.ts
index 896347b..1346bf3 100644
--- a/src/fhirtypes/common.ts
+++ b/src/fhirtypes/common.ts
@@ -299,12 +299,14 @@ export function replaceReferences<T extends AssignmentRule | CaretValueRule>(
       assignedReference.sdType = instanceMeta.sdType;
     }
   } else if (value instanceof FshCode) {
-    const codeSystem = tank.fish(value.system, Type.CodeSystem);
+    const [system, ...versionParts] = value.system?.split('|') ?? [];
+    const version = versionParts.join('|');
+    const codeSystem = tank.fish(system, Type.CodeSystem);
     const codeSystemMeta = fisher.fishForMetadata(codeSystem?.name, Type.CodeSystem);
     if (codeSystem && codeSystemMeta) {
       clone = cloneDeep(rule);
       const assignedCode = getRuleValue(clone) as FshCode;
-      assignedCode.system = codeSystemMeta.url;
+      assignedCode.system = `${codeSystemMeta.url}${version ? `|${version}` : ''}`;
     }
   }
   return clone ?? rule;
diff --git a/src/fshtypes/FshCode.ts b/src/fshtypes/FshCode.ts
index eb1c086..8750192 100644
--- a/src/fshtypes/FshCode.ts
+++ b/src/fshtypes/FshCode.ts
@@ -18,7 +18,9 @@ export class FshCode extends FshEntity {
     }
     if (this.system) {
       if (this.system.indexOf('|') > -1) {
-        [coding.system, coding.version] = this.system.split('|', 2);
+        let versionParts;
+        [coding.system, ...versionParts] = this.system.split('|');
+        coding.version = versionParts.join('|');
       } else {
         coding.system = this.system;
       }


Test patch:
diff --git a/test/export/InstanceExporter.test.ts b/test/export/InstanceExporter.test.ts
index 9e65af8..3d22eab 100644
--- a/test/export/InstanceExporter.test.ts
+++ b/test/export/InstanceExporter.test.ts
@@ -1679,6 +1679,26 @@ describe('InstanceExporter', () => {
       ]);
     });
 
+    it('should assign a code with a version to a top level element while replacing the local code system name with its url and use the specified version', () => {
+      const brightInstance = new Instance('BrightObservation');
+      brightInstance.instanceOf = 'Observation';
+      const assignedCodeRule = new AssignmentRule('code');
+      assignedCodeRule.value = new FshCode('bright', 'Visible|1.0.0|a'); // Version should include anything that comes after the first |
+      brightInstance.rules.push(assignedCodeRule);
+      doc.instances.set(brightInstance.name, brightInstance);
+
+      const visibleSystem = new FshCodeSystem('Visible');
+      doc.codeSystems.set(visibleSystem.name, visibleSystem);
+      const exported = exportInstance(brightInstance);
+      expect(exported.code.coding).toEqual([
+        {
+          code: 'bright',
+          version: '1.0.0|a',
+          system: 'http://hl7.org/fhir/us/minimal/CodeSystem/Visible'
+        }
+      ]);
+    });
+
     it('should assign a code to a nested element while replacing the local code system name with its url', () => {
       const brightInstance = new Instance('BrightObservation');
       brightInstance.instanceOf = 'Observation';