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

Fix e.type is not iterable error #107

Merged
merged 2 commits into from
Jan 21, 2020
Merged

Conversation

ngfreiter
Copy link
Contributor

I noticed when running SUSHI that an error would pop up sometimes saying "e.type is not iterable". On an ElementDefinition, type is technically not required, and there were some places we were assuming type would exist as an array. This PR fixes the places where we made those assumptions so that the error doesn't happen.

@@ -472,7 +472,7 @@ export class StructureDefinition {
let matchingType: ElementDefinitionType;
const matchingXElements = elements.filter(e => {
if (e.path.endsWith('[x]')) {
for (const t of e.type) {
for (const t of e.type ?? []) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Good catch! But I notice that you had to do this here, as well as in another location. I imagine we might have to iterate over type in other places in the future, as well.

Is there a reason that we can't just modify the type property itself to be set to an empty array if it isn't otherwise set? That might prevent us from having to catch cases like this in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah that is a good point. I am not sure how to go about setting type though, because we don't want to output JSON with an empty array for type.

Copy link
Member

Choose a reason for hiding this comment

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

My one concern there is that null types are usually something we inherit from the parent. So when we serialize back out to JSON, we'd have to make sure that we don't change what was a null type to an empty type ([]) instead. Might have to put special logic in the serialized to make that happen.

Another possible approach would be to introduce a nullSafeType getter or function and try to always use that instead. But then I guess we need to ask if that's any easier than remembering to do ?? [] as above.

Copy link
Contributor

Choose a reason for hiding this comment

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

Given the issue Chris raises, I would consider rescinding my suggestion. If there's a distinct difference between null and empty for serialization, then getting around that would arguably be more unclear than what we are doing.

@kjmahalingam
Copy link
Contributor

Do we want a test for this now that we can ensure it functions correctly?

@kjmahalingam
Copy link
Contributor

All checks pass for me, and the changes generally seem to work, though I still would like to see if there's anything to address with my comments above.

@ngfreiter
Copy link
Contributor Author

Ok @kjmahalingam I added a test that would fail without this fix, so it shows this is working correctly. Is there a consensus with you and @cmoesel on if I should add extra handling to the type property?

Copy link
Contributor

@kjmahalingam kjmahalingam left a comment

Choose a reason for hiding this comment

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

I'm happy with the test! And I'm fine with leaving the code as-is in terms of styling, given discussion.

@kjmahalingam kjmahalingam merged commit fc8d83b into master Jan 21, 2020
@kjmahalingam kjmahalingam deleted the fix-iterable-type-bug branch January 21, 2020 16:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants