Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

fix: GenericResponse response must exist if sub properties tested. #54

Merged
merged 2 commits into from
Feb 16, 2021

Conversation

AllanHodkinson
Copy link
Contributor

Issue #, if available:
#53

Description of changes:
Add check that optional response exists before checking sub properties of it.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

…se exists before testing for sub properties.
Copy link
Contributor

@carvantes carvantes left a comment

Choose a reason for hiding this comment

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

Thanks for the PR.

The change looks good. Looks like your branch is now out-of-date with mainline. I can merge this after you sync your branch with manline.

Comment on lines +39 to +51
const response = await this.handler.read(resourceType, id);
const updatedReadResponse = await this.authService.authorizeAndFilterReadResponse({
operation: 'read',
userIdentity: res.locals.userIdentity,
readResponse: response,
});
if (updatedReadResponse && updatedReadResponse.meta) {
res.set({
ETag: `W/"${updatedReadResponse.meta.versionId}"`,
'Last-Modified': updatedReadResponse.meta.lastUpdated,
});
}
res.send(updatedReadResponse);
Copy link
Contributor

Choose a reason for hiding this comment

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

I agree that since we are using any for some return values, type safety cannot be guaranteed and this kind of additional checks are warranted. But, have this caused runtime errors on your side? I don't think that this.authService.authorizeAndFilterReadResponse can actually return null or undefined, even if the typing allows it (but I haven't tested extensively)

The change on L45 only matters if updatedReadResponse is null or undefined and it prevents the code from crashing on L45. But then an undefined updatedReadResponse would be sent as response on L51 and that would break things elsewhere. This change looks good, some future work for us would be to better handle undefined responses or to narrow down our typescript types if undefined responses don't occur to begin with.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We've only seen runtime errors for the 'create' operation case when not returning a response body. I just filled in the cases for other operations returning a GenericResponse type without too much thought!

I agree that this is an obscure corner case - by design & the FHIR spec the persistence component read method should either return a resource or should throw a ResourceNotFoundError. As you say future work might be to update the read interface to always require a resource.

In our use case we have different people implementing persistence components, with varying degrees of FHIR & developer knowledge, so even though it is wrong, there is a chance that one of them would actually fail to set a resource within the GenericResponse. If we are using the stubbed passThroughAuthz component for authorizeAndFilterReadResponse, that would return the undefined readResponse into this code and trigger the bug. I tested the code on L51 if updatedReadResponse is undefined and it is handled ok. I expect this is dealt with somewhere in Express.

@carvantes carvantes merged commit e1740b5 into awslabs:mainline Feb 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants