Skip to content

Commit

Permalink
Support fishing for profiles with a version (#1167)
Browse files Browse the repository at this point in the history
* Add tests to check that version is not used and fall back to type if profile is not available

* Add functionality so tests pass

* Add warnings when profile with mismatched version is found and when falling back to type
  • Loading branch information
jafeltra authored Nov 17, 2022
1 parent e761187 commit 496a294
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
24 changes: 22 additions & 2 deletions src/fhirtypes/ElementDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2278,13 +2278,33 @@ export class ElementDefinition {
if (newElements.length === 0) {
// If we have exactly one profile to use, use that, otherwise use the code
const type = profileToUse ?? this.type[0].code;
const json = fisher.fishForFHIR(
type,
// There could possibly be a |version appended to the type, so don't include it while fishing
const [typeWithoutVersion, version] = type.split('|', 2);
let json = fisher.fishForFHIR(
typeWithoutVersion,
Type.Resource,
Type.Type,
Type.Profile,
Type.Extension
);
if (json && version != null && json.version != null && json.version !== version) {
logger.warn(
`${type} is based on ${typeWithoutVersion} version ${version}, but SUSHI found version ${json.version}`
);
}
if (!json && profileToUse) {
logger.warn(
`SUSHI tried to find profile ${type} but could not find it and instead will try to use ${this.type[0].code}`
);
// If we tried to fish based on a profile and didn't find anything, fall back to the type
json = fisher.fishForFHIR(
this.type[0].code,
Type.Resource,
Type.Type,
Type.Profile,
Type.Extension
);
}
if (json) {
const def = StructureDefinition.fromJSON(json);
if (def.inProgress) {
Expand Down
37 changes: 36 additions & 1 deletion test/fhirtypes/ElementDefinition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { loadFromPath } from '../../src/fhirdefs/load';
import { FHIRDefinitions } from '../../src/fhirdefs/FHIRDefinitions';
import { ElementDefinition, ElementDefinitionType } from '../../src/fhirtypes/ElementDefinition';
import { StructureDefinition } from '../../src/fhirtypes/StructureDefinition';
import { TestFisher } from '../testhelpers';
import { loggerSpy, TestFisher } from '../testhelpers';
import { Type } from '../../src/utils/Fishable';
import { Invariant, FshCode } from '../../src/fshtypes';
import path from 'path';
Expand Down Expand Up @@ -36,6 +36,7 @@ describe('ElementDefinition', () => {
valueId = ElementDefinition.fromJSON(jsonValueId);
valueX.structDef = observation;
valueId.structDef = observation;
loggerSpy.reset();
});

describe('#fromJSON', () => {
Expand Down Expand Up @@ -733,6 +734,40 @@ describe('ElementDefinition', () => {
);
expect(fooSliceExtensionTest.calculateDiff().type).toEqual(extensionSlice.type);
});

it('should unfold an element with a profile that includes a version', () => {
const referenceRange = observation.elements.find(
e => e.id === 'Observation.referenceRange.low'
) as ElementDefinition; // The element will exist based on the test fixtures

// Set type with a profile that has a version number
referenceRange.type[0].profile = [
'http://hl7.org/fhir/StructureDefinition/SimpleQuantity|1.0.0'
];

const newElements = referenceRange.unfold(fisher);
expect(newElements).toHaveLength(7); // It should unfold elements on referenceRange.low
expect(loggerSpy.getAllMessages('warn')).toHaveLength(1);
expect(loggerSpy.getLastMessage('warn')).toMatch(
/SimpleQuantity|1\.0\.0 is based on SimpleQuantity version 1\.0\.0, but SUSHI found version 4\.0\.1/
);
});

it('should fall back to the type if a profile cannot be fished while unfolding', () => {
const referenceRange = observation.elements.find(
e => e.id === 'Observation.referenceRange.low'
) as ElementDefinition; // The element will exist based on the test fixtures

// Set type with a profile that doesn't exist in our test definitions
referenceRange.type[0].profile = ['http://hl7.org/fhir/StructureDefinition/FakeQuantity'];

const newElements = referenceRange.unfold(fisher);
expect(newElements).toHaveLength(7); // It should unfold elements on referenceRange.low
expect(loggerSpy.getAllMessages('warn')).toHaveLength(1);
expect(loggerSpy.getLastMessage('warn')).toMatch(
/SUSHI tried to find profile .*FakeQuantity but could not find it and instead will try to use Quantity/
);
});
});

describe('#clone', () => {
Expand Down

0 comments on commit 496a294

Please sign in to comment.