Repository: FHIR/sushi (ts)

Issue:
Allow setting slicing rules for Extension value[x]
With this FSH
```
Extension: TestExtension
* value[x] only CodeableConcept
* value[x] ^slicing.rules = #closed
* valueCodeableConcept from account-type (required)
```
The `Extension.value[x]` section comes out as
```json
{
    "id": "Extension.value[x]",
    "path": "Extension.value[x]",
    "slicing": {
        "discriminator": [
        {
            "type": "type",
            "path": "$this"
        }
        ],
        "ordered": false,
        "rules": "open"
    },
    "type": [
        {
        "code": "CodeableConcept"
        }
    ]
}
```

The `slicing.rules` appears to be ignored, and I can't seem to see any way to set this? Same issue for `slicing.ordered`.

Pull request description:
Fix bug with slicing rules on value[x]
This fixes #564 by preserving any existing slicing information (`ordered` or `rules`) on a value[x] element when that value[x] element is implicitly sliced by the creation of something like a `valueQuantity` element.

Patch:
diff --git a/src/fhirtypes/StructureDefinition.ts b/src/fhirtypes/StructureDefinition.ts
index 49cd55e..031f557 100644
--- a/src/fhirtypes/StructureDefinition.ts
+++ b/src/fhirtypes/StructureDefinition.ts
@@ -672,7 +672,12 @@ export class StructureDefinition {
         // NOTE: The spec is somewhat inconsistent on handling choice slicing, we decided on this
         // approach per consistency with 4.0.1 observation-vitalsigns profiles and per this post
         // https://blog.fire.ly/2019/09/13/type-slicing-in-fhir-r4/.
-        matchingXElement.sliceIt('type', '$this', false, 'open');
+        matchingXElement.sliceIt(
+          'type',
+          '$this',
+          matchingXElement.slicing?.ordered,
+          matchingXElement.slicing?.rules
+        );
         // Get the sliceName for the new element
         const newSlice = matchingXElement.addSlice(sliceName, matchingType);
         return newSlice;


Test patch:
diff --git a/test/fhirtypes/StructureDefinition.test.ts b/test/fhirtypes/StructureDefinition.test.ts
index c0f551e..6c23834 100644
--- a/test/fhirtypes/StructureDefinition.test.ts
+++ b/test/fhirtypes/StructureDefinition.test.ts
@@ -684,6 +684,21 @@ describe('StructureDefinition', () => {
       expect(valueQuantity.min).toBe(0);
       expect(valueX.slicing).toBeDefined();
       expect(valueX.slicing.discriminator[0]).toEqual({ type: 'type', path: '$this' });
+      expect(valueX.slicing.ordered).toBe(false);
+      expect(valueX.slicing.rules).toBe('open');
+      expect(observation.elements.length).toBe(originalLength + 1);
+    });
+
+    it('should preserve existing slicing when making a non-existent choice element explicit', () => {
+      const originalLength = observation.elements.length;
+      const valueX = observation.findElementByPath('value[x]', fisher);
+      expect(valueX.slicing).toBeUndefined();
+      valueX.slicing = { ordered: true, rules: 'closed' };
+      observation.findElementByPath('valueQuantity', fisher);
+      expect(valueX.slicing).toBeDefined();
+      expect(valueX.slicing.discriminator[0]).toEqual({ type: 'type', path: '$this' });
+      expect(valueX.slicing.ordered).toBe(true);
+      expect(valueX.slicing.rules).toBe('closed');
       expect(observation.elements.length).toBe(originalLength + 1);
     });