Skip to content

Commit

Permalink
FHIRPath constant value null casting bug fix (#2012)
Browse files Browse the repository at this point in the history
* Add null check for the retrieved value from a constant

* Add new test and rename tests

* Use safe casting

* Use = instead of lambda

---------

Co-authored-by: Francis Odhiambo Otieno <[email protected]>
  • Loading branch information
FikriMilano and f-odhiambo committed Jun 7, 2023
1 parent f787d5b commit e322dd4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ import org.hl7.fhir.r4.utils.FHIRPathEngine
* Resolves constants defined in the fhir path expressions beyond those defined in the specification
*/
internal object FHIRPathEngineHostServices : FHIRPathEngine.IEvaluationContext {
override fun resolveConstant(appContext: Any?, name: String?, beforeContext: Boolean): Base? {
return if (appContext is Map<*, *> && appContext.containsKey(name)) appContext[name] as Base
else null
}
override fun resolveConstant(appContext: Any?, name: String?, beforeContext: Boolean): Base? =
(appContext as? Map<*, *>)?.get(name) as? Base

override fun resolveConstantType(appContext: Any?, name: String?): TypeDetails {
throw UnsupportedOperationException()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,26 @@ import org.junit.Test
class FHIRPathEngineHostServicesTest {

@Test
fun testFHIRPathHostServices_resolveConstantValueNotPresent_returnsNull() {
fun testFHIRPathHostServices_resolveConstantKeyNotPresent_returnsNull() {
val answer = FHIRPathEngineHostServices.resolveConstant(mapOf("A" to IntegerType(1)), "B", true)

assertThat(answer).isNull()
}

@Test
fun testFHIRPathHostServices_resolveConstantValuePresent_returnsNotNull() {
fun testFHIRPathHostServices_resolveConstantKeyAndValuePresent_returnsNotNull() {
val answer = FHIRPathEngineHostServices.resolveConstant(mapOf("A" to IntegerType(1)), "A", true)

assertThat((answer as Type).asStringValue()).isEqualTo("1")
}

@Test
fun testFHIRPathHostServices_resolveConstantKeyPresentAndValueNotPresent_returnsNull() {
val answer = FHIRPathEngineHostServices.resolveConstant(mapOf("A" to null), "A", true)

assertThat(answer).isNull()
}

@Test
fun testFHIRPathHostServices_resolveConstantNullAppContext_returnsNull() {
val answer = FHIRPathEngineHostServices.resolveConstant(null, "A", true)
Expand Down

0 comments on commit e322dd4

Please sign in to comment.