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: Issue in the extension URL extracted using profile loader #2475

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 Google LLC
* Copyright 2023-2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -116,7 +116,7 @@ class ResourceMapperInstrumentedTest {
val dateTimeType =
patient
.getExtensionByUrl(
"http://fhir.org/guides/who/core/StructureDefinition/who-patient#Patient.birthTime",
"http://hl7.org/fhir/StructureDefinition/patient-birthTime",
)
.value as DateTimeType
val expectedDateTimeType = DateTimeType("2022-02-07T13:28:17-05:00")
Expand All @@ -138,7 +138,7 @@ class ResourceMapperInstrumentedTest {
assertThat(patient).isNotNull()
assertThat(
patient.getExtensionByUrl(
"http://fhir.org/guides/who/core/StructureDefinition/who-patient#Patient.birthTime",
"http://hl7.org/fhir/StructureDefinition/patient-birthTime",
),
)
.isEqualTo(null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import org.hl7.fhir.r4.model.CodeableConcept
import org.hl7.fhir.r4.model.Coding
import org.hl7.fhir.r4.model.DecimalType
import org.hl7.fhir.r4.model.DomainResource
import org.hl7.fhir.r4.model.ElementDefinition
import org.hl7.fhir.r4.model.Enumeration
import org.hl7.fhir.r4.model.Expression
import org.hl7.fhir.r4.model.Extension
Expand Down Expand Up @@ -476,41 +477,46 @@ object ResourceMapper {
questionnaireItem.definition.lastIndexOf("#") + 1,
questionnaireItem.definition.lastIndexOf("."),
)
if (
isExtensionSupportedByProfile(
val extensionElementDefinition =
getExtensionElementDefinitionOrNull(
structureDefinition = it,
extensionForType = extensionForType,
fieldName = fieldName,
)
) {
addDefinitionBasedCustomExtension(questionnaireItem, questionnaireResponseItem, base)
return
} else {
if (extensionElementDefinition == null) {
Timber.w(
"Extension for field '$fieldName' is not defined in StructureDefinition of ${base.fhirType()}, so field is ignored",
)
} else {
addDefinitionBasedCustomExtension(
questionnaireItem,
questionnaireResponseItem,
base,
fieldName,
extensionElementDefinition,
)
}
}
}
}
}

private fun isExtensionSupportedByProfile(
private fun getExtensionElementDefinitionOrNull(
structureDefinition: StructureDefinition,
extensionForType: String,
fieldName: String,
): Boolean {
): ElementDefinition? {
// Partial ElementDefinition from StructureDefinition to check extension is
// "id": "Patient.address.extension:address-preferred",
// "path": "Patient.address.extension",
val listOfElementDefinition =
structureDefinition.snapshot.element.filter { it.path.equals("$extensionForType.extension") }
listOfElementDefinition.forEach {
if (it.id.substringAfterLast(":").equals(fieldName)) {
return true
if (it.id.substringAfterLast(":") == fieldName) {
return it
}
}
return false
return null
}

/**
Expand All @@ -519,6 +525,8 @@ private fun isExtensionSupportedByProfile(
* @param questionnaireItem QuestionnaireItemComponent with details for extension
* @param questionnaireResponseItem QuestionnaireResponseItemComponent for response value
* @param base
* @param fieldName
* @param elementDefinition
* - resource's Base class instance See
* https://hapifhir.io/hapi-fhir/docs/model/profiles_and_extensions.html#extensions for more on
* custom extensions
Expand All @@ -527,22 +535,24 @@ private fun addDefinitionBasedCustomExtension(
questionnaireItem: Questionnaire.QuestionnaireItemComponent,
questionnaireResponseItem: QuestionnaireResponse.QuestionnaireResponseItemComponent,
base: Base,
fieldName: String,
elementDefinition: ElementDefinition,
) {
// Create an extension
val extension =
Extension().apply {
url =
elementDefinition.type?.firstOrNull()?.profile?.firstOrNull()?.valueAsString
?: questionnaireItem.definition
setValue(questionnaireResponseItem.answer.first().value)
}

if (base is Type) {
// Create an extension
val ext = Extension()
ext.url = questionnaireItem.definition
ext.setValue(questionnaireResponseItem.answer.first().value)
// Add the extension to the resource
base.addExtension(ext)
base.addExtension(extension)
}
if (base is DomainResource) {
// Create an extension
val ext = Extension()
ext.url = questionnaireItem.definition
ext.setValue(questionnaireResponseItem.answer.first().value)
// Add the extension to the resource
base.addExtension(ext)
base.addExtension(extension)
}
}

Expand Down
Loading