Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unfold choice type slices when no unfolded elements match #1373

Merged
merged 1 commit into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/fhirtypes/StructureDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ export class StructureDefinition {
const parsedPath = parseFSHPath(path);

let fhirPathString = this.pathType;
let previousPathPart = '';
let matchingElements = this.elements;
let newMatchingElements: ElementDefinition[] = [];
// Iterate over the path, filtering out elements that do not match
Expand All @@ -241,6 +242,14 @@ export class StructureDefinition {
if (unfoldedElements.length > 0) {
// Only get the children that match our path
newMatchingElements = unfoldedElements.filter(e => e.path.startsWith(fhirPathString));
// If none of those matched, try unfolding the choice elements
if (
newMatchingElements.length === 0 &&
matchingElements[0].id.endsWith(`[x]:${previousPathPart}`)
) {
unfoldedElements = matchingElements[0].unfoldChoiceElementTypes(fisher);
newMatchingElements = unfoldedElements.filter(e => e.path.startsWith(fhirPathString));
}
} else if (matchingElements[0].id.endsWith('[x]')) {
unfoldedElements = matchingElements[0].unfoldChoiceElementTypes(fisher);
newMatchingElements = unfoldedElements.filter(e => e.path.startsWith(fhirPathString));
Expand Down Expand Up @@ -312,6 +321,7 @@ export class StructureDefinition {
);
});
}
previousPathPart = pathPart.base;
}

// We could still have child elements that are matching, if so filter them out now
Expand Down
23 changes: 23 additions & 0 deletions test/fhirtypes/StructureDefinition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,29 @@ describe('StructureDefinition', () => {
expect(observation.elements.length).toBe(originalLength + 8);
});

it('should make explicit a non-existing choice element that must be unfolded when finding it by a type-specific path to a child element', () => {
// NOTE: MedicationStatement-uv-ips profile has some child elements on effective[x]
// but not the child elements of the choiceTypes so need to unfold in order to find those
const ipsMedicationStatement = fisher.fishForStructureDefinition(
'MedicationStatement-uv-ips'
);
const originalLength = ipsMedicationStatement.elements.length;
const effectiveX = ipsMedicationStatement.findElementByPath('effective[x]', fisher);
expect(effectiveX.slicing).toBeUndefined();
const effectivePeriodStart = ipsMedicationStatement.findElementByPath(
'effectivePeriod.start',
fisher
);
expect(effectivePeriodStart).toBeDefined();
expect(effectivePeriodStart.id).toBe(
'MedicationStatement.effective[x]:effectivePeriod.start'
);
expect(effectivePeriodStart.path).toBe('MedicationStatement.effective[x].start');
expect(effectiveX.slicing).toBeDefined();
expect(effectiveX.slicing.discriminator[0]).toEqual({ type: 'type', path: '$this' });
expect(ipsMedicationStatement.elements.length).toBe(originalLength + 7); // unfolded effectivePeriod + it's 6 child elements
});

it('should find an already existing explicit choice element with slicing syntax', () => {
const originalLength = respRate.elements.length;
const valueQuantity = respRate.findElementByPath('value[x][valueQuantity]', fisher);
Expand Down
Loading
Loading