Repository: FHIR/sushi (ts)

Issue:
Trouble w/ instances of Parameters
A user on Zulip has reported an issue using this FSH:

```
Instance: ParametersEx12
InstanceOf: Parameters
Usage: #inline
* parameter.name = "patient"
* parameter.valueReference = Reference(PatientEx12)
```

In this case, they receive the error:
```
error: Cannot read property 'forEach' of undefined.
```
The reporter says `PatientEx12` and `ParametersEx12` are both inline instances in a bundle.

See: https://chat.fhir.org/#narrow/stream/215610-shorthand/topic/valueReference.20for.20Parameters


Pull request:
Fix reference type checking
Fixes #504.

We were being a little too aggressive when checking if a `Reference` could be fixed on an instance. When no `targetProfile` was present on the element having a value set on it, we were not allowing any `Reference` values to be set, when in fact we should allow any `Reference` value to be set in this case. There is an example of this in #504. To test, you can run the test added in https://github.com/FHIR/sushi/commit/717a2f59ab1e6cfbfd23cbd33d56d9f776171807, and see that it fails in that commit, and then if you run the same test on https://github.com/FHIR/sushi/commit/61a40cf60f5895d9902d8d285c3d95e721cd2e23, it passes. Also, you can run the example given in the issue on master, and notice you see an error:
```
error: Cannot read property 'forEach' of undefined.
```
On this branch you will see no such error, and the `valueReference` field will be correctly set.

Patch:
diff --git a/src/fhirtypes/ElementDefinition.ts b/src/fhirtypes/ElementDefinition.ts
index da93ff1..19048c5 100644
--- a/src/fhirtypes/ElementDefinition.ts
+++ b/src/fhirtypes/ElementDefinition.ts
@@ -1162,16 +1162,15 @@ export class ElementDefinition {
         break;
       case 'Reference':
         value = value as FshReference;
-        if (value.sdType) {
+        // If no targetProfile is present, there is nothing to check the value against, so just fix it
+        if (value.sdType && this.type[0].targetProfile) {
           const validTypes: string[] = [];
-          this.type.forEach(t =>
-            t.targetProfile.forEach(tp => {
-              const tpType = fisher.fishForMetadata(tp)?.sdType;
-              if (tpType) {
-                validTypes.push(tpType);
-              }
-            })
-          );
+          this.type[0].targetProfile.forEach(tp => {
+            const tpType = fisher.fishForMetadata(tp)?.sdType;
+            if (tpType) {
+              validTypes.push(tpType);
+            }
+          });
 
           const referenceLineage = this.getTypeLineage(value.sdType, fisher);
           if (!referenceLineage.some(md => validTypes.includes(md.sdType))) {


Test patch:
diff --git a/test/export/InstanceExporter.test.ts b/test/export/InstanceExporter.test.ts
index 4d0d148..9fedbc0 100644
--- a/test/export/InstanceExporter.test.ts
+++ b/test/export/InstanceExporter.test.ts
@@ -1075,6 +1075,21 @@ describe('InstanceExporter', () => {
       });
     });
 
+    it('should fix a reference when the type has no targetProfile', () => {
+      const referencedPatientInstance = new Instance('ReferencedPatient');
+      referencedPatientInstance.instanceOf = 'Patient';
+      doc.instances.set(referencedPatientInstance.name, referencedPatientInstance);
+
+      const fixedRefRule = new FixedValueRule('extension.valueReference');
+      fixedRefRule.fixedValue = new FshReference('ReferencedPatient');
+      patientInstance.rules.push(fixedRefRule); // * extension.valueReference = Reference(BasePatient)
+
+      const exported = exportInstance(patientInstance);
+      expect(exported.extension[0].valueReference).toEqual({
+        reference: 'Patient/ReferencedPatient'
+      });
+    });
+
     it('should log an error when an invalid reference is fixed', () => {
       const observationInstance = new Instance('TestObservation');
       observationInstance.instanceOf = 'Observation';