Repository: FHIR/sushi (ts)

Issue:
Sushi is generating arrays for Reference.identifier
Sushi lets me do this to MeasureReport:

* subject.identifier 1..*
* subject.identifier ^short = "The subject shall have at least one identifier which uniquely identifies the subject to the recepient."
* subject.identifier ^comment = "Requiring subject.identifier allows reports to be queried by locations using a well-known identifier."

And then in instances, creates an array for subject.identifier.

But subject.identifier in the base is 0..1

It should barf at me trying to extend beyond cardinality of 1 for subject.identifier


Pull request:
Throw error when widening cardinality to x..*
Fixes #394.

So it looks like we were actually allowing the cardinality to be widened in the case that the cardinality was being widened to `*`. I don't know how this stayed hidden for this long, but it seems like it really did stay hidden for this long. We did have tests of widening the cardinality from a number `x` to a number `y` with `x < y`, but we did not have tests of widening the cardinality from a number to `*`. Now that works, and I added a test of it. You can also test it on the example given in the issue. On master, you will not get an error, on this branch, you will.

Patch:
diff --git a/src/fhirtypes/ElementDefinition.ts b/src/fhirtypes/ElementDefinition.ts
index 8ca323f..33fae85 100644
--- a/src/fhirtypes/ElementDefinition.ts
+++ b/src/fhirtypes/ElementDefinition.ts
@@ -457,7 +457,7 @@ export class ElementDefinition {
     }
 
     // Check to ensure max <= existing max
-    if (this.max != null && this.max !== '*' && maxInt > parseInt(this.max)) {
+    if (this.max != null && this.max !== '*' && (maxInt > parseInt(this.max) || isUnbounded)) {
       throw new WideningCardinalityError(this.min, this.max, min, max);
     }
 


Test patch:
diff --git a/test/fhirtypes/ElementDefinition.constrainCardinality.test.ts b/test/fhirtypes/ElementDefinition.constrainCardinality.test.ts
index 306f331..4fac116 100644
--- a/test/fhirtypes/ElementDefinition.constrainCardinality.test.ts
+++ b/test/fhirtypes/ElementDefinition.constrainCardinality.test.ts
@@ -148,6 +148,16 @@ describe('ElementDefinition', () => {
       expect(clone).toEqual(status);
     });
 
+    it('should throw WideningCardinalityError when max is * and original max is not *', () => {
+      const status = observation.elements.find(e => e.id === 'Observation.status');
+      const clone = cloneDeep(status);
+      expect(() => {
+        // constrain 1..1 to 1..*
+        clone.constrainCardinality(1, '*');
+      }).toThrow(/1..\* is wider than 1..1\./);
+      expect(clone).toEqual(status);
+    });
+
     // Slice Handling
     it('should update sliced element min when sum of slice mins is constrainted greater than it', () => {
       const category = respRate.elements.find(e => e.id === 'Observation.category');