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

ValueSet self reference #1503

Merged
merged 8 commits into from
Sep 18, 2024
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
12 changes: 11 additions & 1 deletion src/export/ValueSetExporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ export class ValueSetExporter {
composeElement.valueSet = component.from.valueSets.map(vs => {
return this.fisher.fishForMetadata(vs, Type.ValueSet)?.url ?? vs;
});
composeElement.valueSet = composeElement.valueSet.filter(vs => {
if (vs == valueSet.url) {
logger.error(
`Value set with id ${valueSet.id} has component rule with self-referencing value set (by id, value set name, or url). Removing self-reference.`
);
}
return vs != valueSet.url;
});
composeElement.valueSet.forEach(vs => {
// Canonical URI may include | to specify version: https://www.hl7.org/fhir/references.html#canonical
if (!isUri(vs.split('|')[0])) {
Expand Down Expand Up @@ -176,7 +184,9 @@ export class ValueSetExporter {
this.addConceptComposeElement(composeElement, valueSet.compose.include);
}
} else {
valueSet.compose.include.push(composeElement);
if (composeElement.valueSet?.length !== 0 || composeElement.system != undefined) {
valueSet.compose.include.push(composeElement);
}
}
} else {
this.addConceptComposeElement(composeElement, valueSet.compose.exclude);
Expand Down
51 changes: 51 additions & 0 deletions test/export/ValueSetExporter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,57 @@ describe('ValueSetExporter', () => {
});
});

it('should remove and log error when exporting a value set that includes a component from a self referencing value set', () => {
const valueSet = new FshValueSet('DinnerVS');
valueSet.id = 'dinner-vs';
const component = new ValueSetConceptComponentRule(true);
component.from = {
system: 'http://food.org/food1',
valueSets: [
'http://food.org/food/ValueSet/hot-food',
'http://food.org/food/ValueSet/cold-food',
'DinnerVS',
'http://hl7.org/fhir/us/minimal/ValueSet/dinner-vs',
'dinner-vs'
]
};
const component2 = new ValueSetConceptComponentRule(true);
component2.from = {
system: 'http://food.org/food2',
valueSets: ['DinnerVS', 'http://hl7.org/fhir/us/minimal/ValueSet/dinner-vs', 'dinner-vs']
};
valueSet.rules.push(component);
valueSet.rules.push(component2);
doc.valueSets.set(valueSet.name, valueSet);
const exported = exporter.export().valueSets;
expect(exported.length).toBe(1);
expect(exported[0]).toEqual({
resourceType: 'ValueSet',
id: 'dinner-vs',
name: 'DinnerVS',
url: 'http://hl7.org/fhir/us/minimal/ValueSet/dinner-vs',
status: 'draft',
compose: {
include: [
{
system: 'http://food.org/food1',
valueSet: [
'http://food.org/food/ValueSet/hot-food',
'http://food.org/food/ValueSet/cold-food'
]
},
{
system: 'http://food.org/food2'
}
]
}
});
expect(loggerSpy.getAllMessages('error')).toHaveLength(6);
expect(loggerSpy.getLastMessage('error')).toBe(
'Value set with id dinner-vs has component rule with self-referencing value set (by id, value set name, or url). Removing self-reference.'
);
});

it('should export a value set that includes a concept component with at least one concept', () => {
const valueSet = new FshValueSet('DinnerVS');
const component = new ValueSetConceptComponentRule(true);
Expand Down
Loading