Repository: FHIR/sushi (ts)

Issue:
References to contained resources
If you run SUSHI on the following snippet of FSH:
```
Instance: Foo
InstanceOf: ServiceRequest
* contained = annexSR2
* supportingInfo = Reference(annexSR2)

Instance: annexSR2
InstanceOf: Binary
Usage: #inline
* contentType = #zip
```
The output JSON has `supportingInfo.reference` set to `Binary/annexSR2`. The IG Publisher issues errors in the QA report, because `Binary/annexSR2` cannot be resolved, since it is an inline defined contained resource, so it does not exist on its own. Instead, the reference should be set as `#annexSR2` to keep the IG Publisher happy.

Pull request description:
Reference contained resource
Fixes #508 by making it so that references to `#inline` resources are specified as `#id` instead of `ResourceType#id`. This can be tested using this example:
```
Instance: Foo
InstanceOf: ServiceRequest
* contained = annexSR2
* supportingInfo = Reference(annexSR2)

Instance: annexSR2
InstanceOf: Binary
Usage: #inline
* contentType = #zip
```
If you run SUSHI on this snippet on master, the `supportingInfo` reference will look like `Binary/annexSR2`. If you then run the IG Publisher on SUSHI's output, the build should succeed, but the QA report will contain an error like:
![image](https://user-images.githubusercontent.com/52747882/86797332-8aa79d00-c03d-11ea-8a69-05b687d44cd4.png)

If you then run SUSHI on this snippet on this branch, the `supportingInfo` reference will be correctly specified as just `#annexSR2`. Then, when you run the IG Publisher on that output, the QA error shown above should go away.

Patch:
diff --git a/src/fhirtypes/common.ts b/src/fhirtypes/common.ts
index 2089863..5eaee8c 100644
--- a/src/fhirtypes/common.ts
+++ b/src/fhirtypes/common.ts
@@ -248,7 +248,7 @@ export function replaceReferences(
       const id = idRule?.fixedValue ?? instance.id;
       clone = cloneDeep(rule);
       const fv = clone.fixedValue as FshReference;
-      fv.reference = `${instanceMeta.sdType}/${id}`;
+      fv.reference = instance.usage === 'Inline' ? `#${id}` : `${instanceMeta.sdType}/${id}`;
       fv.sdType = instanceMeta.sdType;
     }
   } else if (rule.fixedValue instanceof FshCode) {


Test patch:
diff --git a/test/export/InstanceExporter.test.ts b/test/export/InstanceExporter.test.ts
index 9fedbc0..313a6bd 100644
--- a/test/export/InstanceExporter.test.ts
+++ b/test/export/InstanceExporter.test.ts
@@ -1043,6 +1043,24 @@ describe('InstanceExporter', () => {
       });
     });
 
+    it('should fix an inline reference while resolving the Instance being referred to', () => {
+      const orgInstance = new Instance('TestOrganization');
+      orgInstance.instanceOf = 'Organization';
+      orgInstance.usage = 'Inline';
+      const fixedIdRule = new FixedValueRule('id');
+      fixedIdRule.fixedValue = 'org-id';
+      orgInstance.rules.push(fixedIdRule);
+      const fixedRefRule = new FixedValueRule('managingOrganization');
+      fixedRefRule.fixedValue = new FshReference('TestOrganization');
+      patientInstance.rules.push(fixedRefRule);
+      doc.instances.set(patientInstance.name, patientInstance);
+      doc.instances.set(orgInstance.name, orgInstance);
+      const exported = exportInstance(patientInstance);
+      expect(exported.managingOrganization).toEqual({
+        reference: '#org-id'
+      });
+    });
+
     it('should fix a reference without replacing if the referred Instance does not exist', () => {
       const fixedRefRule = new FixedValueRule('managingOrganization');
       fixedRefRule.fixedValue = new FshReference('http://hl7.org/fhir/us/minimal');
diff --git a/test/export/StructureDefinitionExporter.test.ts b/test/export/StructureDefinitionExporter.test.ts
index e5869d2..771d7a7 100644
--- a/test/export/StructureDefinitionExporter.test.ts
+++ b/test/export/StructureDefinitionExporter.test.ts
@@ -1501,6 +1501,30 @@ describe('StructureDefinitionExporter', () => {
     });
   });
 
+  it('should apply a Reference FixedValueRule and replace the Reference to an inline instance', () => {
+    const profile = new Profile('Foo');
+    profile.parent = 'Observation';
+
+    const instance = new Instance('Bar');
+    instance.id = 'bar-id';
+    instance.instanceOf = 'Patient';
+    instance.usage = 'Inline';
+    doc.instances.set(instance.name, instance);
+
+    const rule = new FixedValueRule('subject');
+    rule.fixedValue = new FshReference('Bar');
+    profile.rules.push(rule);
+
+    exporter.exportStructDef(profile);
+    const sd = pkg.profiles[0];
+
+    const fixedSubject = sd.findElement('Observation.subject');
+
+    expect(fixedSubject.patternReference).toEqual({
+      reference: '#bar-id'
+    });
+  });
+
   it('should not apply a Reference FixedValueRule with invalid type and log an error', () => {
     const profile = new Profile('Foo');
     profile.parent = 'Observation';