-
Notifications
You must be signed in to change notification settings - Fork 302
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
Change validateQuestionnaireResponseItems to allow questionnaire response with missing items #926
Merged
Merged
Changes from 37 commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
090d8bd
Change validation to check input questionnaire response and the quest…
joiskash 41818c4
Comment tests to handle case 1. Input questionnaire response has more…
joiskash af4a512
Handled case where questionnaireResponse that comes via intent has mo…
joiskash b5aabf9
Merge branch 'master' into kj/validation-fix-questionnaireResponse
joiskash 724a643
Merge branch 'master' into kj/validation-fix-questionnaireResponse
joiskash 21e37ec
ran spotless apply
joiskash 3283fdc
Merge branch 'kj/validation-fix-questionnaireResponse' of https://git…
joiskash 39aaf4f
Modified validateQuestionnaireResponse to also check type of answer f…
joiskash 78af019
Added unit tests and change less answer unit test to not throw error
joiskash 90ca73e
Fixed bug which caused questionnaire response to always be null
joiskash 0a5d906
Merge branch 'master' into kj/validation-fix-questionnaireResponse
joiskash a3da5e6
Merge branch 'master' into kj/validation-fix-questionnaireResponse
joiskash 71c46fa
Merge branch 'master' into kj/validation-fix-questionnaireResponse
joiskash 127b062
Merge branch 'master' into kj/validation-fix-questionnaireResponse
joiskash 8c7cd80
Moved validate questionnaire to QuestionnaireResponseValidator.kt and…
joiskash 82857a1
Merge branch 'master' into kj/validation-fix-questionnaireResponse
joiskash b42d79a
Changed order of parameters to keep it the same as before
joiskash c38fd17
Reverted to the old way of initializing questionnaire response and ad…
joiskash d816628
Merge branch 'kj/validation-fix-questionnaireResponse' of https://git…
joiskash 62473cb
Merge branch 'master' into kj/validation-fix-questionnaireResponse
joiskash c782933
Change unit test for input questionnaire response with missing items
joiskash 482d1f0
Merge branch 'kj/validation-fix-questionnaireResponse' of https://git…
joiskash dcbd36a
Ran spotlessApply
joiskash 7204dee
Merge branch 'master' into kj/validation-fix-questionnaireResponse
joiskash 8a92d03
Bumped up test coverage
joiskash 786872e
Merge branch 'kj/validation-fix-questionnaireResponse' of https://git…
joiskash 854710d
Merge branch 'master' into kj/validation-fix-questionnaireResponse
joiskash 24d9196
Merged changes from PR #937 null check added to validateQuestionnaire…
joiskash 47b38ed
Use value "type" that has passed the null check instead of using ques…
joiskash 87ff511
Merge branch 'master' into kj/validation-fix-questionnaireResponse
joiskash 2280128
Merge branch 'master' into kj/validation-fix-questionnaireResponse
joiskash 6d13df6
Using flatMapIndexed instead of asIndexed and flatMap. Using == inste…
joiskash 0c371a3
Using require and == instead of if throws and .equals
joiskash dbd9c93
Changed mutable list to list
joiskash 133a797
Merge branch 'master' into kj/validation-fix-questionnaireResponse
joiskash 619c5ec
Merge branch 'master' into kj/validation-fix-questionnaireResponse
joiskash 4a860fb
Name change to validate function and extraction of private helper fun…
joiskash 2a6bf28
Merge branch 'master' into kj/validation-fix-questionnaireResponse
jingtang10 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,7 @@ object QuestionnaireResponseValidator { | |
* Validates [questionnaireResponseItemList] using the constraints defined in the | ||
* [questionnaireItemList]. | ||
*/ | ||
fun validate( | ||
fun validateQuestionnaireResponseAnswers( | ||
questionnaireItemList: List<Questionnaire.QuestionnaireItemComponent>, | ||
questionnaireResponseItemList: List<QuestionnaireResponse.QuestionnaireResponseItemComponent>, | ||
context: Context | ||
|
@@ -53,10 +53,104 @@ object QuestionnaireResponseValidator { | |
) | ||
if (questionnaireItem.hasNestedItemsWithinAnswers) { | ||
// TODO(https://github.com/google/android-fhir/issues/487): Validates all answers. | ||
validate(questionnaireItem.item, questionnaireResponseItem.answer[0].item, context) | ||
validateQuestionnaireResponseAnswers( | ||
questionnaireItem.item, | ||
questionnaireResponseItem.answer[0].item, | ||
context | ||
) | ||
} | ||
validate(questionnaireItem.item, questionnaireResponseItem.item, context) | ||
validateQuestionnaireResponseAnswers( | ||
questionnaireItem.item, | ||
questionnaireResponseItem.item, | ||
context | ||
) | ||
} | ||
return linkIdToValidationResultMap | ||
} | ||
|
||
/** | ||
* Traverse (DFS) through the [Questionnaire.item] list and the [QuestionnaireResponse.item] list | ||
* to check if the linkId of the matching pairs of questionnaire item and questionnaire response | ||
* item are equal. | ||
*/ | ||
fun validateQuestionnaireResponseStructure( | ||
questionnaire: Questionnaire, | ||
questionnaireResponse: QuestionnaireResponse | ||
) { | ||
validateQuestionnaireResponseItemsStructurally(questionnaire.item, questionnaireResponse.item) | ||
} | ||
|
||
private fun validateQuestionnaireResponseItemsStructurally( | ||
questionnaireItemList: List<Questionnaire.QuestionnaireItemComponent>, | ||
questionnaireResponseInputItemList: | ||
List<QuestionnaireResponse.QuestionnaireResponseItemComponent>, | ||
) { | ||
val questionnaireResponseInputItemListIterator = questionnaireResponseInputItemList.iterator() | ||
val questionnaireItemListIterator = questionnaireItemList.iterator() | ||
|
||
while (questionnaireResponseInputItemListIterator.hasNext()) { | ||
// TODO: Validate type and item nesting within answers for repeated answers | ||
// https://github.com/google/android-fhir/issues/286 | ||
val questionnaireResponseInputItem = questionnaireResponseInputItemListIterator.next() | ||
if (questionnaireItemListIterator.hasNext()) { | ||
val questionnaireItem = questionnaireItemListIterator.next() | ||
require(questionnaireItem.linkId == questionnaireResponseInputItem.linkId) { | ||
"Mismatching linkIds for questionnaire item ${questionnaireItem.linkId} and " + | ||
"questionnaire response item ${questionnaireResponseInputItem.linkId}" | ||
} | ||
val type = checkNotNull(questionnaireItem.type) { "Questionnaire item must have type" } | ||
if (questionnaireResponseInputItem.hasAnswer() && | ||
type != Questionnaire.QuestionnaireItemType.GROUP | ||
) { | ||
if (!questionnaireItem.repeats && questionnaireResponseInputItem.answer.size > 1) { | ||
throw IllegalArgumentException( | ||
"Multiple answers in ${questionnaireResponseInputItem.linkId} and repeats false in " + | ||
"questionnaire item ${questionnaireItem.linkId}" | ||
) | ||
} | ||
questionnaireResponseInputItem.answer.forEachIndexed { | ||
index, | ||
Comment on lines
+111
to
+112
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Looks like the index is not needed, could this become There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will take care of it in my next PR. Thanks! |
||
questionnaireResponseItemAnswerComponent -> | ||
if (questionnaireResponseItemAnswerComponent.hasValue()) { | ||
when (type) { | ||
Questionnaire.QuestionnaireItemType.BOOLEAN, | ||
Questionnaire.QuestionnaireItemType.DECIMAL, | ||
Questionnaire.QuestionnaireItemType.INTEGER, | ||
Questionnaire.QuestionnaireItemType.DATE, | ||
Questionnaire.QuestionnaireItemType.DATETIME, | ||
Questionnaire.QuestionnaireItemType.TIME, | ||
Questionnaire.QuestionnaireItemType.STRING, | ||
Questionnaire.QuestionnaireItemType.URL -> | ||
if (!questionnaireResponseItemAnswerComponent | ||
.value | ||
.fhirType() | ||
.equals(type.toCode()) | ||
) { | ||
throw IllegalArgumentException( | ||
"Type mismatch for linkIds for questionnaire item ${questionnaireItem.linkId} and " + | ||
"questionnaire response item ${questionnaireResponseInputItem.linkId}" | ||
) | ||
} | ||
else -> Unit // Check type for primitives only | ||
} | ||
} | ||
validateQuestionnaireResponseItemsStructurally( | ||
questionnaireItem.item, | ||
questionnaireResponseItemAnswerComponent.item | ||
) | ||
} | ||
} else if (questionnaireResponseInputItem.hasItem()) { | ||
validateQuestionnaireResponseItemsStructurally( | ||
questionnaireItem.item, | ||
questionnaireResponseInputItem.item | ||
) | ||
} | ||
} else { | ||
// Input response has more items | ||
throw IllegalArgumentException( | ||
"No matching questionnaire item for questionnaire response item ${questionnaireResponseInputItem.linkId}" | ||
) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If these two need to have the same length (which it looks like they do, as we throw an error if they don't on line 137), I would recommend using
zip
and doing something like this:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They don't have to be of the same length. The questionnaire response may not have some items . This can happen if the item was disabled / not answered. That's why i removed the zip from the previous implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error we throw on line 137 is to make sure the questionnaire response does not have more items than the questionnaire. But the questionnaire response can have less number of items. Thats why I am iterating over questionnaireResponseList and using the index to fetch the corresponding questionnaire item. You will find more details regarding this in #836 . Please let me know if it does not make sense.