Repository: FHIR/sushi (ts)

Issue:
Overwriting value on a Quantity
See this issue for full details: https://jira.hl7.org/browse/FHIR-28552.

When an author sets `value` on a Quantity, and then sets the Codeable properties on that Quantity as in the following:
```
* dosage.doseAndRate.doseQuantity.value = 250.0
* dosage.doseAndRate.doseQuantity = UCUM#mg "mg"
```
SUSHI overwrites the setting of `value` when setting the Codeable properties in the second line.


Pull request description:
Quantity fix
Fixes #608 in which setting a quantity value, then setting the rest of the quantity fields in a succeeding line would overwrite the previously set value.  

Patch:
diff --git a/src/fhirtypes/common.ts b/src/fhirtypes/common.ts
index a40d421..5ea3fb0 100644
--- a/src/fhirtypes/common.ts
+++ b/src/fhirtypes/common.ts
@@ -198,6 +198,18 @@ export function setPropertyOnInstance(
           if (current[key] == null) current[key] = {};
           current = current[key];
         } else {
+          // Check if the instance already has the element being defined
+          if (current[key] != null && typeof current[key] === 'object') {
+            // Check if the instance already has a quantity element
+            // Quantity elements are the only FHIR types with both a code and value
+            if (current[key].hasOwnProperty('value') && assignedValue.hasOwnProperty('code')) {
+              // Ensure that the existing value is not being overwritten
+              assignedValue = {
+                value: current[key].value,
+                ...assignedValue
+              };
+            }
+          }
           current[key] = assignedValue;
         }
       }


Test patch:
diff --git a/test/export/InstanceExporter.test.ts b/test/export/InstanceExporter.test.ts
index 8f903da..252f03c 100644
--- a/test/export/InstanceExporter.test.ts
+++ b/test/export/InstanceExporter.test.ts
@@ -2217,6 +2217,41 @@ describe('InstanceExporter', () => {
         });
       });
 
+      it('should not overwrite the value property when assigning a Quantity object', () => {
+        const exObservation = new Profile('ExObservation');
+        exObservation.parent = 'Observation';
+        doc.profiles.set(exObservation.name, exObservation);
+
+        const onlyRule = new OnlyRule('value[x]');
+        onlyRule.types = [{ type: 'Quantity' }];
+        exObservation.rules.push(onlyRule);
+
+        // * valueQuantity.value = 17
+        const valueSettingRule = new AssignmentRule('valueQuantity.value');
+        valueSettingRule.value = 17;
+        valueSettingRule.isInstance = false;
+        valueSettingRule.exactly = false;
+
+        // * valueQuantity = UCUM#/min
+        const codeSettingRule = new AssignmentRule('valueQuantity');
+        codeSettingRule.value = new FshCode('mg', 'http://unitsofmeasure.org', 'mg');
+        codeSettingRule.isInstance = false;
+        codeSettingRule.exactly = false;
+
+        const exInstance = new Instance('ExInstance');
+        exInstance.instanceOf = 'ExObservation';
+        exInstance.rules.push(valueSettingRule);
+        exInstance.rules.push(codeSettingRule);
+
+        const exported = exportInstance(exInstance);
+        expect(exported.valueQuantity).toEqual({
+          value: 17,
+          code: 'mg',
+          system: 'http://unitsofmeasure.org',
+          unit: 'mg'
+        });
+      });
+
       it('should assign an inline instance of a profile of a type to an instance', () => {
         const inlineSimple = new Instance('MySimple');
         inlineSimple.instanceOf = 'SimpleQuantity';