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

Warn when reference values do not resolve and have incorrect format #1468

Merged
merged 7 commits into from
Jun 27, 2024
10 changes: 10 additions & 0 deletions src/fhirtypes/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,16 @@ export function replaceReferences<T extends AssignmentRule | CaretValueRule>(
clone = cloneDeep(rule);
(clone.value as FshReference).reference = value.reference.slice(0, firstPipe);
clone = replaceReferences(clone, tank, fisher);
} else if (type == null && id == null && value.reference) {
// if the reference to an entity is provided but is unable to be resolved
if (!value.reference.startsWith('urn:')) {
if (!value.reference.includes('/')) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way this logic is written with the nested if statements makes me think there's something else we want to do if the value starts with urn: but does contain a /. I think it would be simpler to just have one if statement with both conditions combined with "and" (i.e. !value.reference.startsWith('urn:') && !value.reference.includes('/')). While that doesn't change the logic as it currently is, I think it makes the intent a little clearer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or there is, of course, the equivalent inverse expression: !(value.reference.startsWith('urn:') || value.reference.includes('/')). Take your pick!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

De Morgan would be proud of this comment.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right. That's his name!

logger.warn(
`Cannot find the entity referenced at ${value.reference}. The provided reference value will be used, but this reference does not conform to the FHIR Reference format.`,
rule.sourceInfo
);
}
}
}
}
} else if (value instanceof FshCode) {
Expand Down
79 changes: 78 additions & 1 deletion test/export/InstanceExporter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5486,6 +5486,7 @@ describe('InstanceExporter', () => {
expect(exported.managingOrganization).toEqual({
reference: 'Organization/org-id'
});
expect(loggerSpy.getAllMessages('warn')).toHaveLength(0);
});

it('should assign a reference while resolving the Instance of a profile being referred to', () => {
Expand Down Expand Up @@ -5533,6 +5534,82 @@ describe('InstanceExporter', () => {
]);
});

it('should log warning when reference values do not resolve and is not an absolute or relative URL', () => {
// * target = Reference(exampleReferenceUnableToBeResolved)
const assignedRefRule = new AssignmentRule('target');
assignedRefRule.value = new FshReference('exampleReferenceUnableToBeResolved');
provenanceInstance.rules.push(assignedRefRule);
const exported = exportInstance(provenanceInstance);
expect(exported.target).toEqual([
{
reference: 'exampleReferenceUnableToBeResolved'
}
]);
expect(loggerSpy.getAllMessages('warn')).toHaveLength(1);
expect(loggerSpy.getLastMessage('warn')).toMatch(
'Cannot find the entity referenced at exampleReferenceUnableToBeResolved. The provided reference value will be used, but this reference does not conform to the FHIR Reference format.'
);
});
it('should not log warning when reference values do not resolve and is a UUID or OID', () => {
// * target = Reference(urn:uuid:exampleReference)
const assignedRefRule = new AssignmentRule('target');
assignedRefRule.value = new FshReference('urn:uuid:exampleReference');
provenanceInstance.rules.push(assignedRefRule);
const exported = exportInstance(provenanceInstance);
expect(exported.target).toEqual([
{
reference: 'urn:uuid:exampleReference'
}
]);
expect(loggerSpy.getAllMessages('warn')).toHaveLength(0);
});

it('should not log warning when reference values do not resolve and is a relative URL with correct number of parts', () => {
// * target = Reference(UnresolvableType/exampleReferenceUnableToBeResolved)
const assignedRefRule = new AssignmentRule('target');
assignedRefRule.value = new FshReference(
'UnresolvableType/exampleReferenceUnableToBeResolved'
);
provenanceInstance.rules.push(assignedRefRule);
const exported = exportInstance(provenanceInstance);
expect(exported.target).toEqual([
{
reference: 'UnresolvableType/exampleReferenceUnableToBeResolved'
}
]);
expect(loggerSpy.getAllMessages('warn')).toHaveLength(0);
});

it('should not log warning when reference values do not resolve and is a relative URL but has more than two parts', () => {
// * target = Reference(More/Than/Two/Parts/exampleReferenceUnableToBeResolved)
const assignedRefRule = new AssignmentRule('target');
assignedRefRule.value = new FshReference(
'More/Than/Two/Parts/exampleReferenceUnableToBeResolved'
);
provenanceInstance.rules.push(assignedRefRule);
const exported = exportInstance(provenanceInstance);
expect(exported.target).toEqual([
{
reference: 'More/Than/Two/Parts/exampleReferenceUnableToBeResolved'
}
]);
expect(loggerSpy.getAllMessages('warn')).toHaveLength(0);
});

it('should not log warning when reference values are an absolute URL', () => {
// * target = Reference(http://foo.org/fhir/Patient/abc)
const assignedRefRule = new AssignmentRule('target');
assignedRefRule.value = new FshReference('http://foo.org/fhir/Patient/abc');
provenanceInstance.rules.push(assignedRefRule);
const exported = exportInstance(provenanceInstance);
expect(exported.target).toEqual([
{
reference: 'http://foo.org/fhir/Patient/abc'
}
]);
expect(loggerSpy.getAllMessages('warn')).toHaveLength(0);
});

it('should assign a reference leaving the full profile URL when it is specified', () => {
const assignedRefRule = new AssignmentRule('target');
assignedRefRule.value = new FshReference(
Expand Down Expand Up @@ -10190,7 +10267,7 @@ describe('InstanceExporter', () => {
}
]
});
expect(loggerSpy.getAllLogs('warn')).toBeEmpty();
expect(loggerSpy.getAllMessages('warn')).toHaveLength(0);
expect(loggerSpy.getAllLogs('error')).toBeEmpty();
});

Expand Down
Loading