Repository: FHIR/sushi (ts)

Issue:
SUSHI should not log a warning when specifying the version of a CodeSystem
In SUSHI 3.0.0 and later, a warning is logged when trying to look up a CodeSystem with a version specified when assigning codes. When SUSHI fishes for the CodeSystem resource, it treats the version specified as if it is the version of the resource, and not the version of the system itself. The version is assigned in the output correctly. However, SUSHI does not need to fish for the CodeSystem with the version, which would avoid the incorrect warnings being logged when it cannot find a resource with the matching version.

See: https://chat.fhir.org/#narrow/stream/215610-shorthand/topic/Strange.20behaviour.20concerning.20CodeSystem.20versions.20and.20aliases/near/375208251

FSH from the discussion: https://fshschool.org/FSHOnline/#/share/3NT8LKd (note: warning is only logged in SUSHI 3.0.0 and later).

Pull request:
System version doesn't need to match when assigning a code
Fixes #1305 and completes task [CIMPL-1147](https://standardhealthrecord.atlassian.net/browse/CIMPL-1147).

A code assigned with a system may include a version as part of the system. This is generally intended to refer to the system's published version in the world, and is not guaranteed to match the version of the CodeSystem FHIR resource. Fish for any version when resolving the system, and do not emit a warning even if the versions don't match.

Patch:
diff --git a/src/fhirtypes/common.ts b/src/fhirtypes/common.ts
index 5d6dec9..83b61fc 100644
--- a/src/fhirtypes/common.ts
+++ b/src/fhirtypes/common.ts
@@ -35,7 +35,7 @@ import {
 } from '../fshtypes';
 import { FSHTank } from '../import';
 import { Type, Fishable, Metadata } from '../utils/Fishable';
-import { fishForMetadataBestVersion, fishInTankBestVersion, logger } from '../utils';
+import { fishInTankBestVersion, logger } from '../utils';
 import { buildSliceTree, calculateSliceTreeCounts } from './sliceTree';
 import { InstanceExporter } from '../export';
 import { MismatchedTypeError } from '../errors';
@@ -952,12 +952,10 @@ export function replaceReferences<T extends AssignmentRule | CaretValueRule>(
       }
     }
   } else if (value instanceof FshCode) {
-    const codeSystemMeta = fishForMetadataBestVersion(
-      fisher,
-      value.system,
-      rule.sourceInfo,
-      Type.CodeSystem
-    );
+    // the version on a CodeSystem resource is not the same as the system's actual version out in the world.
+    // so, they don't need to match.
+    const baseSystem = value.system?.split('|')[0];
+    const codeSystemMeta = fisher.fishForMetadata(baseSystem, Type.CodeSystem);
     if (codeSystemMeta) {
       clone = cloneDeep(rule);
       const assignedCode = getRuleValue(clone) as FshCode;


Test patch:
diff --git a/test/export/InstanceExporter.test.ts b/test/export/InstanceExporter.test.ts
index 3430f99..4ef9c03 100644
--- a/test/export/InstanceExporter.test.ts
+++ b/test/export/InstanceExporter.test.ts
@@ -6310,6 +6310,36 @@ describe('InstanceExporter', () => {
       ]);
     });
 
+    it('should assign a code with a version while replacing the code system name with its url regardless of the specified version', () => {
+      // the version on a CodeSystem resource is not the same as the system's actual version out in the world.
+      // so, they don't need to match.
+      const brightInstance = new Instance('BrightObservation');
+      brightInstance.instanceOf = 'Observation';
+      const assignedCodeRule = new AssignmentRule('code');
+      assignedCodeRule.value = new FshCode('bright', 'Visible|1.2.3');
+      brightInstance.rules.push(assignedCodeRule);
+      doc.instances.set(brightInstance.name, brightInstance);
+
+      const visibleSystem = new FshCodeSystem('Visible');
+      const visibleSystemUrl = new CaretValueRule('');
+      visibleSystemUrl.caretPath = 'url';
+      visibleSystemUrl.value = 'http://hl7.org/fhir/us/minimal/CodeSystem/Visible';
+      const visibleSystemVersion = new CaretValueRule('');
+      visibleSystemVersion.caretPath = 'version';
+      visibleSystemVersion.value = '1.0.0';
+      visibleSystem.rules.push(visibleSystemUrl, visibleSystemVersion);
+      doc.codeSystems.set(visibleSystem.name, visibleSystem);
+      const exported = exportInstance(brightInstance);
+      expect(exported.code.coding).toEqual([
+        {
+          code: 'bright',
+          version: '1.2.3',
+          system: 'http://hl7.org/fhir/us/minimal/CodeSystem/Visible'
+        }
+      ]);
+      expect(loggerSpy.getAllMessages('warn')).toHaveLength(0);
+    });
+
     it('should assign a code to a top level element if the code system was defined as an instance of usage definition', () => {
       const visibleSystem = new Instance('Visible');
       visibleSystem.instanceOf = 'CodeSystem';